-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIEC62056_21.py
executable file
·227 lines (168 loc) · 6.06 KB
/
IEC62056_21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import serial
import logging
import time
import re
import sys
from decimal import *
from string import Template
from collections import namedtuple
from BraceMessage import BraceMessage as __
IdentificationMessage = namedtuple("IdentificationMessage", "manufacturers_identification baudrate_identification identification protocol_mode baudrate")
DataSetMessage = namedtuple("DataSetMessage", "id value unit")
class InvalidMessageError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class TimeoutException(Exception):
pass
class IEC62056_21:
dataset_regex = re.compile("(?P<ID>[^\(\)/!]*)\((?P<Value>\d+(.\d)+)(\*(?P<Unit>[^\(\)/!]+))?\)")
logger = logging.getLogger(__name__)
ser_is_blocking = True
min_reaction_time=0.2
ctrl_StartChar = bytes('/', 'ascii')[0]
ctrl_EndChar = bytes('!', 'ascii')[0]
ctrl_EOF = bytes('\x21', 'ascii')[0]
ctrl_CTX = bytes('\x02', 'ascii')[0] # Start of text
ctrl_ETX = bytes('\x03', 'ascii')[0] # End of text
ctrl_ACK = bytes('\x06', 'ascii')[0] # Acknowledge
ctrl_NAK = bytes('\x15', 'ascii')[0] # Negative acknowledge
ctrl_SOH = bytes('\x01', 'ascii')[0] # Start of header
def __init__(self, port,
baudrate=300, bytesize=serial.SEVENBITS,
parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE):
self.ser = serial.Serial(port, baudrate, bytesize, parity, stopbits,timeout=self.min_reaction_time*2)
self.logger.debug(__("Using port '{port}', baudrate {baudrate}.",
port = self.ser.portstr, baudrate=self.ser.baudrate))
def close(self):
self.ser.close()
self.ser = None
def read(self):
self.ser.baudrate = 300
msg_signon = bytearray("/?!\r\n", 'ascii')
# 1 start bit + parity + 1 stop bit
expected_delay = len(msg_signon)*1.0 / (self.ser.baudrate / (self.ser.bytesize + 3));
start = time.time()
self.ser.write(msg_signon)
self.ser.flush()
end = time.time()
actual_delay = (end-start)
if actual_delay < expected_delay:
self.ser_is_blocking = False
self.logger.info(__("Non blocking writes. Expected: {expected_duration:3}, actual delay: {duration:3.1}",
expected_duration = expected_delay, duration=actual_delay,
))
time.sleep(expected_delay-actual_delay)
identification_message = self._read_identification_message()
self.logger.debug(identification_message)
self._write_handshake(identification_message)
for dataset in self._read_data_message():
yield self._read_dataset_structure(dataset)
def _write_blocking(self, data):
# 1 start bit + parity + 1 stop bit
expected_delay = len(data) * 1.0 / (self.ser.baudrate / (self.ser.bytesize + 3));
w = self.ser.write(data)
start = time.time();
self.ser.flush()
delay = expected_delay - (time.time() - start)
if not self.ser_is_blocking and delay > 0:
time.sleep(delay)
def _read_identification_message(self):
msg = self._read_dataline()
if len(msg) == 0:
raise TimeoutException
if not msg[0] == ord('/'):
while True:
b = self.ser.read()
if len(b) == 0:
break
msg += b
raise InvalidMessageError("Missing start character, got: " + repr(msg[0]))
if len(msg) < 6:
raise InvalidMessageError("Identification message to short < 5: {0}".format(msg))
manufacturers_identification = msg[1:4].decode('ascii')
baudrate_identification = chr(msg[4])
if msg[5] == '\\':
if len(msg) < 8:
raise InvalidMessageError("Identification message to short < 8: {0}".format(msg))
identification = msg[7:len(msg)-2].decode('ascii')
else:
identification = msg[5:len(msg)-2].decode('ascii')
baudrateselection = [300, 600, 1200, 2400, 4800, 9600, 19200,
0, 0, 0, #reserved
600, 1200, 2400, 4800, 9600, 19200]
protocol_mode = 'A'
if baudrate_identification > 'A' and baudrate_identification <= 'I':
protocol_mode = 'B'
if baudrate_identification > '0' and baudrate_identification <= '9':
protocol_mode = 'C'
if msg[5] == '\\':
protocol_mode = 'E'
baudrate = baudrateselection[int(baudrate_identification, 16)]
result = IdentificationMessage(
manufacturers_identification, baudrate_identification, identification, protocol_mode, baudrate)
return result
def _read_data_message(self):
b = self.ser.read()
if len(b) == 0:
raise TimeoutException
if b[0] != self.ctrl_CTX:
error = "No STX frame start character: " + repr(b)
raise InvalidMessageError(error)
b = self.ser.read()
while b[0] != self.ctrl_EOF:
if len(b) > 0:
dataline = self._read_dataline(bytearray(b))
yield dataline
b = self.ser.read()
b = self.ser.read()
b = self.ser.read()
b = self.ser.read()
if b[0] != self.ctrl_ETX:
error = "No ETX frame end character: " + repr(b)
raise InvalidMessageError(error)
b = self.ser.read()
self.logger.debug(__("BCC Value: {bcc}", bcc=b))
def _read_dataset_structure(self, dataset):
result = self.dataset_regex.match(dataset.decode('ascii'))
if result == None:
error = "Unable to parse dataset structure: " + dataset
raise InvalidMessageError(error)
id = str(result.group("ID"))
value = Decimal(str(result.group("Value")))
if (result.group("Unit") == None):
unit = None
else:
unit = str(result.group("Unit"))
message = DataSetMessage(id, value, unit)
return message
def _write_handshake(self, identification_message):
data = bytearray([self.ctrl_ACK, ord('0'), ord(identification_message.baudrate_identification), ord('0'), 0x0D, 0x0A]);
self._write_blocking(data)
time.sleep(self.min_reaction_time)
self.ser.baudrate = identification_message.baudrate
def _read_dataline(self, msg = None):
if msg == None:
msg = bytearray()
eom = bytes('\r\n', 'ascii')
m = 0
b = self.ser.read()
while len(b) > 0:
msg += b
if eom[m] == b[0]:
m += 1
else:
m = 0
if m == len(eom):
msg.pop(len(msg)-1)
msg.pop(len(msg)-1)
break
b = self.ser.read()
return msg
if __name__ == '__main__':
if len(sys.argv) != 2:
exit(0)
meter = IEC62056_21(sys.argv[1])
for readout in meter.read():
print("Readout: ", readout)