-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_comms_test.py
117 lines (100 loc) · 3.12 KB
/
arduino_comms_test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# import serial
# import time
#
# ser = serial.Serial('COM3', 112500)
# ser.write(str.encode('c'))
#
#
# while True:
# if(ser.inWaiting() > 0):
# recieved = ser.readline()
# print(recieved)
# print(recieved.decode("utf-8"))
# exit()
import struct
import serial
import time
ser = serial.Serial('COM3', 115200, timeout=0)
# var = input("Enter something: ")
# print(b'<')
# ser.write(str.encode(var))
# while 1:
# # try:
# # if ser.in_waiting > 3:
# # returnData = ser.read(4)
# # print(int.from_bytes(returnData, byteorder='little'))
# if ser.in_waiting > 0:
# returnData = ser.readline()
# if returnData.decode()[0] == '<':
# print("got it")
# # print(returnData)
# print(returnData.decode())
# # time.sleep(0.1)
# # print(returnData.decode("utf-8"))
# # print(str(returnData))
# # print(int.from_bytes(returnData, byteorder='little'))
# #
# # print(returnData[1:5])
# # print(int.from_bytes(returnData[1:5], byteorder='little'))
# #
# # decoded_bytes = float(returnData[0:len(returnData) - 2].decode("utf-8"))
# # print(decoded_bytes)
#
# # for byte in returnData:
# # print(byte)
# # print(returnData.decode())
# # print("")
# # print(str(returnData, 'utf-8', 'ignore'))
# # except:
# # print('Data could not be read')
startMarker = 60
endMarker = 62
def recvFromArduino():
global startMarker, endMarker
ck = ""
x = "z" # any value that is not an end- or startMarker
byteCount = -1 # to allow for the fact that the last increment will be one too many
# wait for the start character
while ord(x) != startMarker:
x = ser.read()
# save data until the end marker is found
while ord(x) != endMarker:
if ord(x) != startMarker:
ck = ck + x.decode("utf-8") # change for Python3
byteCount += 1
x = ser.read()
return (ck)
def main():
time.sleep(2)
ser.write("<1>".encode('utf-8'))
while True:
if ser.inWaiting() > 0:
try:
dataRecvd = recvFromArduino()
print("Reply Received " + dataRecvd)
except:
print("cannot read")
if __name__ == '__main__':
main()
# ser = serial.Serial('COM3', 112500, timeout=0)
# print("opened serial")
# message = "0\r\n"
# print(message)
# time.sleep(2)
# ser.write(str.encode(message))
# keepRunning = True
# foundData = False
# while keepRunning:
# try:
# returnData = ser.readline()
# if returnData != b'':
# print(returnData)
# # q.put(returnData)
# foundData = True
# else:
# if foundData == True:
# keepRunning = False
# time.sleep(0.1)
# except ser.SerialTimeoutException:
# print('Data could not be read')
# print("finishing comms")