-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmbus_set_address.py
54 lines (44 loc) · 1.55 KB
/
mbus_set_address.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
import sys, time
serial_port = input("Please enter serial port (default: /dev/ttyUSB0): ") or "/dev/ttyUSB0"
baud_rate = int(input("Please enter baud rate (default: 2400): ") or "2400")
print ("Trying to connect on "+str(serial_port)+" "+str(baud_rate))
sys.path.append('/opt/openenergymonitor/emonhub/src')
from interfacers import *
mbus = EmonHubMBUSInterfacer.EmonHubMBUSInterfacer("MBUS",serial_port,False,False,baud_rate)
old_address = int(input("Please enter current address (default: 254): ") or "254")
new_address = int(input("Please enter new address (default: 1): ") or "1")
print ("Sending command to check meter at address "+str(old_address))
mbus.mbus_short_frame(old_address, 0x40)
time.sleep(1.0)
reply = False
while mbus.ser.in_waiting:
val = ord(mbus.ser.read(1))
if val==229:
print("ACK")
reply = True
if not reply:
print("no reply received")
sys.exit(0)
print ("Sending command to change meter on address "+str(old_address)+" to address "+str(new_address))
mbus.mbus_set_address(old_address, new_address)
time.sleep(1.0)
reply = False
while mbus.ser.in_waiting:
val = ord(mbus.ser.read(1))
if val==229:
print("ACK")
reply = True
if not reply:
print("no reply received")
sys.exit(0)
print ("Sending command to check meter at address "+str(new_address))
mbus.mbus_short_frame(new_address, 0x40)
time.sleep(1.0)
reply = False
while mbus.ser.in_waiting:
val = ord(mbus.ser.read(1))
if val==229:
print("ACK")
reply = True
if not reply:
print("no reply received")