使用ModBut-RTU启用环境传感器中的Python提取数据
我是Modbus-RTU协议的新手。我的Raspberry PI4通过USB 2.0连接到环境传感器,连接到RS485串行转换器适配器电缆。我真正的斗争在于接收传感器的数据。我已经使用以下代码使用串行端口从传感器中获取数据:
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
执行上述代码时,我会收到空数据。请让我知道如何使用USB 2.0到RS485串行转换器适配器电缆从Modbus-RTU设备中提取数据?
数据表提供以下信息。
通信协议
1.标准modbus rtu协议
1.物理端口定义
波特率 - -9600bps,数据位 - 8位,检查位 - no(无检查),no(无检查),停止位 - - 1位
我也想要要知道,我是否需要在两者之间添加任何设备,以便我很容易使用Python脚本将数据提取到PI中。
任何有助于我获取数据的代码都大大化了。
更新: 我尝试了 pymodbus 。代码在下面给出。
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)
但是我遇到了modbus错误。
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.
任何人都可以帮助纠正上述错误。
另外,我已经尝试将奇偶校验位更改为e和延长超时,但我仍然会遇到相同的错误。
Update-1:
传感器的详细信息:
模型: BGT-W812 Pro
参数:
1.功率供应: DC12V(推荐)
2。最大功耗:不加热:130mA/12v;加热:700mA/12V 3.
功率类型:220V AC 、12V DC
4.频协议:MODBUS RTU协议
5. OUTPUTS 5.OUTPUT 5. OUTPUT:RS485
in In in Strong 定义:
1:+12V(红色)
2:gnd(蓝色)
3:rs485_a(黄色)
4:rs485_b(green)
产品链接链接:
[https://www.alibaba.com/product-detail/BGT-Temperature-Humidity-Air-Pressure-Wind_1600425200036.html][1]
硬件连接:
黑色和红线连接到12VDC。
USB-RS485转换器的另一侧连接到PI的USB端口。
更新2:手动数据
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论