-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySerial.py
37 lines (30 loc) · 930 Bytes
/
mySerial.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
import Queue #, Empty
from threading import Thread
import serial
def _listMsgs(q):
try:
while True:
yield q.get_nowait()
except Queue.Empty:
raise StopIteration
class mySerial():
def __init__(self, parent=None):
self.ser = serial.Serial("COM6", 115200)
self.fifo = Queue.Queue()
self._on = True
self.th = Thread(target=self._th_read)
self.th.start()
def _th_read(self):
while self._on:
msg = self.ser.readline().rstrip('\n')
self.fifo.put(msg)
# print(msg)
def readMsg(self):
return list(_listMsgs(self.fifo))
def writeMsg(self, msg):
self.ser.write(msg + '\r')
def stop(self):
self.ser.close()
self._on = False
self.th.join()
print("Outing!!!")