-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathiec1107.py
135 lines (126 loc) · 3.41 KB
/
iec1107.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
#!/usr/bin/env python
#
# Copyright (c) 2020 Poul-Henning Kamp <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
import serial
class dlmsError(Exception):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return repr(self.reason)
class dlms(object):
def __init__(self, serial_port = "/dev/cuaU3"):
self.ser = serial.Serial(
port = serial_port,
baudrate = 300,
bytesize = serial.SEVENBITS,
parity = serial.PARITY_EVEN,
timeout = 3.0)
def query(self):
self.ser.write("/?!\r\n")
state = 0
id = ""
cont = ""
sum = 0
while True:
a = self.ser.read(1)
if len(a) == 0:
raise dlmsError("Rx Timeout")
b = bytearray(a)[0]
if state == 0:
# Read ID string
if b >= 32:
id += a
elif b == 13:
state = 1
else:
raise dlmsError(
"Illegal char in ident 0x%02x" % b)
state = 99
elif state == 1:
# NL ending ID string
if b != 10:
raise dlmsError(
"Ident has 0x%02x after CR" % b)
state = 99
else:
state = 2
elif state == 2:
# STX
if b != 2:
raise dlmsError(
"Expected STX not 0x%02x" % b)
state = 99
else:
state = 3
elif state == 3:
# message body
sum ^= b
if b != 3:
cont += a
else:
state = 4
elif state == 4:
# Checksum
if sum != b:
raise dlmsError(
"Checksum Mismatch")
state == 99
else:
return self.parse(id, cont)
elif state == 99:
# Error, flush
pass
assert False
def parse(self, id, cont):
l = list()
l.append(id)
l.append(dict())
cont = cont.split("\r\n")
if cont[-1] != "":
raise dlmsError(
"Last data item lacks CRNL")
if cont[-2] != "!":
raise dlmsError(
"Last data item not '!'")
for i in cont[:-2]:
if i[-1] != ")":
raise dlmsError(
"Last char of data item not ')'")
return None
i = i[:-1].split("(")
j = i[1].split("*")
l[1][i[0]] = j
return l
if __name__ == "__main__":
foo = dlms()
a = foo.query()
print("%16s: %s" % ("identifier", a[0]))
print("")
for i in a[1]:
j = a[1][i]
if len(j) == 2:
print("%16s: %s [%s]" % (i, j[0], j[1]))
else:
print("%16s: %s" % (i, j[0]))