I've been trying to build a simple temperature measuring system using Minimalmodbus. The components are as follows;
- RPi(Raspberry pi zero) with python ver. 3.7.3
- USB to RS-485 adapter
- 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 °