-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_server.py
55 lines (44 loc) · 1.1 KB
/
socket_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
import socket
import sys
import serial
HOST = '' # Symbolic name, meaning all available interfaces
PORT = 22188 # Arbitrary non-privileged port
try:
ftdi_file = "/dev/ttyUSB0"
ftdi = serial.Serial(ftdi_file, 38400)
print ('FTDI detected')
except Exception:
print ('FTDI not detected')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print ('Socket created')
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print ('Socket bind complete')
#Start listening on socket
s.listen(10)
print ('Socket now listening')
conn, addr = s.accept()
print ('Connected with ' + addr[0] + ':' + str(addr[1]))
while 1:
cmd = b''
while not cmd:
cmd = conn.recv(1)
val = b''
while not val:
val = conn.recv(1)
cmd, val = chr(cmd[0]), val[0]
# Close socket command
if cmd=='C':
break;
string = "{0}{1}".format(cmd,val*4)
# Send to USB, or not if unplugged
try:
ftdi.write(string.encode())
except Exception:
pass
s.close()