From 45500206ad3a59b19714c73749248a0fc9727b2a Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Fri, 2 Jun 2017 16:20:48 +0200 Subject: [PATCH] Pt100: README and pt100_config tool added --- sensors/pt100-board-python-package/README.md | 38 +++++++ .../pt100_board/pt100_config.py | 106 ++++++++++++++++++ sensors/pt100-board-python-package/setup.py | 3 +- 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 sensors/pt100-board-python-package/README.md create mode 100755 sensors/pt100-board-python-package/pt100_board/pt100_config.py diff --git a/sensors/pt100-board-python-package/README.md b/sensors/pt100-board-python-package/README.md new file mode 100644 index 0000000..21774f5 --- /dev/null +++ b/sensors/pt100-board-python-package/README.md @@ -0,0 +1,38 @@ +# Python tools to work with the Pt100 board + +This python package installs a couple of command line tools: + +* `pt100_config` +* `pt100_reader` +* `pt100_csv_reader` +* `pt100_logfile_reader` + +## Changing the configuration + +The `pt100_config` script is particularly interesting for changing the board's configuration. +The settings are stored in the EEPROM. + +* On the address 0x0 - 0x7 the channel calibration values are stored (typical value ~ 0x57) +* At 0x8, the nominal offeset is stored (typical value 0xffff) +* At 0x9, the gain current is stored (typcial value 0xd211) +* At 0xa, the measurement period is stored. +* At 0xb, the uselcd flag is stored (0/1). Use 0 for high speed measurements, as the lcd update routine takes quite some time +* At 0xc, the averaging parameter is stored. Use 0x0 to disable averaging (and send out data at the measurment frequency). + +Some examples for writing to the config register: + + # set the register at 0xa (measurement period) to 500 (= 1000ms) + pt100_config /dev/ttyUSB0 --write-eeprom 0xa 500 + # set the register at 0xb (uselcd) to 0x0 (= off): + pt100_config /dev/ttyUSB0 --write-eeprom 0xb 0x0 + +To read back a value from EEPROM, use: + + pt100_config /dev/ttyUSB0 --read-eeprom 0xb + +## Installation + +Change into this directory and type: + + pip install --upgrade . + diff --git a/sensors/pt100-board-python-package/pt100_board/pt100_config.py b/sensors/pt100-board-python-package/pt100_board/pt100_config.py new file mode 100755 index 0000000..796439c --- /dev/null +++ b/sensors/pt100-board-python-package/pt100_board/pt100_config.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python + +import argparse +import sys +import time + +from pt100_board import PT100BoardDatagram + +try: + import serial +except ImportError: + sys.stderr.write("Could not import the PySerial module. Please install it first.\n") + sys.exit(1) + +REGISTER_NAMES = { + 0x0: 'channel 0 offset', + 0x1: 'channel 1 offset', + 0x2: 'channel 2 offset', + 0x3: 'channel 3 offset', + 0x4: 'channel 4 offset', + 0x5: 'channel 5 offset', + 0x6: 'channel 6 offset', + 0x7: 'channel 7 offset', + 0x8: 'nominal offset', + 0x9: 'gain current', + 0xa: 'period', + 0xb: 'uselcd', + 0xc: 'average', +} + +def parse_uint_tuple(text): + return tuple(parse_uint(el) for el in text.split(',')) + +def parse_uint(text): + text = text.lower() + if text.startswith('0x'): + return int(text, 16) + else: + return int(text) + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('device', help='The serial port to open') + parser.add_argument('--board-id', '-b', type=int, default=0, help='Board ID') + #parser.add_argument('--write-eeprom', '-w', help='') + parser.add_argument('--read-eeprom', type=parse_uint, help='Read EEPROM Address') + parser.add_argument('--write-eeprom', type=parse_uint, metavar=('ADDR','VALUE'), nargs=2, help='write uint16 value to address in eeprom; first arg: ADDR, second: VALUE') + parser.add_argument('--period', type=float, help='Set conversion period in ms') + parser.add_argument('--average', type=parse_uint, help='Number of temperature values to average over (1 to 16, reduces the output frequency).') + parser.add_argument('--reload-configuration', '-r', action='store_true', help='Reload the configuration of the board') + args = parser.parse_args() + + ser = serial.Serial(args.device, baudrate=38400, timeout=0.1) + + def send(cmd): + print('Sending command:', cmd.strip()) + ser.write(cmd.encode('ascii')) + + def write_register(board_id, address, value): + print('Writing 0x{2:04X} = {2:d} to address 0x{1:02X} of board 0x{0:01X}'.format(board_id, address, value)) + send('WE{:1X}0{:02X}{:04X}\n'.format(board_id, address, value)) + + def read_eeprom(board_id, address): + send('WG{:1X}0{:02X}0000\n'.format(board_id, address)) + start = time.time() + print('---') + answer = False + while (time.time() - start) < 0.3: + recv = ser.readline() + if recv: + recv = recv.strip(b'\x00').decode('ascii').strip() + if not recv.startswith('AG'): continue + answer = True + print('Received answer:', recv) + addr = int(recv[4:6], 16) + val = int(recv[6:10], 16) + print('Value stored at address 0x{:02X}: 0x{:04X}'.format(addr, val)) + if addr in REGISTER_NAMES: + print('Register name:', REGISTER_NAMES[addr]) + break + if not answer: print('No answer received so far. Try again?') + + if args.read_eeprom is not None: + read_eeprom(args.board_id, args.read_eeprom) + + if args.write_eeprom is not None: + write_register(args.board_id, *args.write_eeprom) + + if args.average is not None: + address = 0xc + args.average = max( 1, args.average) + args.average = min(16, args.average) + value = args.average - 1 + write_register(args.board_id, address, value) + + if args.period is not None: + address = 0xa + scaling = 2.0 + value = int(round(args.period / scaling)) + print('Setting the period to %d ms' % (value * scaling)) + write_register(args.board_id, address, value) + + if args.reload_configuration: + send('WR{:1X}0000000\n'.format(args.board_id)) + +if __name__ == "__main__": main() diff --git a/sensors/pt100-board-python-package/setup.py b/sensors/pt100-board-python-package/setup.py index 200604c..46f9818 100644 --- a/sensors/pt100-board-python-package/setup.py +++ b/sensors/pt100-board-python-package/setup.py @@ -3,7 +3,7 @@ try: except ImportError: from distutils.core import setup -setup(name='pt100_board', +setup(name='pt100-board', version='0.2', description='Utilities to work with the Pt100 Reader Board', url='', @@ -13,6 +13,7 @@ setup(name='pt100_board', install_requires=['PySerial'], entry_points = { 'console_scripts': [ + 'pt100_config = pt100_board.pt100_config:main', 'pt100_reader = pt100_board.pt100_reader:main', 'pt100_csv_reader = pt100_board.pt100_csv_reader:main', 'pt100_logfile_reader = pt100_board.pt100_logfile_reader:main', -- 2.43.0