-
Notifications
You must be signed in to change notification settings - Fork 21
/
piptest.py
124 lines (106 loc) · 3.73 KB
/
piptest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
# *****BatteryMonitor Getdata from battery cells getdata.py*****
# Copyright (C) 2017 Simon Richard Matthews
# Project loaction https://github.com/simat/BatteryMonitor
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
import serial
import binascii
from re import match
from time import sleep
#commands={'QPIGS':110,'Q':74,'QFLAG':15,'QPIRI':102,'PE':7,'PD':7,'PBCV':7, \
# 'PCVV':7,'PBFT':7,'PCVT':7}
#commands={}
def openpip(port):
openport = serial.Serial(port,baudrate=2400,timeout=2.0) # open serial port
return openport
def sendcmd(command,port='/dev/ttyUSB1'):
"""send command/query to Pip4048, return reply"""
# try:
# x=match("[A-Z,a-z]*",command)
# replylen=commands[x.group()]
# except KeyError:
# replylen=int(input("Enter Reply Length>"))
try:
cmd=command.encode('ascii','strict')
crc=crccalc(cmd)
cmd=cmd+crc.to_bytes(2, byteorder='big')+b'\r'
openport=openpip(port)
openport.reset_input_buffer()
openport.write(cmd)
# reply = openport.read(replylen)
reply=openport.readline()
print ('chars in buf {}'.format(openport.in_waiting))
if crccalc(reply[0:-3]) != int.from_bytes(reply[-3:-1],byteorder='big'):
raise IOError("CRC error in Pip4048 return string")
except IOError as err:
print(err.args)
finally:
openport.close()
print (len(reply),reply)
print(binascii.hexlify(reply))
if command=='QPIGS':
print ('AC output V =',reply[12:17])
print ('AC output VA =',reply[23:27])
print ('AC output W =',reply[28:32])
print ('Bus V =',reply[37:40])
print ('Battery V =',reply[41:46])
print ('Battery In I =',reply[47:50])
print ('Heat sink T =',reply[51:54])
print ('PV Input current for battery I =',reply[60:64])
print ('PV Input V =',reply[65:70])
print ('Battery V from SCC =',reply[71:76])
print ('Battery Discharge I =',reply[77:82])
print ('Status =',reply[83:91])
# (230.0 21.7 230.0 50.0 21.7 5000 4000 48.0 49.0 45.0 51.6 50.2 2 30 030 0 1 3 9 01 0 0 48.0 0 1 000\x19Q\r'
elif command=='QPIRI':
print ('Recharge V =',reply[43:47])
print ('LVD =',reply[48:52])
print ('Bulk V =',reply[53:57])
print ('Float V =',reply[58:62])
def setparam(command,port):
reply=sendcmd(command,port)
print (reply)
if reply[1:4]!=b'ACK':
raise IOError('Bad Parameters')
def getcmd():
"""gets command from user"""
command=str(input("Enter Command>"))
return command
def loop():
while True:
main()
def main(port='/dev/ttyUSB1'):
command=getcmd()
sendcmd(command,port)
def crccalc(command):
"""returns crc as integer from binary string command"""
crc=binascii.crc_hqx(command,0)
crchi=crc>>8
crclo=crc&255
if crchi == 0x28 or crchi==0x0d or crchi==0x0a:
crc+=256
if crclo == 0x28 or crclo==0x0d or crclo==0x0a:
crc+=1
return crc
if __name__ == "__main__":
"""if run from command line, piptest [command] [port]
default port /dev/ttyUSB1, if no command ask user"""
print (sys.argv)
if len(sys.argv) == 2:
sendcmd(sys.argv[1])
else:
if len(sys.argv) == 3:
sendcmd(sys.argv[1],sys.argv[2])
else:
loop()