Skip to content

Instantly share code, notes, and snippets.

@peisenhower
Last active March 30, 2017 23:29
Show Gist options
  • Save peisenhower/58af2200e70b7ab396654a6f16737730 to your computer and use it in GitHub Desktop.
Save peisenhower/58af2200e70b7ab396654a6f16737730 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# python 3
from serial import Serial
import time
import argparse
import re
from threading import Thread
from timeit import default_timer as timer
SAMPLES_TO_AVERAGE = 8
TIME_BETWEEN_COMMANDS = 0.1
class Command():
"""docstring for Command"""
def __init__(self, cmd, delay, well=99):
self.cmd = cmd
self.delay = delay
self.well = well
def has_callback(self):
if b'sample' in self.cmd:
return True
else:
return False
def is_bias(self):
if b'well' in self.cmd:
return True
else:
return False
def is_well(self):
if b'bias' in self.cmd:
return True
else:
return False
sampleWellValue = [0] * 8
sampleWellCode = [0] * 8
sampleBiasValue = [0] * 8
sampleBiasCode = [0] * 8
setup = [
]
command_set = [
]
SAMPLES_TO_AVERAGE = 8
def build_commands():
for i in range(8):
string = bytearray('sample well {0} {1}'.format(i, SAMPLES_TO_AVERAGE), "utf-8")
c = Command(string, TIME_BETWEEN_COMMANDS, i)
command_set.append(c)
string = bytearray('sample bias {0} {1}'.format(i, SAMPLES_TO_AVERAGE), "utf-8")
c = Command(string, TIME_BETWEEN_COMMANDS, i)
command_set.append(c)
def runner():
com = Serial()
com.port = 'COM4'
com.baudrate = 115200
com.timeout = 0.1
com.open()
build_commands();
# setup
for c in setup:
handle_command(com, c)
# main commands
for c in command_set:
handle_command(com, c)
com.close()
def handle_command(com, command):
com.write(command.cmd)
# print(command.cmd.decode())
if command.has_callback():
response = parse_sample_response(com)
parse_response(response)
time.sleep(command.delay)
# clear out read buffer
com.read()
def parse_sample_response(command, line):
nameValue = line.split(',')
resultValue = 0
resultCode = 0
for x in nameValue:
pair = x.split(':')
name = pair[0].strip()
value = pair[1].strip()
if 'value' in name:
resultValue = float(value.strip())
if 'code' in name:
resultCode = int(value.strip())
if command.is_well:
sampleWellCode[command.well] = resultCode
sampleWellValue[command.well] = resultValue
if command.is_bias:
sampleWellCode[command.well] = resultCode
sampleWellValue[command.well] = resultValue
def wait_for_callback(com):
while True:
line = readline(com)
if b'response:sample' in line:
return line
def readline(com):
eol = b'\r\n'
leneol = len(eol)
line = bytearray()
while True:
c = com.read(1)
if c:
line += c
if line[-leneol:] == eol:
break
else:
break
if len(line) > 0:
print(line.decode())
return bytes(line)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='loop serial commands')
runner()
print('WellValue,' + ','.join(str(x) for x in sampleWellValue))
print('WellCode,' + ','.join(str(x) for x in sampleWellCode))
print('BiasValue,' + ','.join(str(x) for x in sampleBiasValue))
print('BiasCode,' + ','.join(str(x) for x in sampleBiasCode))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment