#! /usr/bin/python # -*- coding: utf-8 -*- """ Created on Mo 16 15:28:31 2019 Modul pySerial + numpy have to be installed python -m pip install pyserial numpy Edit File until DEBUG variable and start script via python Logger_TC309.py @author: tetzlav@web.de """ # importieren modules import serial, time, os, numpy, signal, sys # Output filename #OUTFILE = "TC309_Temperaturen_" + time.strftime("%d-%m-%Y_%H-%M-%S") + ".csv" OUTFILE = "TC309_Temperaturen_" + time.strftime("%d-%m-%Y") + ".csv" # row seperator in output file ROWSEP = "," HEAD = ["Datum/mytime", "T1/°C", "T2/°C", "T3/°C", "T4/°C"] # should be data append if outfile exits APPEND = True # newline character NL = "\n" # number of reads, 0 means endless loop REPEAT = 0 # linux serial device file or windows COMX port PORT = 'COM15' # reading interval in seconds INTERVAL = 3 # enable debugging output DEBUG = False # catch CTRL + C for clean exit def signal_handler(signal, frame): print("Catch [CTRL] + [C] .\nWrote output to \"%s\"." %OUTFILE) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) # open serial port Serial = serial.Serial(PORT, timeout=2) # open outfile os.system('cls') if APPEND and os.path.exists(OUTFILE): dat = open(OUTFILE, 'a') print("Output will be append to \"%s\".\n[CTRL] + [C] will stop logging.\n" %OUTFILE) else: dat = open(OUTFILE, 'w') dat.write(ROWSEP.join(HEAD)+NL) print("Output will be wrote to new file \"%s\".\n[CTRL] + [C] will stop logging.\n" %OUTFILE) x = 0 if not REPEAT: last = 1 else: last = REPEAT while x < last: if REPEAT: x += 1 mytime = time.time() mytimesformat = time.strftime("%d.%m.%Y %H:%M:%S") Serial.write(b"A") data = bytearray(Serial.read(45)) if len(data) < 45: print ("no or faulty data!") print ("(answer was %i byte long.)" % len(data)) break databits = numpy.unpackbits(data) if DEBUG: print("---") i = 1 n = 0 print('Byte#: {0:>2d} Data: {1:>3d} Bits: '.format(n + 1, data[n]), end = '') for b in databits: if i % 8 == 0: print(b) #print(n) #print(len(data)) if n < (len(data) - 1): n += 1 print('Byte#: {0:>2d} Data: {1:>3d} Bits: '.format(n + 1, data[n]), end = '') else: print(b, end = ' ') i += 1 print("---") # decode data """ positive temperatures: if data(byte1) < 240: (data[byte1]*256 + data[byte1 + 1])*0.1 negative temperatures: if data(byte1) >= 240: ((data[byte1]-255)*256 + (data[byte1 + 1]-256))*0.1 T1 Byte 8 + 9, OL = Byte 4 Bit 1 (Bit 32 of all 45 Byte) T2 Byte 10 + 11, OL = Byte 5 Bit 1 (Bit 40 of all 45 Byte) T3 Byte 12 + 13, OL = Byte 6 Bit 1 (Bit 48 of all 45 Byte) T2 Byte 14 + 15, OL = Byte 7 Bit 1 (Bit 56 of all 45 Byte) """ # set default values temps = [9999.9, 9999.9, 9999.9, 9999.9] # start byte 8 (- 1) byte = 7 # start temperatur 1 (- 1) temp = 0 # Bit [32, 40, 48, 56] (- 1) check temp channel is not OL and read data for bit in range(31, 56, 8): if DEBUG: print("T{0:1d} Bit: {1:2d} Byte: {2:2d}\n---".format(temp + 1, bit, byte + 1, )) if databits[bit] == 1: if data[byte] < 240: temps[temp] = (data[byte] * 256 + data[byte + 1]) * 0.1 else: temps[temp] = ((data[byte] - 255) * 256 + (data[byte + 1] - 256)) * 0.1 temp += 1 byte += 2 # console log print ("[{0}] T1:{1:>7.1f} °C T2: {2:>7.1f} °C T3: {3:>7.1f} °C T4: {4:>7.1f} °C".format(time.strftime("%d.%m.%Y %H:%M:%S"), temps[0], temps[1], temps[2], temps[3])) # fiel log line = [mytimesformat, "{T:4.1f}".format(T=temps[0]), "{T:4.1f}".format(T=temps[1]), "{T:4.1f}".format(T=temps[2]), "{T:4.1f}".format(T=temps[3])] dat.write(ROWSEP.join(line)+NL) dat.flush() time.sleep(INTERVAL - (time.time() - mytime)) dat.close()