annotate epro.py @ 28:ea07d14075e6 default tip

Remove superfluous statement.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 21 Dec 2021 15:06:13 +1030
parents 8b8efc343b0c
children
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 # +---+
27
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
11 # +-+ +-+
0
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 # +-------+
27
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
15 # 6 ... 1
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 #
27
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
17 # Solid Twisted Signal Actual twisted
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
18 # Colors Colours Name Colours
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
19 #---------------------------------------------------------
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
20 # 1 Blue Wht/Grn VCC (VBatt) Grn
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
21 # 2 Yellow Wht/Org RX TTL (5V) Org
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
22 # 3 Green Blu TX TTL (5V) Wht/Blu
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
23 # 4 Red Wht/Blu GND Blu
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
24 # 5 Black Org Temp in Wht/Org
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
25 # 6 White Grn Temp out Wht/Grn
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
26 #
8b8efc343b0c Update comment about wiring althoug it's clear as mud..
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
27 # Note sure what happened with the wiring..
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 #
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 # 2400bps 8E1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 # s = serial.Serial('/dev/cu.usbserial-AM01Z7TZ', 2400, parity='E')
13
4450cf739263 Add link to protocol document.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
31 #
4450cf739263 Add link to protocol document.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
32 # Protocol document: https://www.enerdrive.com.au/wp-content/uploads/ePRO-communication-interface-spec-rev.02.pdf
0
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 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
35 MSGTYPE = -1
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 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
38 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
39 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
40 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
41 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
42 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
43
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 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
45 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
46 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
47 # 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
48 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
49 hdr += " Data:"
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 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
51 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
52 else:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 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
54
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 return hdr
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 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
58 """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
59 MSGTYPE = 0x60
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 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
61 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 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
64 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
65 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
66
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 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
68 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
69 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
70 return s
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 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
73 """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
74 MSGTYPE = 0x61
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 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
76 LEN = 3
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 __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
79 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
80 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
81 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
82 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
83
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 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
85 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
86 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
87 return s
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 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
90 """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
91 MSGTYPE = 0x62
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 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
93 LEN = 3
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 __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
96 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
97 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
98 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
99 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
100
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 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
102 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
103 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
104 return s
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 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
107 """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
108 MSGTYPE = 0x64
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 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
110 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 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
113 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
114 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
115
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 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
117 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
118 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
119 return s
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 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
122 """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
123 MSGTYPE = 0x65
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 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
125 LEN = 3
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 __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
128 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
129 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
130 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
131 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
132
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 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
134 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
135 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
136 return s
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 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
139 """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
140 MSGTYPE = 0x66
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 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
142 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 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
145 super(BatteryTemperature, self).__init__(dstadr, srcadr, devid, msgtype, data)
26
3090d8bf4242 Fix temperature decoding.
Daniel O'Connor <darius@dons.net.au>
parents: 14
diff changeset
146 self.temp = (data[2] & 0x7f | (data[1] & 0x7f) << 7 | (data[0] & 0x03) << 14) / 10.0
3090d8bf4242 Fix temperature decoding.
Daniel O'Connor <darius@dons.net.au>
parents: 14
diff changeset
147 if data[0] & 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
148 self.temp *= -1
14
60ead9b5fc1b Mark temperature as invalid when <-20C as per spec.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
149 if self.temp < -20:
3
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
150 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
151
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 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
153 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
154 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
155 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
156 else:
6d0fe22566ab Fix cut & paste error for variable name, report disconnected sensor specially.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
157 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
158 return s
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 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
161 """Monitor status"""
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 MSGTYPE = 0x67
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 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
164 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 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
189 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
190 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
191 ( "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
192 ( "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
193 ( "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
194 ( "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
195 ( "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
196 ( "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
197 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
198 if var:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 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
200 else:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 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
202
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 return s
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 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
206 """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
207 MSGTYPE = 0x68
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 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
209 LEN = 3
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 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
212 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
213 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
214
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 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
216 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
217 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
218 return s
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 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
221 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
222 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
223 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
224 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
225
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 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
227 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
228 self.packets = []
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 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
231 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
232 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
233 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
234 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
235 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
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
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 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
239 # 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
240 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
241 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
242 continue
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 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
244 self.data = []
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 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
246 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
247 # Source address
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 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
249 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
250 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
251 # Device ID
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 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
253 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
254 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
255 # Message type
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 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
257 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
258 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
259 # Data
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 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
261 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
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 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
264 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
265 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
266 if len(self.data) != t.LEN:
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
267 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
268 continue
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 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
271 else:
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 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
273
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 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
275 added = True
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
276 return added
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
277
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
278 def main():
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
279 if len(sys.argv) != 2:
12
0a571da65068 Use logging rather than print.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
280 print('Bad usage')
4
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
281 exit(1)
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
282
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
283 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
284 s.timeout = 0.2
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
285
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
286 p = Processor()
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
287 while True:
787d9c8fdec6 Add working example when run as an exe
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
288 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
289 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
290 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
291
1de08e57774f Initial comit of code to talk to EnerDrive ePro battery monitor
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 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
293 main()