yoyo
yoyo

Reputation: 69

Connecting and reading modbus protocol

I'm completely new to Modbus so sorry if this question seems rudimentary or if i miss certain aspects. Basically i have a sensor which outputs data in modbus protocol i have purchased a USB TO RS485 Connector im connecting this to my computer and its coming up as COM7. From reading the sensor documentation it seems like i need to connect to the sensor with the following specifications:

  1. Port = COM7 -> checked it through device manager
  2. baudrate = 19200
  3. party = none
  4. stop bits = 1
  5. bytesize = 8
  6. method is rtu

I am trying to establish this connection by adding in the following commands:

from pymodbus.client.sync import ModBusSerialClient

serial = ModBusSerialClient(method='rtu', port='COM7', baudrate='19200', parity='0')
serial.connect()

I couldn't figure out how to add the stop bits and byte size but this is running with no issues.

From there i need to send the following command to the sensor so that i can read the data: 02 04 00 76 00 02 90 22 to registers 30118 and 30119. I have read the pymodbus documentation and know that i can do this by using the writetoregisters method but how do i convert that hex data to modbus protocol?

Upvotes: 0

Views: 7610

Answers (1)

Marcos G.
Marcos G.

Reputation: 3516

See here for a more detailed description of Modbus function code 04 (read input registers).

You will find out that 02 04 00 76 00 02 90 22 translates to:

02: slave address decimal 2 (unit for pymodbus)

04: function code 04 (read input regs)

0076: start reading on register decimal 118+30001=30119

0002: read 2 registers

9022: checksum of 02 04 00 76 00 02, check it yourself here; don't forget to select HEX instead of ASCII

But as Brits wrote in his comments, the whole idea of using pymodbus is that you can just add this line to your code:

 regs = serial.read_input_registers(0x76,2,unit=2)

And then you can show the contents of the result with:

print regs.registers

Depending on your device you might need to change 0x76 to 0x75 to start reading on register 30118.

Upvotes: 3

Related Questions