forked from akileos/TeleQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teleinfo.py
executable file
·42 lines (33 loc) · 1.47 KB
/
teleinfo.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
#!/usr/bin/env python
import serial
class Teleinfo:
ser = serial.Serial()
def __init__ (self, port='/dev/teleinfo'):
self.ser = serial.Serial(port, baudrate=1200, bytesize=serial.SEVENBITS, parity=serial.PARITY_EVEN)
def checksum (self, etiquette, valeur):
sum = 32
for c in etiquette: sum = sum + ord(c)
for c in valeur: sum = sum + ord(c)
sum = (sum & 63) + 32
return chr(sum)
def read (self):
# Wait for data
while self.ser.inWaiting() :
message = ""
completed = False
while not completed:
char = self.ser.read(1)
if char != chr(2):
message = message + char
else:
completed = True
frames = [
frame.split(" ")
for frame in message.strip("\r\n\x03").split("\r\n")
]
framesOK = dict([
[frame[0],frame[1]]
for frame in frames
if (len(frame) == 3) and (self.checksum(frame[0],frame[1]) == frame[2])
])
return framesOK