0

I have started the modbus rtu server using the following code.

server=StartSerialServer(context, framer=ModbusRtuFramer, identity=identity,
                        port='/dev/ttyUSB0', 
                        timeout=1, 
                        baudrate=9600, 
                        parity='N',
                        bytesize=8,
                        stopbits=1)`

and reading the registers using the following code:

def read_all_data(slave_id,context,type,address,no_of_reg):

      while True:
        print("Slave_id ",slave_id,"type ",type,"Address ",address,"No of Reg ",no_of_reg)

        data = context[slave_id].getValues(type, address, no_of_reg)
        print("data recv",data,context[slave_id].ModbusRtuFramer)
        time.sleep(1)

Is there any way where i can capture the raw bytes beforit is being processed . Any help would be highly appreciated

In order to to see change currently i am fetching all the register values and then appending it in the the list then i am comparing the value from the previous version of list and check the change to get the value of register type and address which have been changed. This requires an infinit loop. In order to avoid that loop I want to get raw bytes which it receives so that i can straight away decode it and extract register address and register type in order to order to fetch it from the context using above context.I have tried intializing custom handlers but none of them could help me out . Tried accessing the funtion of the ModbusRtuFramer class getRawbytes where he is unable to access the buffer which was never intialized becase class was passed as the parameter in StartSerialServer not an object of the class.

  • 1
    You could write your own framer (using the [rtu_framer](https://github.com/pymodbus-dev/pymodbus/blob/dev/pymodbus/framer/rtu_framer.py) as a base. However I'm struggling to understand your end goal here; perhaps show your current attempt and we may be able to suggest alternatives (If I have understood your aim then I would have thought it would be easier to handle things in the datastore). – Brits Feb 04 '23 at 21:13
  • 1
    The easiest way would be to use `rtu_framer` and overload `processIncomingPacket` with whatever you need to do. You don't need any additional interrupts, `processIncomingPacket` will be triggered as soon as you get a frame. – Marcos G. Feb 06 '23 at 08:08
  • my ultimate aim is to capture the packet and extract, the slave id register address, register type, and value. Then I want to use them for further processing. – Umer Farooq Feb 08 '23 at 09:27
  • In that case the advice above (re the rtu framer) is probably your best option. However, as we don't know your end goal, its difficult to provide further suggestions. – Brits Feb 08 '23 at 20:53
  • I solved the issue by creating custom modbusrtuframer as suggested by @Brits – Umer Farooq Feb 09 '23 at 14:59

0 Answers0