-
Notifications
You must be signed in to change notification settings - Fork 0
/
M-Bus.py
73 lines (61 loc) · 1.71 KB
/
M-Bus.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
import serial
import time
import datetime
class DataError(Exception):
def __init__(self,data):
self.data = data
def SendData(SerObj, Cmd, Number):
"""docstring for SendData"""
time.sleep(0.05)
SerObj.write(Cmd)
t1=time.time()
data = ser.read(1)
t2=time.time()
data = data + ser.read(Number-1)
#print tinfo,
tinfo = (t2-t1)*1000
if len(data) != Number:
return ["No Receive",tinfo,data]
else:
string = ' '.join(hex(ord(x))[2:] for x in data)
return [string,tinfo,data]
def SendNKE(SerObj):
"""docstring for fname"""
data = SendData(SerObj,SND_NKE,1)
if data[2] != "\xE5":
raise DataError(data[2])
return data
else:
return data
ser = serial.Serial(port='COM9', baudrate=2400, timeout=0.5, parity=serial.PARITY_EVEN)
fdata = open("data.txt",'a')
print "Port Name:" + ser.name
#x = ser.read() # read one byte
#s = ser.read(10) # read up to ten bytes (timeout)
#line = ser.readline() # read a '\n' terminated line
SND_NKE = "\x10\x40\xFD\x3D\x16" #1 byte return
REQ_UD2 = "\x10\x7B\xFD\x78\x16" #34 byte return
counter=0
tsum= 8.0 * 10
while True:
print str(counter) + ": ",
data = str(datetime.datetime.now())
fdata.write(data + "\n")
data = SendData(ser,SND_NKE,1)
print "'"+data[0]+"'",
data = data[0] + "\n"
fdata.write(data)
data = SendData(ser,REQ_UD2,33)
tsum = tsum - tsum / 10
tsum = data[1] + tsum
tinfo="t:{:.3f}ms".format(tsum/10)
print "'"+data[0]+"'" + tinfo,
pinfo = data[0] + '\n'
fdata.write(pinfo)
fdata.write(tinfo + "\n")
fdata.flush()
counter = counter + 1
print
#time.sleep(20.0)
fdata.close()
ser.close()