-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
140 lines (120 loc) · 4.27 KB
/
server.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import serial
import socket
import queue
import sys
import threading
class serialConnect:
comPort = 'COM8'
baudrate = 115200
myserial = serial.Serial(comPort, baudrate)
def serial_run(self):
# self.comPort = input('Comport: ')
try:
if not self.myserial.isOpen():
self.myserial.open()
else:
print('Port is already open!' )
except IOError as e:
print('Error: ', e)
def serial_stop(self):
self.myserial.close()
def serial_read(self):
data = self.myserial.read(16)
data.decode('UTF-8')
return data
def serial_write(self, data):
data += '\n' #the arduino needs a \n after each command.
data_bytes = data.encode('UTF-8')
self.myserial.write(data_bytes)
print('send data: ', data_bytes)
class socketServer:
host = ''
port = 5555
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.setblocking(1)
data_queue = queue.Queue(1)
def __init__(self):
try:
self.soc.bind((self.host, self.port))
except:
print('Bind error: ', sys.exc_info())
self.soc.listen(5)
def socket_accept_thread(self):
while True:
try:
print('Waiting for a new connection')
conn, addr = self.soc.accept()
client_thread = threading.Thread(target=self.threaded_client, args=(conn, self.data_queue))
client_thread.daemon = True
client_thread.start()
except:
print('Accept thread Error: ', sys.exc_info())
def threaded_client(self, conn, data_queue):
# conn.send(str.encode('welcome, type your info \n'))
try:
while True:
data = conn.recv(2048)
if not data:
print('A connection closed.')
break
# reply = 'server output: ' + data.decode('UTF-8')
data_queue.put(data.decode('UTF-8'))
print("Items in queue: ", data_queue.qsize()) #items in queue should never be > 1!!
# conn.sendall(str.encode(reply))
print("Received data in threaded_client: ", data.decode('UTF-8'))
except:
print("Error: ", sys.exc_info())
conn.close()
def get_data(self):
data = self.data_queue.get()
return data
#Check received data.
def data_check(self, input_data):
#data must be in this form <,1,2,3,4,> anything else is wrong.
#if correct data has been found, it wil be returned as a string, else it returns False
state = 0
buffer = ''
count = 0
input_data = input_data.split(',')
return_data = False
for i in input_data:
if state == 0 and i == '<':
print('state 0')
buffer += ('<')
state += 1
elif state == 1 and i != '<':
print('state 1')
buffer += (',' + i)
if i == '>':
print('Data received correctly')
return_data = buffer
state += 1
elif count >= (len(input_data) - 1) and i != '>':
print('No end byte found')
return_data = False
elif state == 1 and i == '<':
return_data = False
break
elif count >= (len(input_data) - 1) and i != '>' and state == 0:
print('No end byte found')
return_data = False
elif state == 2:
break
return return_data
def Main():
server = socketServer()
arduino_conn = serialConnect()
accept_thread = threading.Thread(target=server.socket_accept_thread)
while True:
if not accept_thread.is_alive():
accept_thread.daemon = True
accept_thread.start()
arduino_conn.serial_run()
data_received = server.get_data()
arduino_conn.serial_write(data_received)
arduino_conn.serial_stop()
print('-------------------------------- \n')
#Features to be added
#Dilluting blue color between set times.
if __name__ == '__main__':
Main()