-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialServer.py
More file actions
29 lines (25 loc) · 901 Bytes
/
Copy pathSerialServer.py
File metadata and controls
29 lines (25 loc) · 901 Bytes
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
import zmq
import serial
from time import sleep
class SerialServer:
def __init__(self,port,device):
context = zmq.Context()
self.socket = context.socket(zmq.REP)
self.socket.bind("tcp://*:%d"%port)
self.serial = serial.Serial(device, 115200,timeout=0)
print("SerialServer listening at *:%d relaying to %s" % (port,device))
def loop(self):
while True:
msg = self.socket.recv()
# print("Processing %s" % msg.decode("utf-8").strip())
self.serial.write(msg)
sleep(0.2)
reply=self.serial.readline().strip()
self.socket.send(reply.encode())
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-p","--port")
parser.add_option("-d","--device")
(options,args)=parser.parse_args()
myServer = SerialServer(int(options.port),options.device)
myServer.loop()