首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >树莓Pi与SHT20传感器的最小RTU连接问题(XY MD02)

树莓Pi与SHT20传感器的最小RTU连接问题(XY MD02)
EN

Stack Overflow用户
提问于 2021-11-12 07:02:11
回答 3查看 604关注 0票数 0

我一直在尝试用Minimalmodbus建立一个简单的测温系统。各组成部分如下;

  1. RPi(Raspberry pi 0)与python。3.7.3
  2. USB到RS-485适配器
  3. 温湿度传感器: XY-MD02(SHT20)

RPi上很好地安装了Modbus库(Minimalmodbus),它可以成功地与Arduino Uno和Arduino Modbus库(smarmengol)通信。当与PC(windows10)连接时,还可以使用modbus主仿真软件"Modbus Poll“验证传感器的工作能力。但是当这个传感器通过USB连接到RPi到RS-485适配器时,我无法获得任何数据。我看不出它的电路有什么问题。当我在调试模式下运行它来读取寄存器时,它显示读取查询按其手册所示正确释放。错误信息只是说“没有响应错误”。即使是任何评论都将是非常感谢的。请参阅python代码和调试和错误消息如下;

代码语言:javascript
复制
#!/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)

< Debuggin & Error Message>

代码语言:javascript
复制
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)
代码语言:javascript
复制
*** 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 °
EN

回答 3

Stack Overflow用户

发布于 2021-11-13 00:05:33

感谢您的评论。实际上,USBtoRs485_adapter( RPi )和传感器之间有4条线连接;+5V、GND、A和B。正如我所写的,这些适配器和传感器在PC(windows)上工作良好,但与RPi不兼容。配线不会是一个问题,因为同样的布线是与PC一起工作的。我需要用RPi做点什么吗?

票数 0
EN

Stack Overflow用户

发布于 2021-11-25 16:38:52

你找到问题的原因了吗?下面是在我的RPi上测试的代码。它起作用了。

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

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2021-12-06 08:59:36

下面是用于我的项目的适配器。祝你好运:) usb到rs485适配器

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69939068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档