import serial import struct from base64 import encode # open serial port #ser = serial.Serial('/dev/ttyS2', 9600, timeout=1) ser = serial.Serial( '/dev/ttyS2', baudrate=9600, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE, parity=serial.PARITY_NONE ) # configure RS485 settings command = [1, 4, 0, 1, 0, 2, 32, 11] data = bytearray(command) # ser.setRTS(False) ser.write(data) #ser.setDTR(True) #ser.write(b'\x01\x04\x00\x01\x00\x02\x20\x0B') ser.flush() # ser.setRTS(True) #ser.setDTR(False) # read response response = ser.read(9) # temperature = response[3] + response[4] # humidity = response[5] + response[6] # (temp,) = struct.unpack(">h", temperature) # print("temperature " + str(temp)) # (humi,) = struct.unpack(">h", humidity) # print("humidity " + str(humi)) print("Temp and Humid Read.") # print response print(response) # close serial port ser.close()