-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpair.py
More file actions
82 lines (66 loc) · 2.55 KB
/
pair.py
File metadata and controls
82 lines (66 loc) · 2.55 KB
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
#!/usr/bin/env python3
import argparse
import serial
import time
SERIAL_PORT = '/dev/ttyUSB0'
BAUD_RATE = 38400
DEVICES = {
'screen': {'unit_byte': 0x41, 'unit_code': 0x01},
'rolluik1': {'unit_byte': 0x42, 'unit_code': 0x02},
'rolluik2': {'unit_byte': 0x43, 'unit_code': 0x03},
}
def pair_updown(ser: serial.Serial, unit_byte: int, attempts: int = 3) -> None:
for attempt in range(1, attempts + 1):
packet = bytes([
0x09, 0x19, 0x15, 0x00,
0x10, 0x20, 0x30,
unit_byte,
0x03,
0x00,
])
ser.reset_input_buffer()
ser.write(packet)
ser.flush()
print(f"UPDOWN attempt {attempt}: {' '.join(f'{b:02X}' for b in packet)}")
time.sleep(0.35)
response = ser.read(10)
print(f"Response: {' '.join(f'{b:02X}' for b in response)}")
time.sleep(0.5)
def pair_position(ser: serial.Serial, unit_code: int, unit_byte: int, attempts: int = 2) -> None:
candidates = [unit_code, unit_byte]
for candidate in candidates:
for attempt in range(1, attempts + 1):
packet = bytes([
0x0C, 0x31, 0x00, 0x00,
0x10, 0x20, 0x30, 0x40,
candidate,
0x03,
0x00, 0x00, 0x00,
])
ser.reset_input_buffer()
ser.write(packet)
ser.flush()
print(f"POSITION unit 0x{candidate:02X} attempt {attempt}: {' '.join(f'{b:02X}' for b in packet)}")
time.sleep(0.35)
response = ser.read(10)
print(f"Response: {' '.join(f'{b:02X}' for b in response)}")
time.sleep(0.5)
def main() -> None:
parser = argparse.ArgumentParser(description='Pair Brel devices by mode.')
parser.add_argument('device', choices=DEVICES.keys(), help='Device to pair')
parser.add_argument('mode', choices=['updown', 'position', 'both'], help='Pairing mode')
args = parser.parse_args()
unit_byte = DEVICES[args.device]['unit_byte']
unit_code = DEVICES[args.device]['unit_code']
print('Put the motor in pairing mode first (P2 workflow).')
print('Stop Flask/service before pairing so serial port is free.')
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=2) as ser:
ser.flushInput()
if args.mode in ['updown', 'both']:
pair_updown(ser, unit_byte)
if args.mode == 'both':
time.sleep(0.8)
if args.mode in ['position', 'both']:
pair_position(ser, unit_code, unit_byte)
if __name__ == '__main__':
main()