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.
- I have setup OpenPLC with Arduino Nano ESP32 with 1 contact and 1 coil.
- The contact use %QX0.0 and coil %QX0.3
- I have setup Modbus TCP communication
- 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