pymodbus-dev / pymodbus

A full modbus protocol written in python
Other
2.16k stars 889 forks source link

pymodbus 3.6.8 does not work, while works with modbus-tk and pymodbus 2.5.3 #2218

Closed rishabh1212 closed 2 weeks ago

rishabh1212 commented 2 weeks ago

Versions

Pymodbus Specific

Description

Code and Logs

Working for pymodbus 2.5.3

import time
import logging
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.constants import Defaults

# Enable logging
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# Create Modbus client
client = ModbusTcpClient('192.168.0.11', port=502, timeout=5.0)

while True:
    try:
        time.sleep(2)
        response = client.read_input_registers(4001, 1, unit=1)

        if not response.isError():
            print("Register value:", response.registers[0])
        else:
            logger.error(f"Error reading input register: {response}")

        time.sleep(0.1)

        # To write to a register, uncomment and use the following line:
        # client.write_register(4100, response.registers[0], unit=1)

    except Exception as e:
        logger.error(str(e))
        client.close()
        break

Pymodbus 2.5.3 Working log

DEBUG:pymodbus.client.sync:Connection to Modbus server established. Socket ('192.168.0.55', 58178)
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x4 0x2 0x0 0x2d
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x4 0x2 0x0 0x2d
DEBUG:pymodbus.factory:Factory Response[ReadInputRegistersResponse: 4]
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Register value: 45
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 2
DEBUG:pymodbus.transaction:SEND: 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x2 0x0 0x0 0x0 0x5 0x1 0x4 0x2 0x0 0x2f
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x2 0x0 0x0 0x0 0x5 0x1 0x4 0x2 0x0 0x2f
DEBUG:pymodbus.factory:Factory Response[ReadInputRegistersResponse: 4]
DEBUG:pymodbus.transaction:Adding transaction 2
DEBUG:pymodbus.transaction:Getting transaction 2
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Register value: 47
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 3
DEBUG:pymodbus.transaction:SEND: 0x0 0x3 0x0 0x0 0x0 0x6 0x1 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x3 0x0 0x0 0x0 0x5 0x1 0x4 0x2 0x0 0x31
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x3 0x0 0x0 0x0 0x5 0x1 0x4 0x2 0x0 0x31
DEBUG:pymodbus.factory:Factory Response[ReadInputRegistersResponse: 4]
DEBUG:pymodbus.transaction:Adding transaction 3
DEBUG:pymodbus.transaction:Getting transaction 3
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Register value: 49

Working for modbus-tk

import modbus_tk.modbus_tcp as modbus_tcp
import modbus_tk.defines as cst

# Enable logging
import logging
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
client = modbus_tcp.TcpMaster(host='192.168.0.11', port=502)
client.set_timeout(5.0)
while True:
    try:
        import time
        time.sleep(2)
        # Read input register (address 4002 in your example)
        # Note: Modbus address 4002 corresponds to address 3 in zero-based index
        response = client.execute(1, cst.READ_INPUT_REGISTERS, 4001, 1)

        print("Register value:", response[0])
        time.sleep(0.1)
        # response = client.execute(1, cst.WRITE_SINGLE_REGISTER, 4100, output_value=response[0])
    except Exception as e:
        logger.error(str(e))
        client.close()

Working modbus-tk log

(.plc_test) ➜  IOT git:(main) ✗ python3 test_modbus_tk.py
Register value: 1
Register value: 3
Register value: 5
Register value: 7

Not working for pymodbus 3.6.8

import time
import logging
from pymodbus.client import ModbusTcpClient
from pymodbus.exceptions import ModbusException

# Enable logging
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# Create Modbus client
client = ModbusTcpClient('192.168.0.11', port=502, timeout=5.0)

while True:
    try:
        time.sleep(2)
        # Read input register (address 4002 in your example)
        # Note: Modbus address 4002 corresponds to address 4001 in zero-based index
        response = client.read_input_registers(4001, 1, unit=1)

        if not response.isError():
            print("Register value:", response.registers[0])
        else:
            logger.error(f"Error reading input register: {response}")

        time.sleep(0.1)

        # To write to a register, uncomment and use the following line:
        # client.write_register(4100, response.registers[0], unit=1)

    except ModbusException as e:
        logger.error(str(e))
        client.close()
        break

