[e3ad44]: / Microcontroller_Data_Collection_Socket_Connection.py

Download this file

81 lines (55 with data), 1.7 kB

 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
import socket
import msvcrt
import csv
def kbfunc():
#this is boolean for whether the keyboard has been hit
x = msvcrt.kbhit()
if x:
#getch acquires the character encoded in binary ASCII
ret = msvcrt.getch()
else:
ret = False
return ret
l = []
packet = []
p = ""
tp = []
s = socket.socket()
s.bind(('0.0.0.0', 80 ))
s.listen(0)
record = input("Press R to start recording...")
if record == 'r' :
client, addr = s.accept()
carry = ''
try:
while True:
content = client.recv(1)
if len(content) ==0:
# print("heree?")
break
else:
#print(len(content))
temp = ''
content = content.decode("utf-8")
if content == '!':
tp =[]
p = ''
elif content == '@':
tp.append(p)
p = ''
tp.append('RA')
packet.append(tp)
print(tp)
elif content ==',':
tp.append(p)
p = ''
else:
p += content
except KeyboardInterrupt:
pass
client.close()
print(len(packet))
with open('FILE_NAME.csv','w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y", "gyro_z","label"])
writer.writerows(packet)