fg71
I try to understand why I get sometimes error from my python script to talk to Modbus Tcp. When this only happen when I use OpenPLC. I do the same example in Arduino IDE and I do not get any error. I use Arduino Nano Esp32.
 
Why does below python script only work sometimes without any modification?
 
Does any know if OpenPLC implementation is different from saying using <ModbusIP_ESP8266.h> in Arduino IDE.
 
  1. I have setup OpenPLC with Arduino Nano ESP32 with 1 contact and 1 coil.
  2. The contact use %QX0.0 and coil %QX0.3
  3. I have setup Modbus TCP communication
  4. All compile and download to the Arduino Nano ESP3
I use pymodbus module in python 3 here is my code.
from pymodbus.client import ModbusTcpClient
from pymodbus import pymodbus_apply_logging_config

client = ModbusTcpClient('192.168.0.189')
if client.connect():
    print("Connect to Modbus server...")

pymodbus_apply_logging_config("DEBUG")

"""
Write to coil
"""
client.write_coil(0, True, slave=1)

r = client.read_coils(0, 1, slave=1)
print(r.bits)
print(r.bits[0])

client.close()
 
Here is the log that is correct
 
>>> %Run read_modbus.py
Connect to Modbus server...
2024-09-29 13:05:09,940 DEBUG logging:103 Current transaction state - IDLE
2024-09-29 13:05:09,940 DEBUG logging:103 Running transaction 1
2024-09-29 13:05:09,940 DEBUG logging:103 SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-09-29 13:05:09,940 DEBUG logging:103 New Transaction state "SENDING"
2024-09-29 13:05:09,940 DEBUG logging:103 Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2024-09-29 13:05:09,940 DEBUG logging:103 Transaction failed. (Modbus Error: [Connection] ModbusTcpClient 192.168.0.189:502: Connection unexpectedly closed 0.000 seconds into read of 8 bytes without response from slave before it closed connection)
2024-09-29 13:05:09,940 DEBUG logging:103 Processing:
2024-09-29 13:05:09,940 DEBUG logging:103 Getting transaction 1
2024-09-29 13:05:09,940 DEBUG logging:103 Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
2024-09-29 13:05:09,950 DEBUG logging:103 Connection to Modbus server established. Socket ('192.168.0.177', 55808)
2024-09-29 13:05:09,950 DEBUG logging:103 Current transaction state - TRANSACTION_COMPLETE
2024-09-29 13:05:09,950 DEBUG logging:103 Running transaction 2
2024-09-29 13:05:09,950 DEBUG logging:103 SEND: 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x1 0x0 0x0 0x0 0x1
2024-09-29 13:05:09,950 DEBUG logging:103 New Transaction state "SENDING"
2024-09-29 13:05:09,950 DEBUG logging:103 Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2024-09-29 13:05:09,962 WARNING logging:109  after returning 10 bytes: b'\x00\x02\x00\x00\x00\x04\x01\x01\x01\x01'
2024-09-29 13:05:09,962 DEBUG logging:103 Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
2024-09-29 13:05:09,962 DEBUG logging:103 RECV: 0x0 0x2 0x0 0x0 0x0 0x4 0x1 0x1 0x1 0x1
2024-09-29 13:05:09,962 DEBUG logging:103 Got response!!!
2024-09-29 13:05:09,962 DEBUG logging:103 Processing: 0x0 0x2 0x0 0x0 0x0 0x4 0x1 0x1 0x1 0x1
2024-09-29 13:05:09,962 DEBUG logging:103 Factory Response[ReadCoilsResponse': 1]
2024-09-29 13:05:09,962 DEBUG logging:103 Adding transaction 2
2024-09-29 13:05:09,962 DEBUG logging:103 Getting transaction 2
2024-09-29 13:05:09,962 DEBUG logging:103 Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
[True, False, False, False, False, False, False, False]
True
>>>

>>> %Run read_modbus.py
Connect to Modbus server...
2024-09-29 13:22:12,364 DEBUG logging:103 Current transaction state - IDLE
2024-09-29 13:22:12,364 DEBUG logging:103 Running transaction 1
2024-09-29 13:22:12,364 DEBUG logging:103 SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-09-29 13:22:12,364 DEBUG logging:103 New Transaction state "SENDING"
2024-09-29 13:22:12,365 DEBUG logging:103 Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2024-09-29 13:22:12,373 DEBUG logging:103 Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
2024-09-29 13:22:12,374 DEBUG logging:103 RECV: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-09-29 13:22:12,374 DEBUG logging:103 Processing: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-09-29 13:22:12,374 DEBUG logging:103 Factory Response[WriteSingleCoilResponse': 5]
2024-09-29 13:22:12,374 DEBUG logging:103 Adding transaction 1
2024-09-29 13:22:12,374 DEBUG logging:103 Getting transaction 1
2024-09-29 13:22:12,374 DEBUG logging:103 Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
2024-09-29 13:22:12,374 DEBUG logging:103 Current transaction state - TRANSACTION_COMPLETE
2024-09-29 13:22:12,374 DEBUG logging:103 Running transaction 2
2024-09-29 13:22:12,374 DEBUG logging:103 SEND: 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x1 0x0 0x0 0x0 0x1
2024-09-29 13:22:12,374 DEBUG logging:103 New Transaction state "SENDING"
2024-09-29 13:22:12,375 DEBUG logging:103 Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2024-09-29 13:22:12,375 DEBUG logging:103 Transaction failed. (Modbus Error: [Connection] ModbusTcpClient 192.168.0.189:502: Connection unexpectedly closed 0.000 seconds into read of 8 bytes without response from slave before it closed connection)
2024-09-29 13:22:12,375 DEBUG logging:103 Processing:
2024-09-29 13:22:12,375 DEBUG logging:103 Getting transaction 2
2024-09-29 13:22:12,375 DEBUG logging:103 Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
Traceback (most recent call last):
  File "C:\Users\fgustafsson_user\Documents\OpenPLC\ModbusTcp\ModbusTcp_v1\python\read_modbus.py", line 16, in <module>
    print(r.bits)
AttributeError: 'ModbusIOException' object has no attribute 'bits'
>>>
 
The log above is the error log, it looks to me compare the 2 logs the error log are missing: Connection to Modbus server established
Reply 0 0
fg71
I will add that Node-Red configure as Modbus Tcp client have issue to connect to Arduino Nano ESP32 after OpenPLC Modbus server is made.
The same code in Node-Red using Arduino IDE with <ModbusIP_ESP8266.h> do not have any problem to connect from Node-Red.
The error message: port not open. I use port 502 default modbus..
 
So know I have try both pymodbus and node-red and still there is serious problem with OpenPLC modbus tcp server implementation.
I did also try umodbus other Python module and it work better with OpenPLC Modbus Tcp.
 
In my first post I said that I have done same examples with Arduino IDE to make Modbus Tcp Server and there has not been any issue from Python or Node-Red.
Reply 0 0
fg71
Here is screenshot of my Node-Red setup. The first injection use fc: 1 and last 3 injection use fc: 5. I use the IP and port 502 for my Arduino Nano ESp32.
[203128%2F9002628%2Fmceclip0]
 
 
Here is my code in Arduino IDE if you like to try.
/*
    DIGITAL_INPUT: 2, 3, 4, 5, 6
    DIGITAL_OUTPUT: 7, 8, 12, 13
    ANALOG_INPUT: A0, A1, A2, A3, A4, A5
    ANALOG_OUTPUT: 9, 10, 11
*/

#include <WiFi.h>
#include <ModbusIP_ESP8266.h>

const int QX0_0 = 0;
const int QX0_1 = 1;
const int QX0_2 = 2;
const int QX0_3 = 3;

const int ledPinRed = LED_RED;
const int ledPinGreen = LED_GREEN;
const int ledPinBlue = LED_BLUE;

const int ledPin = LED_BUILTIN;

ModbusIP mb;

void setup() {
  Serial.begin(115200);

  WiFi.begin("SSID", "PASSWORD");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  mb.server();

  pinMode(ledPin, OUTPUT);
  pinMode(ledPinRed, OUTPUT);
  pinMode(ledPinGreen, OUTPUT);
  pinMode(ledPinBlue, OUTPUT);
  mb.addCoil(QX0_0);
  mb.addCoil(QX0_1);
  mb.addCoil(QX0_2);
  mb.addCoil(QX0_3);
}

void loop() {
   //Call once inside loop() - all magic here
   mb.task();

   //Attach ledPin to LED_COIL register
   digitalWrite(ledPinRed, mb.Coil(QX0_0));
   digitalWrite(ledPinGreen, mb.Coil(QX0_1));
   digitalWrite(ledPinBlue, mb.Coil(QX0_2));
   digitalWrite(ledPin, mb.Coil(QX0_3));
   delay(10);
}
 
Reply 0 0
fg71
Side note:
 
If your communication with USB or dfu-util do not work in Windows 11 after using OpenPLC you maybe need to update the usb driver before you could download the bootloader.bin to your board.
 
 
From the menu you click: Options/List All Devices and pick your DFU and check so the USB-ID is correct for your board before you update the driver.
 
 
For Arduino Giga r1 ex.
"%LOCALAPPDATA%\Arduino15\packages\arduino\tools\dfu-util\0.11.0-arduino5\dfu-util" --device ,0x0483:0xdf11 -D "%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\mbed_giga\4.1.5\bootloaders\GIGA\bootloader.bin" -a0 --dfuse-address=0x8000000
 
Reply 0 0
fg71
I find a bug in the hal for Arduino Giga r1 board.
 
The hal should be:
# HAL configuration
# Format :
# Board type, FQBN

Arduino Giga, arduino:mbed_giga:giga
 
Reply 0 0
thiagoralves
OpenPLC does not use ModbusIP_ESP8266.h as that's specific just for ESP8266 boards. Instead, OpenPLC has its own Modbus implementation that should work on all boards. Can you share how you are setting up your Modbus settings on OpenPLC? Once you upload your PLC program to the board, can you ping the board from the network? Does it respond to the pings? Arduino Nano ESP32 is not an officially supported board, so maybe there are differences in there that makes it not work correctly.
 
Also, for your Arduino Giga comment, the hal file is not used anymore by the editor. Now the editor only needs the global hals.json file.
Reply 0 0
fg71
OpenPLC does not use ModbusIP_ESP8266.h as that's specific just for ESP8266 boards
It also work with ESP32 boards include Arduino Nano Esp32.
 
[203128%2F9005329%2Fmceclip0]
 
[203128%2F9005330%2Fmceclip0]
 
 
The code below will set true to contact %QX0.0 and the coil %QX0.3 become true. This is working with Nano ESP32. But the problem is that connection to the server is not always working. I'm not sure if this is python or pymodbus or server thing. Maybe its normal that if read and write will lost the connection to the server and later connect. I have read in random places that people have difficulty with modbus when it looks like there is connection problem.
 
I do not think there is anything wrong with my code or how i have use it in OpenPLC its not different from your @thiagoralves example in Youtube.
from pymodbus.client import ModbusTcpClient
from pymodbus import pymodbus_apply_logging_config

client = ModbusTcpClient('192.168.0.50', port=502)
if client.connect():
    print("Connect to Modbus server...")

pymodbus_apply_logging_config("DEBUG")
client.write_coil(0, True, slave=1)

res = client.read_coils(0, 8, slave=1)
print("Output:", res.bits[0], res.bits[3])
client.close()
 
Reply 0 0
fg71
If you google this error I'm not alone to have it.
Modbus Error: [Connection] ModbusTcpClient 192.168.0.50:502: Connection unexpectedly closed 0.000 seconds into read of 8 bytes without response from slave before it closed connection)
 
And most of the case it happens when you have read and write statement in the pymodbus code.
Reply 0 0
thiagoralves
So, just to clarify, this issue happens every time there is a read after write, or randomly?
Reply 0 0
fg71
And to make sure its not pymodbus problem I made a simple example in Siemens LOGO.
[203128%2F9005480%2Fmceclip0]
 
The modbus tcp address for memory bite M1 = 8256 and the coil Q1 = 8192.
The script is setting true/false to memory M1 with 3 seconds in between.
 
[203128%2F9005483%2Fmceclip0]
Reply 0 0
fg71
So I'm thinking there is no connection problem with Siemens LOGO but I do have problem with Arduino Nano Esp32 and Arduino Giga r1.
 
Doing the same thing with OpenPLC is not possible.
 
Meaning i get
ModbusTcpClient 192.168.0.50:502: Connection unexpectedly closed 0.000 seconds into read of 8 bytes without response from slave before it closed connection)
Reply 0 0
thiagoralves
Does it happen every time there is a read after write, or randomly?
Reply 0 0
thiagoralves
Just tested your script from your first post on a ESP32-WROOM-32D programmed using the "Generic ESP32" entry on the latest version of the OpenPLC Editor. I programmed it with the Blink example and just added a location for the blink_led (%QX0.1 - modbus coil 1) and created another variable to be manipulated using Modbus (%QX0.0 - modbus coil 0):
Screenshot 2024-10-01 at 10.35.55 AM.png
 
