]> jspc29.x-matter.uni-frankfurt.de Git - labtools.git/commitdiff
pt100: Use serialman in pt100_reader
authorPhilipp Klaus <philipp.klaus@gmail.com>
Fri, 14 Jul 2017 18:13:11 +0000 (20:13 +0200)
committerPhilipp Klaus <philipp.klaus@gmail.com>
Fri, 14 Jul 2017 18:13:11 +0000 (20:13 +0200)
sensors/pt100-board-python-package/pt100_board/pt100_reader.py
sensors/pt100-board-python-package/setup.py

index c6533f2c64aecbe298c6c0b938dd41a7ce5e42be..66276ee85cac3b80a8e4eb9a053da54deada26a8 100755 (executable)
@@ -4,13 +4,14 @@ import argparse
 import sys
 import time
 from datetime import datetime as dt
+from multiprocessing import Queue
 
 from pt100_board import PT100BoardDatagram
 
 try:
-    import serial
+    from serialman import SerialManager, NewlineChunker
 except ImportError:
-    sys.stderr.write("Could not import the PySerial module. Please install it first.\n")
+    sys.stderr.write("Could not import the serialman module. Please install it first.\n")
     sys.exit(1)
 
 def main():
@@ -26,31 +27,32 @@ def main():
         from matplotlib import pyplot as plt
         import random
 
-    ser = serial.Serial(args.device, baudrate=38400, timeout=0.1)
+    sm = SerialManager(args.device, baudrate=38400, timeout=0.01)
+    sm.set_chunker(NewlineChunker())
+    sm.start()
 
     log_n_plot_interval = 2.
     last_time = time.time()
     data = {'timestamps': [], 'sensor_values': {}}
     sensor_values = {}
+
     try:
         while True:
-            line = ser.readline()
-            if not line: continue
-            line = line.strip(b'\x00')
+            chunk = sm.in_queue.get()
+            dg = PT100BoardDatagram(chunk)
             try:
-                dg = PT100BoardDatagram(line)
+                dg = PT100BoardDatagram(chunk)
             except:
-                sys.stderr.write("Could not interpret this line: {}\n".format(repr(line)))
+                sys.stderr.write("Could not interpret this message: {}\n".format(repr(chunk)))
                 continue
             if dg.kind == 'temperature':
                 if dg.connected:
-                    line = '{}.{} {}\n'.format(dg.board, dg.sensor, dg.temperature)
                     sensor_values['{}.{}'.format(dg.board, dg.sensor)] = dg.temperature
                 else:
                     continue
             else:
                 continue
-            sys.stdout.write(line)
+            sys.stdout.write('{}.{} {}\n'.format(dg.board, dg.sensor, dg.temperature))
             sys.stdout.flush()
 
             now = time.time()
@@ -73,9 +75,11 @@ def main():
                     plt.legend()
                     plt.draw()
                     plt.pause(0.0001)
-
     except KeyboardInterrupt:
         sys.stderr.write("Ctrl-C pressed, exiting...\n")
-        sys.exit(1)
+        sm.close()
+    finally:
+        sm.close()
+    sm.join()
 
 if __name__ == "__main__": main()
index 46f9818073f4336ed42c2984d81aada2bfe6c344..4ecf7a253d271a66488ef86e03f9588fbdae4559 100644 (file)
@@ -10,7 +10,7 @@ setup(name='pt100-board',
       author='Philipp Klaus',
       author_email='klaus@physik.uni-frankfurt.de',
       packages=['pt100_board'],
-      install_requires=['PySerial'],
+      install_requires=['PySerial', 'serialman'],
       entry_points = {
         'console_scripts': [
           'pt100_config         = pt100_board.pt100_config:main',