Wadam00
Hi!
I'm trying to figure out how to control multiple modbus devices from an esp32 or any other microcontroller with openplc.
Can i get some help.
I know there is no webserver but i cant find any options in openplc editor to add modbus devices.
Reply 0 0
fg71
If you like to control multiple Modbus devices you need to configure it (ESP32) as Client
You connect to a device, read/write, close connection
You repeat it for every device.
Make sure 2 device do not have a open connection at same time.
 
If you ask me I would do it in ST and use sequence approach instead of trying to do it in Ladder Diagram.
 
[203128%2F9241234%2Fmceclip0]
Reply 0 0
Wadam00
If you like to control multiple Modbus devices you need to configure it (ESP32) as Client
You connect to a device, read/write, close connection
You repeat it for every device.
Make sure 2 device do not have a open connection at same time.
 
If you ask me I would do it in ST and use sequence approach instead of trying to do it in Ladder Diagram.
 
[203128%2F9241234%2Fmceclip0]
Hey i want to control modbus rtu devices.
Reply 0 0
thiagoralves
OpenPLC does not support Modbus master on microcontrollers. You can only do this using the Windows or Linux runtime (on a Raspbery Pi for example)
Reply 1 0
Wadam00
OpenPLC does not support Modbus master on microcontrollers. You can only do this using the Windows or Linux runtime (on a Raspbery Pi for example)
super!
thanks for the info.
Reply 0 0
fg71
I have made Modbus Client in OpenPLC Editor and upload it to Nano Esp32 without any problem. I used Modbus Tcp.
Like I have said before if you could do it in Arduino IDE you could do it in OpenPLC Editor.
 
Here is simple example in Arduino IDE to use Modbus Client to write true to memory bit M2 (address 8257) on Siemens LOGO
Like any board some libraries is not compatible but ModbusIP_ESP8266.h does support ESP32
 
I understand that you are using Modbus RTU but I do not think it will be so different.
#include <WiFi.h>
#include <ModbusIP_ESP8266.h> // ESP-IDF Arduino core boards

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSWORD";

IPAddress serverIP(SERVER_IP); // Replace with the server's IP address

// Create a ModbusIP object
ModbusIP mb;

void setup() {
  // Initialize Serial for debugging
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  // Initialize Modbus TCP client
  mb.client();
  mb.connect(serverIP);
}

unsigned long previousMillis = 0;
const long interval = 1000;
bool state = false;

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    state = true;
    /*
    if (state == false) {
      state = true;
    } else {
      state = false;
    }
    */
    mb.writeCoil(serverIP, 8257, state);
    mb.task();
  }
}
 
Reply 0 0
fg71
So what you could do is... if a output is TRUE and its location is used you also know it will use Arduino API.
You could read the state of Arduino API and write the state to Modbus API.
You could do all this in the loop() for a Arduino extension.
 
You will not interfere with the runtime because you only read the state of variable and forward it state to Modbus API.
That is not different from Gateway to Bridge between Arduino API and Modbus API.
 
The plc task is running before the Arduino task in the cycle.
Reply 0 0
richw
You could use the Arduino sketch option in the editor to make your esp32 a RTU modbus master. I did it a while ago and was able to read 3 slaves with no problems. Be carful not to delay the PLC loop by trying to read all your slave devices in one scan cycle.
Reply 0 0
Reply