import serial import time import math # def readtemp(): # command write to read two bytes from tmp1075, into internal memory x = b'\x90\xeb\x02\x02\x06\x00\x48\x00\x06\x00' #command to read chip ser.write(x) time.sleep(0.1) x = b'\x90\xeb\x01\x00\x08\x00' #command to read data from registers ser.write(x) time.sleep(0.1) # def readtime(): # command write to read first eight bytes of 3231 into internal registers x = b'\x90\xeb\x02\x02\x0e\x00\x68\x00\x0e\x00' #command to read chip ser.write(x) time.sleep(0.1) x = b'\x90\xeb\x04\x00\x10\x00' #command to read data from registers ser.write(x) time.sleep(0.1) # def writetime(): # write discrete time set in string x = b'\x90\xeb\x06\x02\x0e\x00\x68\x00\x1d\x00' #header to write x = x + b'\x00\x30\x20\x01\x03\x03\x24\x00' #time:20:30, sunday, march 3, 24 ser.write(x) time.sleep(0.1) # # configure the serial connections ser = serial.Serial() ser.baudrate = 115200 ser.port = 'COM8' ser.open() # print(ser.name) # check which port was really used print(ser.is_open) # #writetime() # while True: y = bytearray(14) readtime() y = ser.read(14) time_sec = (y[6]&0x0f) + ((y[6]>>4) * 10) time_min = (y[7]&0x0f) + ((y[7]>>4) * 10) time_hour = (y[8]&0x0f) + ((y[8]>>4) * 10) print ('Time:',time_hour,':',time_min,':',time_sec) readtemp() y = ser.read(8) temp = y[6] + ((y[7]>>4)/16) if temp >= 128: #handle negative temperatures temp = temp-256 temp = temp*1.8+32 #convert to fahrenheit print ('Temp:',temp) time.sleep(1) # ser.close()