how to add CRC to pymodbus RTU message?

24 views Asked by At

i need to read registers from a flow meter over serial using pymodbus. the flow meter requires a CRC at the end of the message in order for it to return the data i need. i have no idea how to add the CRC to the message.

here's my code

from pymodbus.client.sync import ModbusSerialClient
modbusRTUPort = '/dev/ttymxc3'
modbusRTUBaudRate = 19200
flowTotalFWDAddress = int(7008)

modbusClient = ModbusSerialClient(
    method='rtu',
    port=modbusRTUPort ,
    baudrate=mmodbusRTUBaudRate ,
    parity='N',
    stopbits=1,
    bytesize=8
)

if modbusClient.connect():
    print('Modbus Connexion successful')
    request = modbusClient.read_discrete_inputs(flowTotalFWDAddress, 2, unit=1)
    if not request.isError():
        print(f'request: {request.registers}')
    else:
        print(f'ERROR: {request}')

    modbusClient.close()

so, dirt simple. i want to connect and get a message. don't even care to decode at this point. when i run the code, i get this response:

Modbus Connexion successful
ERROR: Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, 
  expected at least 2 bytes (0 received)

now i know that i'm getting zero bytes back because i need to have a CRC tacked on the end of the message... but i have no idea how to do that. i read the docs and found no examples, just the computeCRC utility, which generates the correct CRC BTW (A2 FE).

so can someone who knows modbus please illuminate how to add the CRC to the payload so the device will talk to me? thx.

EDIT: i was diving thru the docs and found a section about Framer? specifically pymodbus.framer.rtu-framer.ModbusRtuFramer but no examples or real explanation of how to use it. it appears to allow sequencing a CRC into the mix, but as to how... i have no idea. granted, this is not my forte, but i want to learn and get better at this.

0

There are 0 answers