-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialtest.py
47 lines (42 loc) · 1.01 KB
/
serialtest.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
import time
from serial import Serial
#class ReadThread(threading.Thread):
# print('initialize read thread..")
def connect():
#Initiate a connection
print("initializing..")
arduino = Serial('/dev/ttyACM0', 115200)
#Read confirmation from Arduino
print("Done initializing...")
arduino.flushInput()
arduino.flushOutput()
arduino.flush()
count = 0
try:
while True:
if count == 0:
print('about to write..')
arduino.write("H".encode('utf-8'))
print('wrote..')
count = count + 1
if arduino.inWaiting() > 0:
#read the message from the Arduino
print('about to read.')
time.sleep(0.2)
raw_message = str(arduino.read(1))
print('reading..')
#remove EOL characters from string
message = raw_message.rstrip()
print(message)
count = count + 1
if count >= 100:
count = 0
print ("count " + str(count))
except:
arduino.flushInput()
arduino.flushOutput()
arduino.flush()
arduino.close()
print('closed')
if __name__ == "__main__":
connect()