cm5024z/combined.py

288 lines
7.6 KiB
Python
Raw Normal View History

2021-09-12 13:56:27 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import serial, sys
import os
from datetime import datetime
from bitstring import BitArray
serial_timeout=0.1
byte3=0
byte4=0
byte5=0
byte6=0
i=0
pvfaultdesc = [0]*8
pvfaultdesc[0]="load short-circuit protection"
pvfaultdesc[1]="load overcurrent protection"
pvfaultdesc[2]="battery low-voltage protection"
pvfaultdesc[3]="reserved"
pvfaultdesc[4]="solar panel array1 open-circuit breakdown(it means the controller does not detect the solar panel voltage within 24h)"
pvfaultdesc[5]="solar panel array1 over-voltage protection"
pvfaultdesc[6]="delayed protection phase for load overcurrent"
pvfaultdesc[7]="shut the load compulsively"
def pvstatusbit(bit):
if bit == '1':
print("gesetzt :%s" % bit)
else:
print("nicht gesetzt :%s" % bit)
def pvstatus():
# current status of system
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x61\x00\x00\x00\x00\xBD"
global byte3
global byte4
del byte3
del byte4
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
ser.close()
if (
len(byte3) > 0 and len(byte4) > 0
):
byte3=byte3.encode('hex')
bitstring1=BitArray(hex=byte3)
# sys.stdout.write('Inputstring:\t')
# sys.stdout.write(bitstring1.bin)
# sys.stdout.write('\n')
pvfaultstate(bitstring1.bin)
sys.stdout.write('\n')
else :
sys.stdout.write('Voltage communication error!')
def pvfaultstate(bytearray):
global pvfaultdesc
# print("Inputstring Funktion:\t %s" %bytearray)
i=0
arraylen=len(bytearray)
# print (" Array ist %s Elemente lang" %(arraylen))
while i <=arraylen-1:
if bytearray[i] =='1':
print("Fehler aktiv: %s (Byte: %s)" % (pvfaultdesc[i], i) )
#pvstatusbit(bytearray[i])
if i<=arraylen: i=i+1
# last anschalten
def pvswitch(state):
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
if state == 0:
command = "\x01\x55\x00\x00\x00\x00\x26" #last aus
print ("demanding controller removing closing the load compulsively")
else:
command = "\x01\x54\x00\x00\x00\x00\xF5" #last an
print ("demanding controller closing the loads compulsively")
ser.write(command)
ser.close()
def get_PV_Ah():
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x05\x00\x00\x00\x00\x9B" # the back transmission data from the controlleris the accumulative generating power data of solar panel array1(Ah, DC/DC output), and the data are the long data shaping with symbol, and the accumulative Ah scaling is 1Ah/LSB.
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
byte5=ser.read(1)
byte6=ser.read(1)
ser.close()
print ("PV: \t\t\t %s Ah" % (BitArray(hex=byte6.encode('hex'))+BitArray(hex=byte5.encode('hex'))+BitArray(hex=byte4.encode('hex'))+BitArray(hex=byte3.encode('hex'))).uint)
def get_load_Ah():
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x16\x00\x00\x00\x00\xA3" # the back transmission data from the controlleris the accumulative generating power data of solar panel array1(Ah, DC/DC output), and the data are the long data shaping with symbol, and the accumulative Ah scaling is 1Ah/LSB.
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
byte5=ser.read(1)
byte6=ser.read(1)
ser.close()
print ("Load: \t\t\t %s Ah" % (BitArray(hex=byte6.encode('hex'))+BitArray(hex=byte5.encode('hex'))+BitArray(hex=byte4.encode('hex'))+BitArray(hex=byte3.encode('hex'))).uint)
while True:
try:
now = datetime.now()
os.system('clear')
print(now)
# pvswitch(1)
# battery voltage
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x10\x00\x00\x00\x00\x2B"
del byte3
del byte4
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
ser.close()
if (
len(byte3) > 0 and len(byte4) > 0
):
value1=ord(byte3)
value2=ord(byte4)
voltage=(value2 * 256 + value1) / 100
voltage_modulo=(value2 * 256 + value1) % 100
sys.stdout.write('BATTERY:\t\t')
sys.stdout.write(str(voltage))
sys.stdout.write('.')
sys.stdout.write(str(voltage_modulo))
sys.stdout.write(' V\n')
else :
sys.stdout.write('Voltage communication error!')
# pv voltage/current
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x01\x00\x00\x00\x00\x84"
del byte3
del byte4
del byte5
del byte6
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
byte5=ser.read(1)
byte6=ser.read(1)
ser.close()
value1=ord(byte3)
value2=ord(byte4)
value3=ord(byte5)
value4=ord(byte6)
voltage=(value2 * 256 + value1) / 100
voltage_modulo=(value2 * 256 + value1) % 100
current=(value4 *256 + value3) / 100
current_modulo=(value4 * 256 + value3) % 100
sys.stdout.write('PV INPUT:\t\t')
sys.stdout.write(str(voltage))
sys.stdout.write('.')
sys.stdout.write(str(voltage_modulo))
sys.stdout.write(' V')
sys.stdout.write('\t')
sys.stdout.write(str(current))
sys.stdout.write('.')
sys.stdout.write(str(current_modulo))
sys.stdout.write(' A \n')
# current load
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x14\x00\x00\x00\x00\x34"
del byte3
del byte4
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
ser.close()
value1=ord(byte3)
value2=ord(byte4)
current=(value2 * 256 + value1) / 100
current_modulo=(value2 * 256 + value1) % 100
sys.stdout.write('LOAD:\t\t\t\t')
sys.stdout.write(str(current))
sys.stdout.write('.')
sys.stdout.write(str(current_modulo))
sys.stdout.write(' A\n')
# temperature & battery capacity
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=serial_timeout)
command = "\x01\x11\x00\x00\x00\x00\xF8"
del byte3
del byte4
del byte5
del byte6
ser.flushInput() #clear buffer
ser.write(command) #send prepared command
skip=ser.read(2) #skip first two bytes
byte3=ser.read(1)
byte4=ser.read(1)
byte5=ser.read(1)
byte6=ser.read(1)
ser.close()
value1=ord(byte3)
value2=ord(byte4)
value3=ord(byte5)
value4=ord(byte6)
temperature=(value2 * 256 + value1)
capacity=(value4 * 256 + value3)
sys.stdout.write('Temperature:\t\t\t\t')
sys.stdout.write(str(temperature))
sys.stdout.write(' °C\n')
sys.stdout.write('Battery Capacity:\t\t\t')
sys.stdout.write(str(capacity))
sys.stdout.write(' %\n')
print("\n\naktive Fehler:")
pvstatus()
get_PV_Ah()
get_load_Ah()
# sleep 10 seconds before starting over
time.sleep(10)
except KeyboardInterrupt:
print("\n\nkilled via CTRL+C - Bye")
#pvswitch(0) #last ausschalten
sys.exit()