-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1_serial.py
executable file
·211 lines (164 loc) · 5.79 KB
/
P1_serial.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
"""
Read dsmr telgrams from P1 USB serial
To test in bash the P1 USB connector:
sudo apt-get install -y python3-serial
sudo chmod o+rw /dev/ttyUSB0
python3 -m serial.tools.miniterm /dev/ttyUSB0 115200 --xonxoff
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import serial
import threading
import time
import re
import config as cfg
# Logging
import __main__
import logging
import os
script=os.path.basename(__main__.__file__)
script=os.path.splitext(script)[0]
logger = logging.getLogger(script + "." + __name__)
class TaskReadSerial(threading.Thread):
def __init__(self, trigger, stopper, telegram):
"""
Args:
:param threading.Event() trigger: signals that new telegram is available
:param threading.Event() stopper: stops thread
:param list() telegram: dsmr telegram
"""
logger.debug(">>")
super().__init__()
self.__trigger = trigger
self.__stopper = stopper
self.__telegram = telegram
self.__counter = 0
# [ Serial parameters ]
if cfg.PRODUCTION:
self.__tty = serial.Serial()
self.__tty.port = cfg.ser_port
self.__tty.baudrate = cfg.ser_baudrate
self.__tty.bytesize = serial.SEVENBITS
self.__tty.parity = serial.PARITY_EVEN
self.__tty.stopbits = serial.STOPBITS_ONE
self.__tty.xonxoff = 0
self.__tty.rtscts = 0
self.__tty.timeout = 20
logger.info(f"Using USB device {self.__tty.port} with baud {self.__tty.baudrate}")
try:
if cfg.PRODUCTION:
self.__tty.open()
logger.debug(f"serial {self.__tty.port} opened")
else:
self.__tty = open(cfg.SIMULATORFILE, 'rb')
except Exception as e:
logger.error(f"ReadSerial: {type(e).__name__}: {str(e)}")
self.__stopper.set()
raise ValueError('Cannot open P1 serial port', cfg.ser_port)
def __del__(self):
logger.debug(">>")
def __preprocess(self):
"""
Add a virtual dsmr entry, which is sum of tariff 1 and tariff 2
"1-0:1.8.1" + "1-0:1.8.2" --> "1-0:1.8.3"
"1-0:2.8.1" + "1-0:2.8.2" --> "1-0:2.8.3"
1-0:1.8.1(016230.132*kWh)
1-0:1.8.2(007449.542*kWh)
1-0:2.8.1(005998.736*kWh)
1-0:2.8.2(015098.938*kWh)
Returns:
None
"""
e_consumed = 0.0
e_returned = 0.0
for element in self.__telegram:
try:
value = re.match(r"1-0:1\.8\.1\((\d{6}\.\d{3})\*kWh\)", element).group(1)
e_consumed = e_consumed + float(value)
except:
pass
try:
value = re.match(r"1-0:1\.8\.2\((\d{6}\.\d{3})\*kWh\)", element).group(1)
e_consumed = e_consumed + float(value)
except:
pass
try:
value = re.match(r"1-0:2\.8\.1\((\d{6}\.\d{3})\*kWh\)", element).group(1)
e_returned = e_returned + float(value)
except:
pass
try:
value = re.match(r"1-0:2\.8\.2\((\d{6}\.\d{3})\*kWh\)", element).group(1)
e_returned = e_returned + float(value)
except:
pass
# Insert the vrrtual entries in the dsmr telegram
e_consumed = "{0:10.3f}".format(e_consumed)
line = f"1-0:1.8.3({e_consumed}*kWh)"
self.__telegram.append(line)
e_returned = "{0:10.3f}".format(e_returned)
line = f"1-0:2.8.3({e_returned}*kWh)"
self.__telegram.append(line)
def __read_serial(self):
"""
Opens & Closes serial port
Reads dsmr telegrams; stores in global variable (self.__telegram)
Sets threading event to signal other clients (parser) that
new telegram is available.
In non-production mode, reads telegrams from file
Returns:
None
"""
logger.debug(">>")
while not self.__stopper.is_set():
# wait till parser has copied telegram content
# ...we need the opposite of trigger.wait()...block when set; not available
while self.__trigger.is_set():
time.sleep(0.1)
# add a counter as first field to the list
self.__counter += 1
self.__telegram.append(f"{self.__counter}")
# Decode from binary to ascii
# Remove CR LF
line = self.__tty.readline().decode('utf-8').rstrip()
# First element in telegram starts with "!"
nrof_elements = 0
while (not self.__stopper.is_set()) and (not line.startswith('!') ):
self.__telegram.append(line)
line = self.__tty.readline().decode('utf-8').rstrip()
nrof_elements += 1
# logger.debug(f"TELEGRAM Element = {line}")
# Only in simulator mode; detect EOF in file
if (not cfg.PRODUCTION) and line.startswith('EOF'):
self.__stopper.set()
logger.debug(f"EOF Detected in {cfg.SIMULATORFILE}")
break
# do some magic on telegram
self.__preprocess()
# Trigger that new telegram is available for MQTT
self.__trigger.set()
# In simulation mode, insert a delay
if not cfg.PRODUCTION:
# 1sec delay mimics dsmr behaviour, which transmits every 1sec a telegram
time.sleep(1.0)
logger.debug("<<")
def run(self):
logger.debug(">>")
try:
# In production, ReadSerial has infinite loop
# In simulation, ReadSerial will return @ EOF
self.__read_serial()
except Exception as e:
logger.error(f"Exception: {e}")
finally:
self.__tty.close()
self.__stopper.set()
logger.debug("<<")