-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUS-100.py
43 lines (37 loc) · 980 Bytes
/
US-100.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
import serial
import time
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout = 1)
#ser = serial.Serial('COM3', 9600)
def get_input():
data = ser.read().hex()
data2 = ser.read().hex()
ser.flushInput()
hdata = int(data,base=16)
ldata = int(data2,base=16)
distance = (hdata*256+ldata)/10
return distance
def get_temp():
temp = ser.read().hex()
ser.flushInput()
tdata = int(temp,base=16)
ovtemp = tdata-45
return ovtemp
try:
while True:
ser.write(chr(0x55).encode())
time.sleep(0.005)
print("%f CM" %(get_input()))
ser.write(chr(0x50).encode())
time.sleep(0.005)
print("%f ℃" %(get_temp()))
time.sleep(1)
except ValueError:
print('获取信息超时')
ser.flushInput()
ser.flushOutput()
ser.close()
except KeyboardInterrupt:
print("按键终止")
ser.flushInput()
ser.flushOutput()
ser.close()