|
@@ -0,0 +1,25 @@
|
|
|
|
+import serial
|
|
|
|
+import io
|
|
|
|
+import time
|
|
|
|
+
|
|
|
|
+class my_serial():
|
|
|
|
+ def __init__(self, port, baud_rate):
|
|
|
|
+ self.serial = serial.Serial(port, baud_rate, timeout=1)
|
|
|
|
+ self.serial_io_wrapped = io.TextIOWrapper(io.BufferedRWPair(self.serial, self.serial))
|
|
|
|
+
|
|
|
|
+ # receive message via serial
|
|
|
|
+ def read(self):
|
|
|
|
+ read_out = None
|
|
|
|
+ timeout = time.time() + 0.1
|
|
|
|
+ while ((self.serial.in_waiting > 0) and (timeout > time.time())):
|
|
|
|
+ pass
|
|
|
|
+ if self.serial.in_waiting > 0:
|
|
|
|
+ read_out = self.serial_io_wrapped.readline()
|
|
|
|
+ return read_out
|
|
|
|
+
|
|
|
|
+ser = my_serial('/dev/ttyUSB0', 9600)
|
|
|
|
+
|
|
|
|
+while True:
|
|
|
|
+ incomingMessage = ser.read()
|
|
|
|
+ if incomingMessage is not None:
|
|
|
|
+ print(incomingMessage)
|