Not working log

DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('192.168.0.55', 58263)
DEBUG:pymodbus.logging:Current transaction state - IDLE
DEBUG:pymodbus.logging:Running transaction 1
DEBUG:pymodbus.logging:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x4 0xf 0xa1 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] No response received, expected at least 8 bytes (0 received)) 
DEBUG:pymodbus.logging:Processing: 
DEBUG:pymodbus.logging:Frame check, no more data!
DEBUG:pymodbus.logging:Getting transaction 1
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
ERROR:root:Error reading input register: Modbus Error: [Input/Output] Modbus Error: [Invalid Message] No response received, expected at least 8 bytes (0 received)
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('192.168.0.55', 58268)
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 2
DEBUG:pymodbus.logging:SEND: 0x0 0x2 0x0 0x0 0x0 0x6 0x0 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:No response received to unbounded read !!!!
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 
DEBUG:pymodbus.logging:Processing: 
DEBUG:pymodbus.logging:Frame check, no more data!
DEBUG:pymodbus.logging:Getting transaction 2
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
ERROR:root:Error reading input register: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('192.168.0.55', 58271)
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 3
DEBUG:pymodbus.logging:SEND: 0x0 0x3 0x0 0x0 0x0 0x6 0x0 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:No response received to unbounded read !!!!
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 
DEBUG:pymodbus.logging:Processing: 
DEBUG:pymodbus.logging:Frame check, no more data!
DEBUG:pymodbus.logging:Getting transaction 3
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
ERROR:root:Error reading input register: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('192.168.0.55', 58277)
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 4
DEBUG:pymodbus.logging:SEND: 0x0 0x4 0x0 0x0 0x0 0x6 0x0 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:No response received to unbounded read !!!!
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 
DEBUG:pymodbus.logging:Processing: 
DEBUG:pymodbus.logging:Frame check, no more data!
DEBUG:pymodbus.logging:Getting transaction 4
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
ERROR:root:Error reading input register: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('192.168.0.55', 58280)
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 5
DEBUG:pymodbus.logging:SEND: 0x0 0x5 0x0 0x0 0x0 0x6 0x0 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.logging:New Transaction state "SENDING"
DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"
DEBUG:pymodbus.logging:No response received to unbounded read !!!!
DEBUG:pymodbus.logging:Changing transaction state from "WAITING FOR REPLY" to "PROCESSING REPLY"
DEBUG:pymodbus.logging:RECV: 
DEBUG:pymodbus.logging:Processing: 
DEBUG:pymodbus.logging:Frame check, no more data!
DEBUG:pymodbus.logging:Getting transaction 5
DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"
ERROR:root:Error reading input register: Modbus Error: [Input/Output] No Response received from the remote slave/Unable to decode response
DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('192.168.0.55', 58281)
DEBUG:pymodbus.logging:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.logging:Running transaction 6
DEBUG:pymodbus.logging:SEND: 0x0 0x6 0x0 0x0 0x0 0x6 0x0 0x4 0xf 0xa1 0x0 0x1
DEBUG:pymodbus.logging:New Transaction state "SENDING"
janiversen commented 2 weeks ago

Clearly it does not work, unit= no longer exist, did you try to read the documentation?

rishabh1212 commented 2 weeks ago

@janiversen Thanks a lot for pointing it out 🙏 Its very convenient to have active supporting modbus community. I was using trying to figure out for a while now and stuck at 2.5.3! If it is possible to take into consideration of displaying warning on using invalid kwargs, it would be great!

Regards, Rishabh.

janiversen commented 2 weeks ago

we have parameter documentation in the code, and a full documentation on readthedocs, so I do not really see the need for a warning......there are many other cases where people should read the documentation.

janiversen commented 2 weeks ago

Ohhh forgot we have nearly replaced all kwargs, with named parameters among others yours.

rishabh1212 commented 2 weeks ago

Thanks! I will keep a note of these from next time.