-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialporttask.py
64 lines (52 loc) · 1.95 KB
/
serialporttask.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
import serial
import threading
class SerialPortThread(threading.Thread):
def __init__(self, input_queue, output_queue):
threading.Thread.__init__(self)
self.input_queue = input_queue
self.output_queue = output_queue
self.sp = serial.Serial()
def command(self, value):
method = getattr(self, value['method'], lambda: 'Invalid')
return method(value['args'])
def open(self, value):
try:
self.sp.port = value['port']
self.sp.baudrate = int(value['baudrate'])
self.sp.timeout = 1
self.sp.open()
except serial.serialutil.SerialException as err:
return "Port name not found."
#print("Serial port opened...")
return value["msg"]
def close(self, value):
self.sp.close()
#print("Serial port closed...")
return value["msg"]
def send(self, value):
if not self.sp.isOpen():
return "Not sending data, port is closed."
#print("Sending data...")
return value["msg"]
def writeSerial(self, data):
self.sp.write(bytes(data + "\n", "utf-8"))
def readSerial(self):
return self.sp.readline()
def run(self):
while True:
if self.sp.isOpen():
# look for incoming tornado request
if not self.input_queue.empty():
data = self.input_queue.get()
# send it to the serial device
self.writeSerial(data)
# look for incoming serial data
try:
if (self.sp.inWaiting() > 0):
data = self.readSerial()
# send it back to tornado
self.output_queue.put(data.decode("utf-8").replace("\n", ""))
except serial.SerialException:
pass
except Exception:
pass