Newer
Older
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
import serial
import serial.tools.list_ports
import time
SERIAL_NUMBER : str = "3307142A37323835191D33324B572D44"
# ser = serial.Serial("/dev/tty.usbmodem1101", 9600)
def find_device(serial_number:str) -> str:
"""Check the `hwid` through the list ports for a `SER` key
that matches `serial_number`
"""
for port, desc, hwid in serial.tools.list_ports.comports():
infos = hwid.split(" ")
for info in infos:
if not info.startswith("SER"):
continue
serial_num = info.split("=")[-1]
if not serial_num == serial_number:
break
return port
return ""
MY_PORT : str = find_device(SERIAL_NUMBER)
print(MY_PORT)
device = serial.Serial()
device.port = MY_PORT
device.timeout = 1
device.baudrate = 9600
device.open()
def write_value(valve_percent:float):
device.write(b'w')
data = str(valve_percent)
device.write(data.encode() + b'\n')
def read_value():
device.write(b'r')
time.sleep(0.1)
response = float(device.readline().decode().strip())
return response
def read_pressure():
device.write(b'p')
time.sleep(0.1)
response = float(device.readline().decode().strip())
return response
if __name__ == "__main__":
while True:
print("Enter valve opening percentage:")
valve_percent = input()
if float(valve_percent) < 0:
break
write_value(valve_percent)
print(read_value())
# print(read_pressure())
device.close()