Mercurial > ~darius > hgwebdir.cgi > iec1107
comparison iec1107.py @ 0:fd8520d969c4
Initial commit, only readmeter function works
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Wed, 20 Nov 2013 10:16:37 +1030 |
parents | |
children | 99f25c8ab92f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fd8520d969c4 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 # | |
4 # http://www.domoticaforum.eu/viewtopic.php?f=71&t=7489 | |
5 # | |
6 | |
7 import exceptions | |
8 import serial | |
9 import sys | |
10 import time | |
11 | |
12 baudtable = {'0' : 300, '1' : 600, '2' : 1200, '3' : 2400, '4' : 4800, '5' : 9600, '6' : 19200} | |
13 | |
14 class Error(exceptions.BaseException): | |
15 pass | |
16 | |
17 def readmeter(portname): | |
18 s = serial.Serial(portname, baudrate = 300, bytesize = 7, parity = 'E', stopbits = 1) | |
19 s.timeout = 1.5 | |
20 | |
21 # Send ident message | |
22 s.write('/?!\r\n') | |
23 | |
24 rtn = s.readline() | |
25 if len(rtn) < 6 or rtn[0] != '/' or rtn[-1] != '\n' or rtn[-2] != '\r': | |
26 raise Error('Invalid line "%s"' % (rtn)) | |
27 | |
28 # ACK meter and ask for data | |
29 s.write('\x06000\x0d\x0a') | |
30 s.timeout = 2 | |
31 | |
32 lines = [] | |
33 cksum = 0 | |
34 | |
35 line = s.readline() | |
36 if len(line) == 0 or line[0] != '\x02': | |
37 raise Error('No reply to query or invalid reply \"%s\"' % (line)) | |
38 else: | |
39 cksum ^= reduce(lambda x, y: x ^ y, map(ord, line[1:])) | |
40 lines.append(line[1:].strip()) | |
41 while True: | |
42 line = s.readline() | |
43 cksum ^= reduce(lambda x, y: x ^ y, map(ord, line)) | |
44 line = line.strip() | |
45 if len(line) == 0: | |
46 raise Error('Timeout during message') | |
47 if line == '!': | |
48 break | |
49 lines.append(line) | |
50 fin = s.read(2) | |
51 if len(fin) != 2: | |
52 raise Error('Timeout reading trailer') | |
53 if fin[0] != '\x03': | |
54 raise Error('Trailer malformed, expected 0x03, got 0x%02x' % (ord(fin[0]))) | |
55 | |
56 cksum ^= ord(fin[0]) | |
57 if cksum != ord(fin[1]): | |
58 raise Error('Checksum mismatch, expected 0x%02x, got 0x%02x' % (cksum, ord(fin[1]))) | |
59 return lines | |
60 | |
61 | |
62 class IEC1107(object): | |
63 def __init__(self, port): | |
64 # Open port | |
65 self.s = serial.Serial(port, baudrate=300, bytesize=7, parity='E', stopbits=1) | |
66 self.s.timeout = 0.2 | |
67 | |
68 # Send ident message | |
69 self.s.write('/?!\r\n') | |
70 self.s.flush() | |
71 rtn = self.s.readline() | |
72 if len(rtn) < 6 or rtn[0] != '/' or rtn[-1] != '\n' or rtn[-2] != '\r': | |
73 raise Error('Invalid line "%s"' % (rtn)) | |
74 rtn = rtn.strip() | |
75 self.mfg = rtn[1:4] | |
76 | |
77 if self.mfg[2].isupper(): | |
78 self.restime = 0.2 | |
79 else: | |
80 self.restime = 0.02 | |
81 | |
82 #self.baudid = rtn[4] | |
83 self.baudid = '0' | |
84 if self.baudid not in baudtable: | |
85 raise Error('Invalid baud rate %c from "%s"' % (selfbaudid, rtn)) | |
86 else: | |
87 self.baud = baudtable[self.baudid] | |
88 | |
89 if rtn[5] == '/': | |
90 self.mode = rtn[6] | |
91 self.mfg = rtn[7:] | |
92 else: | |
93 self.mode = None | |
94 self.mfg = rtn[5:] | |
95 | |
96 # Send ACK/option message | |
97 # Byte Meaning | |
98 # 0 ACK (0x06) | |
99 # 1 Protocol character ('0' = normal, '1' = secondary, '2' = HDLC protocol) | |
100 # 2 Baud rate ID ('0', '1', etc) | |
101 # 3 Mode control('0' = read data, '1' = device prog) | |
102 self.s.write('\x060%c0\x0d\x0a' % (self.baudid)) | |
103 self.s.flush() | |
104 | |
105 time.sleep(self.restime) | |
106 self.s.setBaudrate(self.baud) | |
107 | |
108 self.s.timeout = 1 | |
109 | |
110 def getdata(self): | |
111 self.dat = '' | |
112 while len(self.dat) == 0: | |
113 data = self.s.read(1000) | |
114 self.dat += data | |
115 | |
116 cksum = reduce(lambda x, y: x ^ y, map(ord,self.dat[1:-1])) | |
117 if cksum != ord(self.dat[-1]): | |
118 raise Error('checksum mismatch, epected 0x%02x got 0x%02x' % (cksum, ord(self.dat[-1]))) | |
119 return self.dat | |
120 | |
121 def main(): | |
122 d = IEC1107('/dev/cu.usbserial-AM01Z7UC') | |
123 | |
124 if __name__ == '__main__': | |
125 main() | |
126 | |
127 s = '\x02C.1(12880041.0(22:25 18-11-13)\r\n1.8.1(0000000597*Wh)\r\n1.8.2(0000000000*Wh)\r\n1.8.3(0000264238*Wh)\r\n1.8.0(0000264835*Wh)\r\n2.8.0(0000511354*Wh)\r\n!\r\n\x03{' | |
128 | |
129 | |
130 # Meter number is 1288004 | |
131 # 1.8.0 is import | |
132 # 1.8.1 is ?? | |
133 # 1.8.2 is ?? | |
134 # 1.8.3 is ?? | |
135 # 2.8.0 is export | |
136 | |
137 # C.1(12880041.0(22:25 18-11-13) | |
138 # 1.8.1(0000000597*Wh) | |
139 # 1.8.2(0000000000*Wh) | |
140 # 1.8.3(0000264238*Wh) | |
141 # 1.8.0(0000264835*Wh) | |
142 # 2.8.0(0000511354*Wh) | |
143 | |
144 # ==> /?!<0D><0A> | |
145 # <== /ACE5SMLCD | |
146 # ==> <06>050<0D><0A> | |
147 # <== -- STX -- | |
148 # <== C.1(12880041.0(22:48 18-11-13) | |
149 # <== 1.8.1(0000000597*Wh) | |
150 # <== 1.8.2(0000000000*Wh) | |
151 # <== 1.8.3(0000264460*Wh) | |
152 # <== 1.8.0(0000265057*Wh) | |
153 # <== 2.8.0(0000511354*Wh) | |
154 # <== ! | |
155 # <== -- ETX -- | |
156 # <== -- BCC -- |