-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserproxy.py
More file actions
executable file
·110 lines (96 loc) · 2.49 KB
/
Copy pathserproxy.py
File metadata and controls
executable file
·110 lines (96 loc) · 2.49 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
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
#!/usr/bin/python
from socket import *
from threading import Thread
import daemon
import logging
import serial
import time
import sys
import os
ser = None
allClients = []
BUFSIZ = 1024
serialDevice = ''
def serialReader():
global ser
global allClients
global serialDevice
while 1:
try:
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port=serialDevice,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# ser.open() # Doesn't seem to be needed
ser.isOpen()
logging.info('Serial port opened %s', ser)
while 1:
out = ser.read(1)
for client in allClients:
client.send(out)
except:
# Wait a while then try again
logging.debug('Error on serial port')
if ser is not None:
ser.close()
ser = None
time.sleep(10)
def handler(clientsock,addr):
global ser
global allClients
logging.info('connected from: %s', addr)
allClients.append(clientsock)
while 1:
data = clientsock.recv(BUFSIZ)
if not data:
break
if not (ser is None):
ser.write(data)
logging.debug('disconnected from: %s', addr)
allClients.remove(clientsock)
clientsock.close()
def mainProgram():
global serialDevice
global port
if len(sys.argv)>=4 and sys.argv[1] == '-p':
writePidFile(sys.argv[2])
del sys.argv[2]
del sys.argv[1]
if len(sys.argv) != 3:
print 'usage: ', sys.argv[0], ' device port'
exit()
logging.basicConfig(filename='/var/log/serproxy.log', filemode='w', format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG)
serialDevice=sys.argv[1]
port=int(sys.argv[2])
Thread(target=serialReader).start()
ser = None
allClients = []
listenAddr = ('', port)
try:
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(listenAddr)
serversock.listen(2)
logging.info('waiting for connection')
while 1:
clientsock, addr = serversock.accept()
Thread(target=handler, args=(clientsock, addr)).start()
except KeyboardInterrupt:
if not ser is None:
ser.close()
os._exit(0)
def writePidFile(pidfile):
pid = str(os.getpid())
f = open(pidfile, 'w')
f.write(pid)
f.close()
if __name__=='__main__':
if len(sys.argv)>=4 and sys.argv[1] == '-d':
del sys.argv[1]
with daemon.DaemonContext():
mainProgram()
else:
mainProgram()