-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarControl.py
71 lines (56 loc) · 1.41 KB
/
carControl.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
import time
import serial
serialComm = 0
is_inited = False
# start by using this function
def init_comm(port="COM3"):
global serialComm
global is_inited
try:
serialComm = serial.Serial("COM3", 9600, timeout=1)
time.sleep(2)
is_inited = True
except:
print("couldn't init")
is_inited = False
# this would be used to communicate with the car
def car_go(msg, show_response=False):
global serialComm
global is_inited
if not is_inited:
print("init communication!")
return
if isinstance(msg, int):
msg = str(msg)
serialComm.write(msg.encode())
time.sleep(0.1)
if show_response:
print(serialComm.readline().decode('ascii'))
# execute this at end of the program
def end_comm():
global serialComm
global is_inited
if not is_inited:
print("init communication!")
return
serialComm.close()
is_inited = False
#########################################
# read those test cases to #
# under stand the module a bit more #
# Thanks #
#########################################
def test0__():
init_comm()
car_go(6)
end_comm()
test0__()
# def test1__():
# init_comm()
# while True:
# mm = input("enter msg: ")
# car_go(mm) # ex : mm = "b"
# if mm == "end":
# break
# end_comm()
# test1__()