I am new to MODBUS-RTU protocol. I have Raspberry Pi4 connected to a Environmental Sensor through USB 2.0 to RS485 Serial Converter Adapter Cable. My real struggle lies in receiving the data from the sensor. I have used the below code for fetching the data from the sensor using the serial port:
import serial
from time import sleep
ser = serial.Serial ("/dev/ttyUSB0", 9600) #Open port with baud rate
while True:
print(ser)
received_data = ser.read() #read serial port
print(received_data)
sleep(0.03)
data_left = ser.inWaiting() #check for remaining byte
received_data += ser.read(data_left)
print (received_data) #print received data
When i execute the above code i receive empty data. Kindly let me know how i can extract the data from the MODBUS-RTU device using USB 2.0 to RS485 Serial Converter Adapter Cable into pi?
The data sheet provides the following information.
Communication protocol
1.Standard MODBUS RTU protocol
1.Physical port definition
Baud rate——9600bps ,Data bits——8bits,check bit——NO(NO check ),Stop bit——1bit
I also want to know,whether i need to add any device in between so that it becomes easy for me to extract the data into pi using the python script.
Any other code which helps me in getting the data is much appriciated.
UPDATE: I have tried PyModbus. The code is given below.
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
from pymodbus.constants import Endian
from pymodbus.constants import Defaults
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer
Defaults.RetryOnEmpty = True
# set Modbus defaults
Defaults.Timeout = 5
Defaults.UnitId = 1
Defaults.Retries = 5
client = ModbusClient(method='rtu',
port='/dev/ttyUSB1',
stopbits=1,
bytesize=8,
timeout=3,
baudrate=9600,
parity='N')
connection = client.connect()
print ("Readout started")
#result = client.read_discrete_inputs(0)
result = client.read_holding_registers(3,4,unit=1)
#result = client.read_input_registers(0,1)
print(result)
But i get Modbus Error.
Readout started
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x0 0x3 0x0 0x4 0xb4 0x9
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x1 0x3 0x0 0x8 0x0 0x8 0x1 0x1a 0x2 0x2a 0x23 0x7a 0x98
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x1 0x3 0x0 0x8 0x0 0x8 0x1 0x1a 0x2 0x2a 0x23 0x7a 0x98
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Modbus Error: [Input/Output] No Response received from the remote unit/Unable to decode response.
Can anybody help in correcting the above error.
Also i have tried changing the parity bit to E and extended timeout but i still get the same error.
Update-1:
Details of the sensor:
Model: BGT-W812 pro
Parameter:
1.Power supply :DC12V(recommand)
2.Maximum power consumption: Without heating:130mA/12V;heating:700mA/12V
3.Power type: 220V AC 、12V DC
4.Communication protocol:MODBUS RTU protocol
5.Output:RS485
Navigation in defining:
1:+12V (RED)
2:GND (BLUE)
3:RS485_A(YELLOW)
4:RS485_B (GREEN)
Product link:
[https://www.alibaba.com/product-detail/BGT-Temperature-Humidity-Air-Pressure-Wind_1600425200036.html][1]
Hardware Connection:
Black and Red Wires are connected to 12VDC.
The other side of the USB-RS485 converter is connected to the USB port of PI.
Update 2: Manual data