Hi,
I am pretty noob in Unitronics and Modbus, but I am trying my best to learn on the go.
We have a V1210 with modbus configured and I am trying to read holding registers from there as I would like to get this data to my linux server every 1-2 seconds.
I have tried to follow https://pymodbus.readthedocs.io/en/latest/source/client.html official documentation and create a .py for collecting the data.
It seems that I am able to get the connection successfully and also my read request (modbus code 3) is sent successfully, but V1210 is responding with non-standard response, which is not recognised as Modbus protocol by my python client or Wireshark.
I try to give all relevant information, but please ask more if my problem is not clear or detailed enough for giving answers.
Modbus is working normally between this V1210 and other V1210:s we have with same connection details.
I have tried to also use 40321 directly in the code, but it has the same results, and according to my understanding of pymodbus we should use 320.
If anyone has example code of pymodbus client read holding registers towards V1210 I would appreciate a lot to receive it!
My modbus config in V1210:
Network ID: 255
Port 20257
Error I get:
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('IP of PLC', 59772)
DEBUG:pymodbus.logging:Current transaction state - IDLE
DEBUG:pymodbus.logging:Running transaction 1
DEBUG:pymodbus.logging:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x9d 0x80 0x0 0x1
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 8 bytes (6 received))
DEBUG:pymodbus.logging:Processing:
DEBUG:pymodbus.logging:Getting transaction 1
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
Register Values: Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 8 bytes (6 received)
My python code:
import logging
from pymodbus.client import ModbusTcpClient
from pymodbus.exceptions import ModbusIOException
# Configure logging for pymodbus
logging.basicConfig()
log = logging.getLogger('pymodbus')
log.setLevel(logging.DEBUG)
# Unitronics Modbus TCP configuration details
PLC_IP_ADDRESS = "IP of the PLC"
PLC_PORT = 20257
UNIT_ID = 255 # Network ID
TIMEOUT = 10 # Timeout in seconds (100 units of 10 ms each)
RETRIES = 3 # Number of retries
# Create a Modbus TCP client
client = ModbusTcpClient(PLC_IP_ADDRESS, port=PLC_PORT, timeout=TIMEOUT, retries=RETRIES)
# Connect to the PLC
client.connect()
# Reading holding registers corresponding to MI320 to MI322
START_ADDRESS = 320 # Start at MI320 (Modbus address 40321)
REGISTER_COUNT = 3 # Read MI320 to MI322 (3 registers)
try:
# Read holding registers
response = client.read_holding_registers(START_ADDRESS, REGISTER_COUNT, unit=UNIT_ID)
# Check if response is an error
if response.isError():
print("Error reading registers:", response)
else:
register_values = response.registers
print("Register Values:", register_values)
except ModbusIOException as e:
print("Modbus IO error:", e)
except Exception as e:
print("General error:", e)
finally:
# Close the client connection
client.close()