0

我一直在尝试使用 Minimalmodbus 构建一个简单的温度测量系统。组件如下;

  1. RPi(树莓派零)与 python 版本。3.7.3
  2. USB 转 RS-485 适配器
  3. 温湿度传感器: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 °
4

3 回答 3

0

你找到问题的原因了吗?这是您在我的 RPi 上测试的代码。有用。

pi@raspberrypi:~/pythonDev/modbus $ python3 modbus01.py

MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x04\x00\x01\x00\x01`\n' (01 04 00 01 00 01 60 0A)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. No sleep required before write. Time since previous read: 1862540.46 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '\x01\x04\x02\x00Æ9b' (01 04 02 00 C6 39 62) (7 bytes), roundtrip time: 22.1 ms. Timeout for reading: 2000.0 ms.

198
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x04\x00\x01\x00\x01`\n' (01 04 00 01 00 01 60 0A)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. No sleep required before write. Time since previous read: 3023.28 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '\x01\x04\x02\x00Æ9b' (01 04 02 00 C6 39 62) (7 bytes), roundtrip time: 22.5 ms. Timeout for reading: 2000.0 ms.

198
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x04\x00\x01\x00\x01`\n' (01 04 00 01 00 01 60 0A)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. No sleep required before write. Time since previous read: 3026.49 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '\x01\x04\x02\x00Æ9b' (01 04 02 00 C6 39 62) (7 bytes), roundtrip time: 22.6 ms. Timeout for reading: 2000.0 ms.

198
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x04\x00\x01\x00\x01`\n' (01 04 00 01 00 01 60 0A)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. No sleep required before write. Time since previous read: 3027.09 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '\x01\x04\x02\x00Æ9b' (01 04 02 00 C6 39 62) (7 bytes), roundtrip time: 22.7 ms. Timeout for reading: 2000.0 ms.

逻辑分析仪上的 modbus RS485

逻辑分析仪上的 modbus RS485

于 2021-11-25T16:38:52.643 回答
0

谢谢你的评论。实际上,USBtoRs485_adapter(RPi) 和传感器之间连接了 4 根线;+5V、GND、A 和 B。正如我所写,这些适配器和传感器在 PC(Windows)上运行良好,但与 RPi 不兼容。接线不会有问题,因为相同的接线可用于 PC。我需要对 RPi 做些什么吗?

于 2021-11-13T00:05:33.967 回答
0

这是我用于项目的适配器。祝你好运:) USB 到 rs485 适配器

这是我用于项目的适配器。祝你好运 :)

于 2021-12-06T08:59:36.530 回答