rtu_master_err.py 682 B

1234567891011121314151617181920
  1. # Import modbus module
  2. import modbus
  3. # Create a ModBusRTU object, specify the send buffer and receive buffer size as 128 bytes
  4. mb = modbus.ModBusRTU(128, 128)
  5. # Set slave address to 1
  6. mb.setSlave(1)
  7. # Generate a request frame for reading registers, specify the start address as 0 and the quantity as 10
  8. send_buff = mb.serializeReadRegisters(0, 10)
  9. # Print the byte string of the request frame
  10. print(send_buff)
  11. # Parse a response frame for reading registers, return a list containing the values of the registers
  12. host_regists = mb.deserializeReadRegisters(
  13. b'\x01\x03\x14\x00\x00\x04\xD2\x00\x00\x00\x00\x00\x7B\x00\x00\x00\x00\x00\x00\x00\x00\xE5\x0B'
  14. )
  15. print(host_regists)