#!/usr/bin/python3 import serial import time import sys from time import sleep from datetime import datetime #-------------------------------------------------------- def werte5(tx): x1 = 346 # Gesamt Bezug hx1 = tx[x1:x1+6] hs = '0x' + hx1 xx = int(hs, 0) print('Gesamtbezug (kWh) : {} '.format(xx/10000) ) #....................................................... x1 = 240 # Gesamt Einspeisung hx1 = tx[x1:x1+6] hs = '0x' + hx1 xx = int(hs, 0) print('Einspeisung (kWh) : {} '.format(xx/10000) ) #.......................................................... x1 = 434 # aktuelle Leistung hx1 = tx[x1:x1+4] #print(hx1) hs = '0x' + hx1 xx = int(hs, 0) print('Leistung (W) : {} '.format(xx) ) #....................................................... x1 = 642 # Spannung 642 hx1 = tx[x1:x1+4] hs = '0x' + hx1 xx = int(hs, 0) print('Spannug (V) : {} '.format(xx/10) ) print('------------------------------------ ') """ #....................................................... #x1 = 240 ???? # Strom #hx1 = tx[x1:x1+6] #hs = '0x' + hx1 #xx = int(hs, 0) print('Strom (A) : {} '.format(xx/1000) ) """ ser = serial.Serial( port='/dev/ttyS0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) try: # Start-Sequenz suchen start_sequence = b'\x1b\x1b\x1b\x1b\x01\x01\x01\x01' while True: if ser.read(1) == b'\x1b': data = ser.read(7) if data == start_sequence[1:]: break data = ser.read(800) ## evtl. weniger als 800 Bytes ! hexx = '1b1b1b1b01010101' + data.hex() ser.close() #print(hexx) #------------------ Daten aufbereiten und anzeigen werte5(hexx) #now = datetime.now() #de = now.strftime("%m/%d/%Y, %H:%M:%S") #stu = time.strftime('%H',time.localtime()) #mi = time.strftime('%M',time.localtime()) #sc = time.strftime('%S',time.localtime()) #stu2 = int(stu) #mi2 = int(mi) #sc2 = int(sc) #g = (stu2*100) + mi2 #g = (mi2*100) +sc2 except serial.SerialException as e: print(f"Serieller Port-Fehler: {e}") except KeyboardInterrupt: print("Skript durch Benutzer beendet") except Exception as e: print(f"Ein Fehler ist aufgetreten: {e}")