-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialworker.py
More file actions
51 lines (40 loc) · 1.36 KB
/
serialworker.py
File metadata and controls
51 lines (40 loc) · 1.36 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
import serial
import time
import multiprocessing
# Change this to match your local settings
SERIAL_PORT = '/dev/ttyUSB0'
SERIAL_BAUDRATE = 115200
class SerialProcess(multiprocessing.Process):
def __init__(self, input_queue, output_queue):
multiprocessing.Process.__init__(self)
self.input_queue = input_queue
self.output_queue = output_queue
self.sp = serial.Serial(SERIAL_PORT, SERIAL_BAUDRATE, timeout=1)
def close(self):
self.sp.close()
def write_serial(self, data):
self.sp.write(data)
# time.sleep(1)
def read_serial(self):
return self.sp.readline().replace("\n", "")
def run(self):
self.sp.flushInput()
while True:
try:
self.loop()
except KeyboardInterrupt:
return
def loop(self):
time.sleep(.01)
# look for incoming tornado request
while not self.input_queue.empty():
data = self.input_queue.get()
# send it to the serial device
self.write_serial(data)
# print "writing to serial: " + data
# look for incoming serial data
if self.sp.inWaiting() > 0:
data = self.read_serial()
# print "reading from serial: " + data
# send it back to tornado
self.output_queue.put(data)