1
votes

Hello Guys I need some help here

code:

from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import logging

def readModbusData():
    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)

    client=ModbusClient(method='rtu',port='COM3',
    baudrate=9600,timeout=1,parity='N',stopbits=2)
    print(client.connect())
    client.debug_enabled()

    log.debug("Read holding registers")
    response=client.read_holding_registers(0,1)

    print(response) #This returns the response for whole length of registers

    # print(response.getRegister(0));  #This returns value of only one 

    client.close()
readModbusData()
1
Console logs: DEBUG:pymodbus.transaction:SEND: 0x0 0x3 0x0 0x0 0x0 0x1 0x85 0xdb DEBUG:pymodbus.client.sync:New Transaction state 'SENDING' DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY' Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)Ashish Batra
I suggest that you test the Modbus device with (instead of your python code) CAS Modbus Scanner (free) or Modbus Poll (free trial). Both are Modbus master/client tools that I know work. If your it works with those tools, then your problem is in your code code; if it does not work with them, the problem is in the Modbus slave device.Marker
You need to pass the device address response=client.read_holding_registers(0,1, unit=<your slave address here>)Sanju
it worked with minimalmodbus.Thanks everyone for their response.Ashish Batra
@AshishBatra I posted an answer, I hope it to be helpfulBenyamin Jafari

1 Answers

0
votes

Check this stack_post,

You forgot the unit argument and using print(response.registers) instead of print(response).

You should having like the following snippet code:

response = client.read_holding_registers(0, 1, unit=<set-the-unit-ID>)  # Note.

if not response.isError():
    '''isError() method implemented in pymodbus 1.4.0 and above'''

    print(response.registers)  # Note.

else:
    # Handle Error.
    print('Unable to read or there is the connection problem.')

[NOTE]:

  • In many cases unit is 1 in slave side as default.