简体   繁体   中英

Minimalmodbus RTU connection problem of Raspberry Pi with SHT20 sensor(XY MD02)

I've been trying to build a simple temperature measuring system using Minimalmodbus. The components are as follows;

  1. RPi(Raspberry pi zero) with python ver. 3.7.3
  2. USB to RS-485 adapter
  3. Temperature & Humidity Sensor : XY-MD02(SHT20)

Modbus RTU library(Minimalmodbus) was nicely installed at RPi, which can successfully communicates with Arduino Uno with Arduino Modbus library (smarmengol) The sensor was also confirmed to work by using a modbus master simulation software "Modbus Poll" when connected to PC(windows10). But I can't get any data when this sensor got connected to RPi via USB to RS-485 adaptor. I don't see anything wrong with its circuit. When I run it in debug mode to read a register, which shows the reading query is correctly released as its manual says. Error message just says "No response Error". Even any comments would be MUCH appreciated. Pls refer to python code and debugging & error messages as follows;

#!/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>

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 °

Thanks for your comment. Actually 4 wires are connected between USBtoRs485_adapter(RPi) and the sensor; +5V, GND, A and B. As I wrote, these adapter and sensor are working well with PC(windows), but not with RPi. Wiring would not be a problem, because the same wiring was working with PC. Do I need to do something with RPi?

Did you find the reason to the problem ? Here is your code tested on my RPi. It works.

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

MinimalModbus debug mode. Create serial port /dev/ttyUSB0 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 on logic analyzer

here is the adapter I use for my project. Good luck :) usb to rs485 adapter

here is the adapter I use for my project. Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM