You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
trying to use pymodbus to read from a controller that uses TCP/IP communication , serial communication from the controller will be converted to TCP/IP using Moxa NPort IA-5150.. wasn't able to get it to read any register or a coil data. I'm sure that I'm missing some input.
from pymodbus.client import ModbusTcpClient
def connect_to_modbus_server():
# Create a Modbus TCP client instance
client = ModbusTcpClient('xx.x.xx.xx', port=502, timeout=5)
print(f"Attempting to connect to xx.x.xx.xx on port 502...")
connection = client.connect()
if connection:
print("Successfully connected...")
print(f"Socket details: {client.socket}")
print(f"Socket connected: {client.socket is not None}")
print(f"Socket timeout: {client.socket.gettimeout()}")
try:
# Try different slave IDs (in older versions it's called 'slave' not 'unit')
print("\n--- Testing different Slave IDs ---")
for slave_id in range(1, 6): # Try slave IDs 1-5
try:
print(f"\nTrying Slave ID: {slave_id}")
response = client.read_holding_registers(0, 1, slave=slave_id)
print(f"Response: {response if response.isError() else response.registers}")
except Exception as e:
print(f"Exception: {e}")
# Try different register types with slave ID 1
print("\n--- Testing different register types ---")
slave_id = 1 # Default Modbus slave ID
try:
print("\nTrying Input Registers (function code 4)")
response = client.read_input_registers(0, 1, slave=slave_id)
print(f"Response: {response if response.isError() else response.registers}")
except Exception as e:
print(f"Exception: {e}")
try:
print("\nTrying Coils (function code 1)")
response = client.read_coils(0, 1, slave=slave_id)
print(f"Response: {response if response.isError() else response.bits}")
except Exception as e:
print(f"Exception: {e}")
try:
print("\nTrying Discrete Inputs (function code 2)")
response = client.read_discrete_inputs(0, 1, slave=slave_id)
print(f"Response: {response if response.isError() else response.bits}")
except Exception as e:
print(f"Exception: {e}")
# Try a few different address ranges for holding registers
print("\n--- Testing different address ranges ---")
for address in [0, 100, 1000, 3000, 4000]:
try:
print(f"\nTrying Holding Registers at address {address}")
response = client.read_holding_registers(address, 1, slave=slave_id)
print(f"Response: {response if response.isError() else response.registers}")
except Exception as e:
print(f"Exception: {e}")
# Try Modbus device ID function (0x11)
try:
print("\n--- Testing Modbus Device Identification ---")
rq = client.execute(0x11, b'\x00')
print(f"Device ID response: {rq}")
except Exception as e:
print(f"Device ID exception: {e}")
except Exception as e:
print(f"General exception: {e}")
finally:
# Close the connection when done
client.close()
print("\nConnection closed")
else:
print("Failed to connect to the Modbus server. Check IP, port, and network connectivity.")
if __name__ == "__main__":
connect_to_modbus_server()
And here is the output i get:
Attempting to connect to xx.x.xx.xx on port 502...
Successfully connected...
Socket details: <socket.socket fd=288, family=2, type=1, proto=0, laddr=('10.248.67.69', 54883), raddr=('xx.x.xx.xx', 502)>
Socket connected: True
Socket timeout: 5.0
--- Testing different Slave IDs ---
Trying Slave ID: 1
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Slave ID: 2
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Slave ID: 3
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Slave ID: 4
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Slave ID: 5
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
--- Testing different register types ---
Trying Input Registers (function code 4)
Exception: ModbusClientMixin.read_input_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Coils (function code 1)
Exception: ModbusClientMixin.read_coils() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Discrete Inputs (function code 2)
Exception: ModbusClientMixin.read_discrete_inputs() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
--- Testing different address ranges ---
Trying Holding Registers at address 0
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Holding Registers at address 100
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Holding Registers at address 1000
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Holding Registers at address 3000
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Trying Holding Registers at address 4000
Exception: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
--- Testing Modbus Device Identification ---
Device ID exception: 'bytes' object has no attribute 'transaction_id'
Connection closed
Note that whenever i run the following: response = client.read_holding_registers(address=0, count=1, unit=1)
I get: TypeError: ModbusClientMixin.read_holding_registers() got an unexpected keyword argument 'unit'
I have uninstalled and installed the library several times already, which didn't help.
The text was updated successfully, but these errors were encountered:
Hello,
trying to use pymodbus to read from a controller that uses TCP/IP communication , serial communication from the controller will be converted to TCP/IP using Moxa NPort IA-5150.. wasn't able to get it to read any register or a coil data. I'm sure that I'm missing some input.
And here is the output i get:
Note that whenever i run the following:
response = client.read_holding_registers(address=0, count=1, unit=1)
I get:
TypeError: ModbusClientMixin.read_holding_registers() got an unexpected keyword argument 'unit'
I have uninstalled and installed the library several times already, which didn't help.
The text was updated successfully, but these errors were encountered: