annotate epro.py @ 12:0a571da65068

Use logging rather than print.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 06 Dec 2021 11:25:41 +1030
parents 3baca74482b6
children 4450cf739263
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
3 import logging
4
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
4 import serial
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
5 import sys
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
6
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
7 logger = logging.getLogger('epro')
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
8
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 # View facing ePro from the back
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 # +---+
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 # +-| |-|
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 # | |
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 # | |
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 # +-------+
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # 1 ... 6
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 #
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 # RJ12 plug (Jaycar cable colours)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 # 1 white temp out
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 # 2 black temp in
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 # 3 red GND
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 # 4 green TX TTL (5V)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 # 5 yellow RX TTL (5V)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 # 6 blue VCC (12V)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 #
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 # 2400bps 8E1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 # s = serial.Serial('/dev/cu.usbserial-AM01Z7TZ', 2400, parity='E')
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 class Packet(object):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 MSGTYPE = -1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 self.dstadr = dstadr
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 self.srcadr = srcadr
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 self.devid = devid
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 self.msgtype = msgtype
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 self.data = data
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 hdr = "Src: 0x%02x Dst: 0x%02x DevID: 0x%02x MsgType: 0x%02x" % (
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 self.dstadr, self.srcadr, self.devid, self.msgtype)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 # MSGTYPE is overridden for subclasses which also have their own repr so don't dumb data here
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 if self.MSGTYPE == -1:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 hdr += " Data:"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 for d in self.data:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 hdr += " 0x%02x" % (d)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 else:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 hdr += " Name: " + self.MSGNAME
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 return hdr
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 class MainVoltage(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 """Main Battery Voltage"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 MSGTYPE = 0x60
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 MSGNAME = "Main Voltage"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 super(MainVoltage, self).__init__(dstadr, srcadr, devid, msgtype, data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 self.volts = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x03) << 14) / 100.0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 s = super(MainVoltage, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 s += ": %.2f V" % (self.volts)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 class BatteryCurrent(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 """Main Battery Current"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 MSGTYPE = 0x61
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 MSGNAME = "Battery Current"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 super(BatteryCurrent, self).__init__(dstadr, srcadr, devid, msgtype, data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 self.amps = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x3f) << 14) / 100.0
6
2f7ee650e6fb Use correct byte for sign bit.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
75 if data[0] & 0x40:
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 self.amps *= -1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 s = super(BatteryCurrent, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 s += ": %.2f A" % (self.amps)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 class AmpHours(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 """Number of amp hours removed from the battery"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 MSGTYPE = 0x62
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 MSGNAME = "Amp Hours"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 super(AmpHours, self).__init__(dstadr, srcadr, devid, msgtype, data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 self.amphrs = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x3f) << 14) / 100.0
7
3baca74482b6 Fix a few more incorrect sign bits
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
92 if data[0] & 0x40:
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 self.amphrs *= -1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 s = super(AmpHours, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 s += ": %.2f Ah" % (self.amphrs)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 class StateOfCharge(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 """State of battery charge"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 MSGTYPE = 0x64
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 MSGNAME = "State Of Charge"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 super(StateOfCharge, self).__init__(dstadr, srcadr, devid, msgtype, data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 self.soc = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x03) << 14) / 10.0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 s = super(StateOfCharge, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 s += ": %.2f%%" % (self.soc)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 class TimeRemaining(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 """Time remaining until battery needs charging"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 MSGTYPE = 0x65
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 MSGNAME = "Time remaining"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 super(TimeRemaining, self).__init__(dstadr, srcadr, devid, msgtype, data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 self.time = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x3f) << 14)
7
3baca74482b6 Fix a few more incorrect sign bits
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
124 if data[0] & 0x40:
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 self.time *= -1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 s = super(TimeRemaining, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 s += ": %.2f min" % (self.time)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 class BatteryTemperature(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 """Battery temperature (degrees Celcius)"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 MSGTYPE = 0x66
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 MSGNAME = "Battery Temperature"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 super(BatteryTemperature, self).__init__(dstadr, srcadr, devid, msgtype, data)
3
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
140 self.temp = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x3f) << 14) / 10.0
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 if data[2] & 0x40:
3
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
142 self.temp *= -1
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
143 if self.temp == -20:
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
144 self.temp = None
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 s = super(BatteryTemperature, self).__repr__()
3
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
148 if self.temp == None:
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
149 s += ": n/a"
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
150 else:
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
151 s += ": %.2f degC" % (self.temp)
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 class MonitorStatus(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 """Monitor status"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 MSGTYPE = 0x67
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 MSGNAME = "Monitor Status"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 super(MonitorStatus, self).__init__(dstadr, srcadr, devid, msgtype, data)
5
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
162 self.autosyncvolt = bool(data[0] & 0x10)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
163 self.autosyncamp = bool(data[0] & 0x08)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
164 self.autosyncchrg = bool(data[0] & 0x04)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
165 self.e501compat = bool(data[0] & 0x02)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
166 self.alarmtst = bool(data[0] & 0x01)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
167 self.backlight = bool(data[1] & 0x40)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
168 self.disptst = bool(data[1] & 0x20)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
169 self.tempsense = bool(data[1] & 0x10) # Seems to be inverted from data sheet
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
170 self.auxhv = bool(data[1] & 0x08)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
171 self.auxlv = bool(data[1] & 0x04)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
172 self.lock = bool(data[1] & 0x02)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
173 self.mainhv = bool(data[1] & 0x01)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
174 self.mainlv = bool(data[2] & 0x40)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
175 self.lowbatalarm = bool(data[2] & 0x20)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
176 self.batflat = bool(data[2] & 0x10)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
177 self.batfull = bool(data[2] & 0x08)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
178 self.charged = bool(data[2] & 0x04)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
179 self.nosync = bool(data[2] & 0x02)
982eeffe9d95 Convert booleans to boolean. Sqlite doesn't do it and stores them as ints instead
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
180 self.monreset = bool(data[2] & 0x01)
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 s = super(MonitorStatus, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 stats = ( ( "ASV", self.autosyncvolt ), ( "ASA", self.autosyncamp ), ( "ASC" , self.autosyncchrg ),
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 ( "E501", self.e501compat ), ( "Alarm test", self.alarmtst ), ( "Light", self.backlight ),
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 ( "Temperature Sensor", self.tempsense ), ( "Aux HV", self.auxhv ), ( "Aux LV", self.auxlv ),
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 ( "Lock", self.lock ), ( "Main HV", self.mainhv ), ( "Main LV", self.mainlv ),
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 ( "Low Battery", self.lowbatalarm ), ( "Battery Flat", self.batfull ),
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 ( "Battery Full", self.batfull ), ( "Battery Charged", self.charged ),
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 ( "No Sync", self.nosync ), ( "Monitor Reset", self.monreset ) )
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 for (short, var) in stats:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 if var:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 s += " %s : True" % (short)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 else:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 s += " %s : False" % (short)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 class AuxVoltage(Packet):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 """Aux Battery Voltage"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 MSGTYPE = 0x68
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 MSGNAME = "Aux Voltage"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 def __init__(self, dstadr, srcadr, devid, msgtype, data):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 super(AuxVoltage, self).__init__(dstadr, srcadr, devid, msgtype, data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 self.volts = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x03) << 14) / 100.0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 def __repr__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 s = super(AuxVoltage, self).__repr__()
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 s += ": %.2f V" % (self.volts)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 return s
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 class Processor(object):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 PKT_TYPES = { MainVoltage.MSGTYPE : MainVoltage, BatteryCurrent.MSGTYPE : BatteryCurrent,
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 AmpHours.MSGTYPE : AmpHours, StateOfCharge.MSGTYPE : StateOfCharge,
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 TimeRemaining.MSGTYPE : TimeRemaining, BatteryTemperature.MSGTYPE : BatteryTemperature,
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 MonitorStatus.MSGTYPE : MonitorStatus, AuxVoltage.MSGTYPE : AuxVoltage }
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 def __init__(self):
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 self.state = 0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 self.packets = []
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 def process(self, dat):
4
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
225 added = False
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 for d in dat:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 d = ord(d)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 if d == 0xff and self.state != 4:
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
229 logger.warn("Packet corruption")
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 continue
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 if self.state == 0:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 # Waiting for destination address (MSB set but not 0xff as that is EOM)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 if d == 0xff or d & 0x80 == 0:
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
235 logger.info("Skipping byte")
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 continue
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 self.dstadr = d & 0x7f
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 self.data = []
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 self.state += 1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 elif self.state == 1:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 # Source address
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 self.srcadr = d
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 self.state += 1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 elif self.state == 2:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 # Device ID
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 self.devid = d
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 self.state += 1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 elif self.state == 3:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 # Message type
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 self.msgtype = d
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 self.state += 1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 elif self.state == 4:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 # Data
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 if d != 0xff:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 self.data.append(d)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 continue
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257 self.state = 0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 if self.msgtype in Processor.PKT_TYPES:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259 t = self.PKT_TYPES[self.msgtype]
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 if len(self.data) != t.LEN:
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
261 logger.warn("Packet length incorrect, expected %d got %d", t.LEN, len(self.data))
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 continue
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 p = self.PKT_TYPES[self.msgtype](self.dstadr, self.srcadr, self.devid, self.msgtype, self.data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 else:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266 p = Packet(self.dstadr, self.srcadr, self.devid, self.msgtype, self.data)
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 self.packets.append(p)
4
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
269 added = True
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
270 return added
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
271
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
272 def main():
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
273 if len(sys.argv) != 2:
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
274 print('Bad usage')
4
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
275 exit(1)
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
276
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
277 s = serial.Serial(sys.argv[1], 2400, parity='E')
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
278 s.timeout = 0.2
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
279
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
280 p = Processor()
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
281 while True:
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
282 res = p.process(s.read(1024))
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
283 while len(p.packets) > 0:
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
284 print(p.packets.pop(0))
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 if __name__ == '__main__':
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 main()