#!/usr/bin/python
import serial
import sys
import json
import time
import string
ser = serial.Serial()
ser.port = "/dev/ttySI2"
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = none #block read
ser.timeout = 2 #non-block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 5 #timeout for write
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput() #flush output buffer, aborting current output and discard all that is in buffer
out=dict()
command = '\xAA\x01\x01\x01\x00\x03\x00\x00\x1E\x32'
ser.write(command)
response = ser.read(100)
if sys.getsizeof(response) < 36 :
ser.write(command)
response = ser.read(100)
if int(response[8].encode('hex'),16) > 0 :
watts=int(float((256*ord(response[13])+ord(response[12])))/10*float((256*ord(response[15])+ord(response[14])))/10)
#watthour below is based on a 15 second period. If your refresh is higher then update it.
watthour=round(float(watts)/(3600/15),3)
else:
watts=0
watthour=0
out["Inverter2_mode"]=int(response[8].encode('hex'),16)
out["pv2_input_voltage"]=float((256*ord(response[11])+ord(response[10])))/10
out["pv2_battery_voltage"]=float((256*ord(response[13])+ord(response[12])))/10
out["pv2_input_current_for_battery"]=float((256*ord(response[15])+ord(response[14])))/10
out["pv2_input_watts"]=watts
out["pv2_input_watthour"]=watthour
out["pv2_heatsink_temperature"]=int(response[28].encode('hex'),16)
out["pv2_batt_temperature"]=int(response[26].encode('hex'),16)
print(json.dumps(out))
ser.close()
except Exception, e:
print "error reading inverter...: " + str(e)
exit()