forked from Interster/PylonTechBattery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyloncom.py
executable file
·112 lines (86 loc) · 3.16 KB
/
pyloncom.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
import sys
import pylonpacket
import logging
import serial
import time
class PylonCom:
PORT = "/dev/ttyUSB0"
BAUD = 115200
def __init__(self):
self.sp = serial.Serial(PylonCom.PORT,PylonCom.BAUD,timeout=0.5)
def GetReply(self, request, reply_type):
self.sp.write(request.GetAsciiBytes())
line = bytearray()
while True:
c = self.sp.read(1)
if c:
line.extend(c)
if c[0] == 0x0D:
break
else:
break
logging.debug("Received sentence %s",line)
preply = pylonpacket.PylonPacket.Parse(line, reply_type)
return preply
def close(self):
self.sp.close()
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
pc = PylonCom()
#for adr in range(0,255):
# ppIn = pylonpacket.PPGetVersionInfo()
# ppIn.ADR=adr
# ppOut=pc.GetReply(ppIn, pylonpacket.PPVersionInfo)
# if ppOut: print("Get protocol version reply:",ppOut)
#return
#ppIn=pylonpacket.PPGetManufacturerInfo()
#print("Get manufacturer info:",ppIn)
#ppOut=pc.GetReply(ppIn, pylonpacket.PPManufacturerInfo)
#print("Get manufacturer info reply:",ppOut)
ppIn = pylonpacket.PPGetSystemParameter()
print("Get system parameter:",ppIn)
ppOut = pc.GetReply(ppIn, pylonpacket.PPSystemParameter)
print("Get system parameter reply:",ppOut)
ppIn = pylonpacket.PPGetSeriesNumber()
ppIn.Command = 0x02
print("Get series number:",ppIn)
ppOut = pc.GetReply(ppIn, pylonpacket.PPSeriesNumber)
print("Get series number reply:",ppOut) #,ppOut.info.hex())
while True:
for adr in range(2,3):
print("Conectiong to addr",adr)
ppIn = pylonpacket.PPGetChargeManagementInformation()
ppIn.Command = adr
ppIn.ADR = adr
#print("Get charge info:",ppIn)
ppOut = pc.GetReply(ppIn, pylonpacket.PPChargeManagementInformation)
print("Get charge info reply:",ppOut)
#return
ppIn = pylonpacket.PPGetAnalogValue()
ppIn.Command = adr
ppIn.ADR = adr
#print("Get analog:",ppIn)
ppOut = pc.GetReply(ppIn, pylonpacket.PPAnalogValue)
print("Get analog reply:",ppOut)
print("")
time.sleep(2)
#ppIn=pylonpacket.PPGetChargeManagementInformation()
#ppIn.Command=0x02
#print("Get charge info:",ppIn)
#ppOut=pc.GetReply(ppIn, pylonpacket.PPChargeManagementInformation)
#print("Get charge info reply:",ppOut)
ppIn = pylonpacket.PPGetAlarmInformation()
ppIn.Command = 0x02
print("Get alarm info:",ppIn)
ppOut = pc.GetReply(ppIn, pylonpacket.PPAlarmInformation)
print("Get alarm info reply:",ppOut) #,ppOut.info.hex())
ppIn = pylonpacket.PPTurnOff()
ppIn.Command = 0x02
print("Turn off:",ppIn)
ppOut = pc.GetReply(ppIn, pylonpacket.PPTurnOffReply)
print("Turn off reply:",ppOut)
if __name__ == "__main__":
root = logging.getLogger()
#root.setLevel(logging.DEBUG)
main(sys.argv[1:])