modbus - Can't connect properly to SHT20 RS485 sensor using QModBus or pySerial - Stack Overflow

I've been trying to connect to the RS485 sensor through various methods for a while now, but it do

I've been trying to connect to the RS485 sensor through various methods for a while now, but it doesn't seem to respond to any of the commands I send.

I've used this Python script and several serial port management programs to transmit commands, but I always receive garbage data. The sensor is constantly flashing a red light. The cables I'm using have continuity, and I'm supplying 5V DC for power as specified in the user manual.

import serial 
import time


ser = serial.Serial( 
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_EVEN,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=3)

def readtempandhum():
    print ("reading temperature-humidity") 
    x= b'\x01\x04\x00\x01\x00\x02\x20\x0B' 
    for i in x:
        envio=chr(i) 
        ser.write(envio.encode()) 

ser.isOpen() 
readtempandhum() 

while True:
    line=ser.readline() 
    if len(line)>0: 
        print(line)
        try:
            temp=float((line[3]*256+line[4])/100) #Data conversion for Temperature
            hum=float((line[5]*256+line[6])/100) #Data conversion for humidity
            print (str(temp)+' '+str(hum))
        except Exception as e:
            print("read error "+str(e))
        ser.flush() #
        readtempandhum() 
    time.sleep(2) #repeat proces each 2 seconds

Here's a link to the manual i'm using for reference.

.pdf

I've tried using two separate sensors of the same type, but both show the same errors. When using QModBus, I consistently receive a similar hex code to the one shown below as a response from the slave that doen't match the expected format. I'm entering the commands exactly as specified in the manual and video tutorials, but none of them seem to work.

01 81 01 81 90

What could be causing these persistent issues?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744301417a4567520.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信