-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialDataGateway.py
executable file
·70 lines (58 loc) · 1.61 KB
/
SerialDataGateway.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
65
66
67
68
69
70
#!/usr/bin/env python
'''
Created on November 20, 2010
@author: Dr. Rainer Hessmer
'''
import threading
import serial
from cStringIO import StringIO
import time
import rospy
def _OnLineReceived(line):
print(line)
class SerialDataGateway(object):
'''
Helper class for receiving lines from a serial port
'''
def __init__(self, port="/dev/ttyUSB0", baudrate=115200, lineHandler = _OnLineReceived):
'''
Initializes the receiver class.
port: The serial port to listen to.
receivedLineHandler: The function to call when a line was received.
'''
self._Port = port
self._Baudrate = baudrate
self.ReceivedLineHandler = lineHandler
self._KeepRunning = False
def Start(self):
self._Serial = serial.Serial(port = self._Port, baudrate = self._Baudrate, timeout = 1)
self._KeepRunning = True
self._ReceiverThread = threading.Thread(target=self._Listen)
self._ReceiverThread.setDaemon(True)
self._ReceiverThread.start()
def Stop(self):
rospy.loginfo("Stopping serial gateway")
self._KeepRunning = False
time.sleep(.1)
self._Serial.close()
def _Listen(self):
stringIO = StringIO()
while self._KeepRunning:
data = self._Serial.read()
if data == '\r':
pass
if data == '\n':
self.ReceivedLineHandler(stringIO.getvalue())
stringIO.close()
stringIO = StringIO()
else:
stringIO.write(data)
def Write(self, data):
info = "Writing to serial port: %s" %data
rospy.loginfo(info)
self._Serial.write(data)
if __name__ == '__main__':
dataReceiver = SerialDataGateway("/dev/ttyUSB0", 115200)
dataReceiver.Start()
raw_input("Hit <Enter> to end.")
dataReceiver.Stop()