#!/usr/bin/env python3 from time import sleep import click from tqdm import tqdm from thermocouples_reference import thermocouples from sacada.SACADA import SACADA typeR = thermocouples['R'] @click.group() @click.option('--port', default="/dev/ttyACM1", help='serial port location eg.: /dev/ttyACM0 (default)') @click.pass_context def cli(ctx, port): ctx.obj['s'] = SACADA(port) @cli.command() @click.pass_context def show(ctx): click.echo(ctx.obj['s'].identify()) @cli.command() @click.argument('channel') @click.pass_context def read(ctx, channel): click.echo(ctx.obj['s'].readVoltage(channel)) @cli.command() @click.argument('channel') @click.option('--interval', default=1000, help='interval bewteen readings (ms)') @click.option('--save', default='', help='file to save readings') @click.pass_context def monitor(ctx, channel, interval, save): #with tqdm(total=256, desc="Channel {}".format(channel), bar_format="{desc}|{bar}|{n_fmt}mV") as bar: #while True: # bar.n = int(ctx.obj['s'].readVoltage(channel) * 1000) # bar.refresh() # sleep(interval/1000.0) if not save: while True: click.echo(ctx.obj['s'].readVoltage(channel)) sleep(interval/1000.0) else: f = open(save, 'w') try: while True: voltage = ctx.obj['s'].readVoltage(channel) click.echo(voltage) f.write(str(voltage) + '\n') sleep(interval/1000.0) except KeyboardInterrupt: f.close() @cli.command() @click.option('--interval', default=1000, help='interval bewteen readings (ms)') @click.option('--save', default='', help='file to save readings') @click.pass_context def lapma(ctx, interval, save): #with tqdm(total=256, desc="Channel {}".format(channel), bar_format="{desc}|{bar}|{n_fmt}mV") as bar: #while True: # bar.n = int(ctx.obj['s'].readVoltage(channel) * 1000) # bar.refresh() # sleep(interval/1000.0) if not save: while True: click.echo(ctx.obj['s'].readVoltage(channel)) sleep(interval/1000.0) else: f = open(save, 'w') try: while True: voltage = ctx.obj['s'].readVoltage("A3") current = ctx.obj['s'].readVoltage("A4") thermocouple = ctx.obj['s'].readVoltage("TC") try: temperature = typeR.inverse_CmV((thermocouple + 0.001453125034) * -1000, Tref=25) except ValueError: temperature = "Error" click.echo("Voltage: {}".format(voltage)) click.echo("Current: {}".format(current)) click.echo("Power: {}".format(voltage * current)) click.echo("TC (mV): {}".format(thermocouple)) click.echo("TC (celsius): {}".format(temperature)) click.echo("-----------------") f.write(str(voltage) + '\t') f.write(str(current) + '\t') f.write(str(thermocouple) + '\t') f.write(str(temperature) + '\n') sleep(interval/1000.0) except KeyboardInterrupt: f.close() if __name__ == '__main__': cli(obj={})