From your script I only modified the IP address of my device and the address of the coils. Here is the final script just for reference:
from pymodbus.client import ModbusTcpClient
from pymodbus import pymodbus_apply_logging_config

client = ModbusTcpClient('192.168.1.195', port=502)
if client.connect():
    print("Connected to Modbus server...")

pymodbus_apply_logging_config("DEBUG")
client.write_coil(0, True, slave=1)

res = client.read_coils(0,8,slave=1)
print("Output:", res.bits[0], res.bits[1])
client.close()
After running this, the script immediately sets my %QX0.0 to true on my ESP32 board. This is the script output:
 
Connected to Modbus server...
2024-10-01 10:34:14,845 DEBUG logging:103 Current transaction state - IDLE
2024-10-01 10:34:14,845 DEBUG logging:103 Running transaction 1
2024-10-01 10:34:14,845 DEBUG logging:103 SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-10-01 10:34:14,845 DEBUG logging:103 New Transaction state "SENDING"
2024-10-01 10:34:14,845 DEBUG logging:103 Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2024-10-01 10:34:14,931 DEBUG logging:103 Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
2024-10-01 10:34:14,931 DEBUG logging:103 RECV: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-10-01 10:34:14,931 DEBUG logging:103 Processing: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2024-10-01 10:34:14,931 DEBUG logging:103 Factory Response[WriteSingleCoilResponse': 5]
2024-10-01 10:34:14,931 DEBUG logging:103 Adding transaction 1
2024-10-01 10:34:14,931 DEBUG logging:103 Getting transaction 1
2024-10-01 10:34:14,931 DEBUG logging:103 Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
2024-10-01 10:34:14,931 DEBUG logging:103 Current transaction state - TRANSACTION_COMPLETE
2024-10-01 10:34:14,931 DEBUG logging:103 Running transaction 2
2024-10-01 10:34:14,931 DEBUG logging:103 SEND: 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x1 0x0 0x0 0x0 0x8
2024-10-01 10:34:14,931 DEBUG logging:103 New Transaction state "SENDING"
2024-10-01 10:34:14,931 DEBUG logging:103 Changing transaction state from "SENDING" to "WAITING FOR REPLY"
2024-10-01 10:34:14,949 DEBUG logging:103 Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
2024-10-01 10:34:14,949 DEBUG logging:103 RECV: 0x0 0x2 0x0 0x0 0x0 0x4 0x1 0x1 0x1 0x1
2024-10-01 10:34:14,949 DEBUG logging:103 Processing: 0x0 0x2 0x0 0x0 0x0 0x4 0x1 0x1 0x1 0x1
2024-10-01 10:34:14,950 DEBUG logging:103 Factory Response[ReadCoilsResponse': 1]
2024-10-01 10:34:14,950 DEBUG logging:103 Adding transaction 2
2024-10-01 10:34:14,950 DEBUG logging:103 Getting transaction 2
2024-10-01 10:34:14,950 DEBUG logging:103 Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
Output: True False
As I said before, the Arduino Nano ESP32 is not an officially supported board. Most likely the network is not correctly initialized when you try to use one of the generic ESP32 entries. As for the Arduino Giga, the support for this board was added by a user from this community. I believe he mentioned that TCP support was not entirely implemented. Since I don't have those boards, I can't debug it to fix the issue. Most likely it is just a simple library misuse or a slightly different API to initialize the network.
Reply 0 0
fg71
So, just to clarify, this issue happens every time there is a read after write, or randomly?
It happen randomly, in other words send python script 10 times and 1 second in between I maybe get the LED to High and to LOW.
 
 
I'm not sure I could be any helpful to fix the issue when I do not know where to look.
I have looking for modbus implementation for the Giga r1 board or maybe other STM32 boards.
 
I did get Nano Esp32 to work with this library ModbusIP_ESP8266.h in Arduino IDE. And I do understand OpenPLC use other implementation.
Reply 0 0
thiagoralves
I changed my script to send Modbus requests every 100ms in an infinite loop:
from pymodbus.client import ModbusTcpClient
from pymodbus import pymodbus_apply_logging_config
import time

client = ModbusTcpClient('192.168.1.195', port=502)
if client.connect():
    print("Connected to Modbus server...")

pymodbus_apply_logging_config("DEBUG")

value = True

while True:
    client.write_coil(0, value, slave=1)
    res = client.read_coils(0,8,slave=1)
    print("Output:", res.bits[0], res.bits[1])
    value = not value
    time.sleep(0.1)

client.close()
I let it run for a few minutes talking with my ESP32 board and I couldn't spot a single failure. Indeed the issue might be caused by the different network implementations from in these new boards.
Reply 0 0
Reply