我一直在尝试使用 Minimalmodbus 构建一个简单的温度测量系统。组件如下;
- RPi(树莓派零)与 python 版本。3.7.3
- USB 转 RS-485 适配器
- 温湿度传感器:XY-MD02(SHT20)
Modbus RTU库(Minimalmodbus)很好地安装在RPi上,可以通过Arduino Modbus库(smarmengol)与Arduino Uno成功通信 连接PC(windows10)时,使用modbus主模拟软件“Modbus Poll”确认传感器工作)。但是当这个传感器通过 USB 到 RS-485 适配器连接到 RPi 时,我无法获得任何数据。我看不出它的电路有什么问题。当我在调试模式下运行它以读取寄存器时,这表明读取查询已按照其手册所述正确释放。错误消息只是说“无响应错误”。甚至任何评论都将不胜感激。请参考python代码和调试&错误信息如下;
#!/usr/bin/env python3
import minimalmodbus
import serial
import time
instr = minimalmodbus.Instrument('/dev/ttyUSB0',1, debug=True)
instr.serial.baudrate = 9600
instr.serial.timeout = 2
while True :
result = instr.read_register(registeraddress=1,functioncode=4)
print (result)
time.sleep(3)
<调试和错误信息>
MinimalModbus debug mode. Create serial port /dev/ttyUSB0
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back) : 01 04 00 01 00 01 60 0A (8 bytes)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. No sleep required before write. Time since previous read: 3916373.5ms, minimum silent period: 4.01ms
MinimalModbus debug mode. Response from instrument: (0 bytes), roundtrip time: 1.0 ms. Timeout for reading : 0.0 ms.
Traceback (most recent call last) :
File "minModTest.py", line 15, in <module>
result = instr.read_register(registeraddress=1,functioncode=4)
File "/usr/local/lib/python3.7/dist-packages/minimalmodbus.py", line 486, in read_register
payloadformat=_Payloadformat.REGISTER,
File "/usr/local/lib/python3.7/dist-packages/minimalmodbus.py", line 1245, in _generic_command
payload_from_slave = self._perform_command(functioncode, payload_to_slave)
File "/usr/local/lib/python3.7/dist-packages/minimalmodbus.py", line 1322, in _perform_command
response_bytes = self._communicate(request_bytes, number_of_bytes_to_read)
File "/usr/local/lib/python3.7/dist-packages/minimalmodbus.py", line 1490, in _communicate
raise NoResponseError("No communicate with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
*** Manual ***
<Query>
Slave Add. Function Code Starting Add H Starting Add L Q’ty H Q’ty L CRC H CRC L
0x01 0x04 0x00 0x01 0x00 0x01 0x60 0x0a
<Response>
Slave Add. FunctionCode BytesNumber TemperatureH TemperatureL CRC H CRC L
0x01 0x04 0x02 0x01 0x31 0x79 0x74
Temperature value=0x131, converted to a decimal 305, the actual temperature value = 305 / 10 = 30.5°
Note: the temperature is signed hexadecimal number, temperature value = 0xFF33, converted to a decimal - 205, the actual temperature = -20.5 °