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():
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()
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()
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',