-
Notifications
You must be signed in to change notification settings - Fork 2
/
LRFX0M20PD-samplecode.py
74 lines (64 loc) · 2.09 KB
/
LRFX0M20PD-samplecode.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# =============================================================================
# The sample code will use the pyserial lib. Please use the following command to install the required packages.
# $ sudo pip install pyserial
# =============================================================================
import serial.tools.list_ports
import sys
#List available COMport
ports = serial.tools.list_ports.comports()
portlist=[]
index=0
for port, desc, hwid in ports:
portlist.append(port)
print(str(index)+") "+desc+"|"+port)
index+=1
if portlist == []:
print("No serial ports detected")
sys.exit()
#Select COMport
portname=input("Please input the number of the desired port: \n")
try:
ser = serial.Serial(
port=portlist[int(portname)],\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0.5)
except Exception as e:
print(str(e))
sys.exit()
#Select functions
while True:
key=input("\nPlease enter the operation command:\n -s single measurement\n -o Laser ON\n -f Laser OFF\n -q Exit\n")
#single measurement
if key=='s':
ser.reset_input_buffer()
cmd=bytearray(b'\x80\x06\x02\x78')
ser.write(cmd)
data = ser.readline()
#Under the resolution setting of 1mm
if len(data)==11:
print('Module response: '+str(data))
distance=float(data[3:10].decode('ascii'))
print(str(distance)+"m")
#Under the resolution setting of 0.1mm
elif len(data)==12:
print('Module response: '+str(data))
distance=float(data[3:11].decode('ascii'))
print(str(distance)+"m")
#Laser ON
elif key=='o':
cmd=bytearray(b'\x80\x06\x05\x01\x74')
ser.write(cmd)
#Laser OFF
elif key=='f':
cmd=bytearray(b'\x80\x06\x05\x00\x75')
ser.write(cmd)
#Exit
elif key=='q':
ser.close()
sys.exit()
#Incorrect Input
else:
print("Invalid option. Please enter one of the above options.")