Casey Koehn
Casey Koehn

Reputation: 23

How Can I Read The Output Of Modbus Sensor Using Python

I am trying to read this Keller depth sensor using Modbus with a Raspberry Pi 5. It reads pressure (in bar) and temperature (in Celsius). The water depth can then be calculated using those values. I have got to where I can read registers. Now, because I don't understand Modbus, I don't know what registers I need to read to access the data I want and how to properly read the data. The communication protocol documentation can be found here.

These are the communication basics:

Mode: RTU Mode (ASCII is not supported)

Coding System: 8–bit binary, hexadecimal 0–9, A–F
Two hexadecimal characters (bytes) contained in each 8–bit field of the message

Bits per Byte: 1 start bit
8 data bits, least significant bit sent first
No parity (default) 1 parity bit: even or odd parity (programmable)
1 stop bit (default) 2 stop bit (programmable) (Class.Group-version 5.21-XX.XX and 5.24-XX.XX)

Error Check Field: 2 Byte Cyclical Redundancy Check (CRC)

Baudrate: programmable 9’600baud (default) or 115’200baud

The code I have been using is this:

import minimalmodbus

mb_address = 1

sensy_boi = minimalmodbus.Instrument('/dev/ttyACM0',mb_address)

sensy_boi.serial.baudrate = 9600
sensy_boi.serial.bytesize = 8
sensy_boi.serial.parity = minimalmodbus.serial.PARITY_NONE
sensy_boi.serial.stopbits = 1
sensy_boi.serial.timeout  = 0.5
sensy_boi.mode = minimalmodbus.MODE_RTU
sensy_boi.clear_buffers_before_each_transaction = True
sensy_boi.close_port_after_each_call = True

print(sensy_boi) 

data =sensy_boi.read_registers(0, 2, 3)

print(f"Raw data is {data}")

sensy_boi.serial.close()

Upvotes: 1

Views: 442

Answers (1)

balun
balun

Reputation: 1324

As @Brits mentioned, you can use tools like mbpoll for a quick test.

Here I will recommend another useful python tool modpoll, which helps not only on quick troubleshooting but also further data acquisition using the same config file.

Create a config file keller-sensor.csv for generic Keller sensors with the following content,

device,testdev01,1,,
poll,holding_register,0,12,BE_BE
ref,Calculated_value,0,float32,r
ref,Pressure_of_sensor1,2,float32,r
ref,Pressure_of_sensor2,4,float32,r
ref,Temperature,6,float32,r
ref,Temperature_of_sensor1,8,float32,r
ref,Temperature_of_sensor2,10,float32,r

Note: Only show partial registers in section 4.4.1.1 Process Value Read Range (0x000x) for demo

Follow guide to install modpoll and run the following command,

modpoll --rtu /dev/ttyACM0 --rtu-baud 9600 --config keller-sensor.csv

You shall be able to view the device readings in realtime.

Furthermore, you can deploy modpoll program in production using the same config file, refer to the official document for more options.

Revised config file for older device models

According to keller manual's section 4.3.2, there are limits of maximal number of registers read in one cycle for some older models.

  • up to 2 registers for earlier versions than Class.Group-version 5.20-10.40
device,testdev01,1,,
poll,holding_register,2,2,BE_BE
ref,Pressure_of_sensor1,2,float32,r
  • up to 4 registers for versions Class.Group-version 5.20-10.40 and later
device,testdev01,1,,
poll,holding_register,2,4,BE_BE
ref,Pressure_of_sensor1,2,float32,r
ref,Pressure_of_sensor2,4,float32,r

Upvotes: 1

Related Questions