ports = serial.tools.list_ports.comports(
include_links
=
False
)
for
port
in
ports :
(
'Find port '
+ port.device)
ser = serial.Serial(port.device)
if
ser.isOpen():
ser.close()
ser = serial.Serial(port.device,
9600
,
timeout
=
1
)
ser.flushInput()
ser.flushOutput()
(
'Successfully Connected to '
+ ser.name)
while
True
:
try
:
data = ser.read()
if
data ==
b
'
\x54
'
:
raw = ser.read(
3
)
dist =
int
((
list
(raw)[
0
] <<
8
) |
list
(raw)[
1
])
full_status =
list
(raw)[
2
]
status = full_status & status_mask
qf = full_status & quality_factor_mask
if
(status ==
0x
00
):
status_response =
"Reading status: No error"
elif
(status ==
0x
04
):
status_response =
"Reading status: Minimum Range"
elif
(status ==
0x
08
):
status_response =
"Reading status: Out of Range"
elif
(status ==
0x
0C
):
status_response =
"Reading status: ambient lightning high"
elif
(status ==
0x
10
):
status_response =
"Reading status: invalid reading"
if
(qf ==
0x
C0
):
qf_response =
"QF is 90-100%"
elif
(qf ==
0x
A0
):
qf_response =
"QF is 80-90%"
Copyright © Terabee 2022
Terabee, 90 rue Henri Fabre
01630 Saint-Genis-Pouilly, France (next to CERN)
15 / 17