Uncategorized

raspberry pi – 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()



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *