annotate iec1107.py @ 2:8f1a773a3cd5

Don't strip lines to make printing easier.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 20 Nov 2013 13:16:56 +1030
parents 99f25c8ab92f
children 535076e31660
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 #
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # http://www.domoticaforum.eu/viewtopic.php?f=71&t=7489
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 #
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import exceptions
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import serial
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 import sys
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 import time
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 baudtable = {'0' : 300, '1' : 600, '2' : 1200, '3' : 2400, '4' : 4800, '5' : 9600, '6' : 19200}
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 class Error(exceptions.BaseException):
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 pass
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
17 class IEC1107Reading(object):
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
18 def __init__(self, port, force300bps = True):
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 # Open port
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
20 s = serial.Serial(port, baudrate = 300, bytesize = 7, parity = 'E', stopbits = 1)
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
21 s.timeout = 2.5
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 # Send ident message
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
24 s.write('/?!\r\n')
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
25 rtn = s.readline()
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
26 if len(rtn) == 0:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
27 raise Error('No reply to probe')
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 if len(rtn) < 6 or rtn[0] != '/' or rtn[-1] != '\n' or rtn[-2] != '\r':
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 raise Error('Invalid line "%s"' % (rtn))
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
30
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 rtn = rtn.strip()
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 self.mfg = rtn[1:4]
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 if self.mfg[2].isupper():
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 self.restime = 0.2
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 else:
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 self.restime = 0.02
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
39 if force300bps:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
40 self.baudid = '0'
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
41 else:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
42 self.baudid = rtn[4]
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 if self.baudid not in baudtable:
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 raise Error('Invalid baud rate %c from "%s"' % (selfbaudid, rtn))
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 else:
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 self.baud = baudtable[self.baudid]
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 if rtn[5] == '/':
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 self.mode = rtn[6]
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 self.mfg = rtn[7:]
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 else:
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 self.mode = None
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 self.mfg = rtn[5:]
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 # Send ACK/option message
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 # Byte Meaning
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 # 0 ACK (0x06)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 # 1 Protocol character ('0' = normal, '1' = secondary, '2' = HDLC protocol)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 # 2 Baud rate ID ('0', '1', etc)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 # 3 Mode control('0' = read data, '1' = device prog)
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
61 s.write('\x060%c0\r\n' % (self.baudid))
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 time.sleep(self.restime)
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
64 s.setBaudrate(self.baud)
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
65
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
66 lines = []
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
67 cksum = 0
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
69 # Read STX
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
70 head = s.read(1)
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
71 if len(head) == 0:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
72 raise Error('No reply to query')
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
73 if head != '\x02':
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
74 raise Error('Invalid reply header 0x%02x' % (ord(head)))
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
75
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
76 # Read result lines
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
77 while True:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
78 line = s.readline()
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
79 cksum ^= reduce(lambda x, y: x ^ y, map(ord, line))
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
80 if len(line) == 0:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
81 raise Error('Timeout during message')
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
82 if line == '!':
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
83 break
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
84 lines.append(line)
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
86 # Read trailer
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
87 fin = s.read(2)
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
88 if len(fin) != 2:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
89 raise Error('Timeout reading trailer')
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
90 if fin[0] != '\x03':
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
91 raise Error('Trailer malformed, expected 0x03, got 0x%02x' % (ord(fin[0])))
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
93 # Validate checksum
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
94 cksum ^= ord(fin[0])
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
95 if cksum != ord(fin[1]):
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
96 raise Error('Checksum mismatch, expected 0x%02x, got 0x%02x' % (cksum, ord(fin[1])))
2
8f1a773a3cd5 Don't strip lines to make printing easier.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
97 self.rawreading = lines
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
98 del s
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
99
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 def main():
1
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
101 if len(sys.argv) != 2:
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
102 print 'Bad usage'
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
103 print '\t%s portname' % (sys.argv[0])
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
104 sys.exit(1)
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
105
99f25c8ab92f Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
106 res = IEC1107Reading(sys.argv[1])
2
8f1a773a3cd5 Don't strip lines to make printing easier.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
107 map(sys.stdout.write, res.rawreading)
8f1a773a3cd5 Don't strip lines to make printing easier.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
108 print res
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 if __name__ == '__main__':
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 main()
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 # Meter number is 1288004
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 # 1.8.0 is import
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 # 1.8.1 is ??
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 # 1.8.2 is ??
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 # 1.8.3 is ??
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 # 2.8.0 is export
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 # C.1(12880041.0(22:25 18-11-13)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 # 1.8.1(0000000597*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 # 1.8.2(0000000000*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 # 1.8.3(0000264238*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 # 1.8.0(0000264835*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 # 2.8.0(0000511354*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 # ==> /?!<0D><0A>
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 # <== /ACE5SMLCD
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 # ==> <06>050<0D><0A>
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 # <== -- STX --
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 # <== C.1(12880041.0(22:48 18-11-13)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 # <== 1.8.1(0000000597*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 # <== 1.8.2(0000000000*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 # <== 1.8.3(0000264460*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 # <== 1.8.0(0000265057*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 # <== 2.8.0(0000511354*Wh)
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 # <== !
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 # <== -- ETX --
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 # <== -- BCC --