-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpy_serial_example.py
More file actions
132 lines (111 loc) · 3.61 KB
/
Copy pathpy_serial_example.py
File metadata and controls
132 lines (111 loc) · 3.61 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
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
import sys
import serial
import twseia
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Simple Serial to TaiSEIA home application example.',
epilog="""\
NOTE: no security measures are implemented.
Only one connection at once is supported. When the connection is terminated
it waits for the next connect.
""")
parser.add_argument(
'SERIALPORT',
help="serial port name")
parser.add_argument(
'BAUDRATE',
type=int,
nargs='?',
help='set baud rate, default: %(default)s',
default=9600)
group = parser.add_argument_group('serial port')
group.add_argument(
"--bytesize",
choices=[5, 6, 7, 8],
type=int,
help="set bytesize, one of {5 6 7 8}, default: 8",
default=8)
group.add_argument(
"--parity",
choices=['N', 'E', 'O', 'S', 'M'],
type=lambda c: c.upper(),
help="set parity, one of {N E O S M}, default: N",
default='N')
group.add_argument(
"--stopbits",
choices=[1, 1.5, 2],
type=float,
help="set stopbits, one of {1 1.5 2}, default: 1",
default=1)
group.add_argument(
'--rtscts',
action='store_true',
help='enable RTS/CTS flow control (default off)',
default=False)
group.add_argument(
'--xonxoff',
action='store_true',
help='enable software flow control (default off)',
default=False)
group.add_argument(
'--rts',
type=int,
help='set initial RTS line state (possible values: 0, 1)',
default=None)
group.add_argument(
'--dtr',
type=int,
help='set initial DTR line state (possible values: 0, 1)',
default=None)
args = parser.parse_args()
# connect to serial port
ser = serial.serial_for_url(args.SERIALPORT, do_not_open=True)
ser.baudrate = args.BAUDRATE
ser.bytesize = args.bytesize
ser.parity = args.parity
ser.stopbits = args.stopbits
ser.rtscts = args.rtscts
ser.xonxoff = args.xonxoff
if args.rts is not None:
ser.rts = args.rts
if args.dtr is not None:
ser.dtr = args.dtr
try:
ser.open()
except serial.SerialException as e:
sys.stderr.write('Could not open serial port {}: {}\n'.format(ser.description, e))
sys.exit(1)
# TaiSEIA spec. constants
SA_TYPE_ID = 4 # Dehumidifier
POWER_SERVICE_ID = 0x00
POWER_ON = 1
POWER_OFF = 0
# read device current power state
read_cmd = twseia.create_read_state_cmd(SA_TYPE_ID, POWER_SERVICE_ID)
ser.write(bytearray(read_cmd))
buffer = []
while not 0 < len(buffer) == buffer[0]:
recv = ser.read()
buffer += list(recv)
response = twseia.parsing_read_state_response(SA_TYPE_ID, buffer)
sys.stderr.write('read dev power state {}\n'.format(
'ON' if response.get('value') == POWER_ON else 'OFF'
))
# write power cmd to device
cmd_value = POWER_OFF if response.get('value') == POWER_ON else POWER_ON
write_cmd = twseia.create_write_state_cmd_from_txt(
SA_TYPE_ID, 'power', cmd_value
)
sys.stderr.write('write dev power state {}\n'.format(
'ON' if cmd_value == POWER_ON else 'OFF'
))
ser.write(bytearray(write_cmd))
buffer = []
while not 0 < len(buffer) == buffer[0]:
recv = ser.read()
buffer += list(recv)
response = twseia.parsing_read_state_response(SA_TYPE_ID, buffer)
sys.stderr.write('dev report power state {}\n'.format(
'ON' if response.get('value') == POWER_ON else 'OFF'
))