Created
December 11, 2015 17:00
-
-
Save HappyCodingRobot/7ab8815f6e4fe672e0dd to your computer and use it in GitHub Desktop.
Reading data from Voltcraft VC506 multimeter for long-term measurements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
Read data from Voltcraft VC506 | |
and write to file or console | |
see: | |
https://pythonhosted.org/pyserial/ | |
''' | |
import serial | |
import time | |
import sys, argparse | |
def readlineCR(port): | |
rv = '' | |
while True: | |
ch = port.read() | |
rv += ch | |
if ch=='\r' or ch=='': | |
return rv | |
# check for command line arguments | |
#parser = argparse.ArgumentParser(description='Voltcraft VC506 log', formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser = argparse.ArgumentParser(description='Voltcraft VC506 log') | |
parser.add_argument('-p',nargs='?', default='/dev/ttyUSB0', help='set serial Port (default: %(default)s)') | |
parser.add_argument('-f',nargs='?', default='', help='file name for logfile, no logfile if empty') | |
parser.add_argument('-c',type=int,default=-1,help='number of measurements, stop with ctrl+c (default: inf)') | |
parser.add_argument('-d',type=float,default=1,help='delay between masurements in seconds (default: %(default)d)') | |
parser.add_argument('-ts',action='store_true',help='add time stamp to measurement') | |
parser.add_argument('-nh',action='store_true',help='do not print header text') | |
parser.add_argument('-cs',action='store_true',help='use comma seperator') | |
args = parser.parse_args() | |
# setting the parameter | |
PORT = args.p | |
NUM_MEAS = args.c | |
NO_HEADER = args.nh | |
USE_SEP = args.cs | |
USE_TS = args.ts | |
OUTFILE = args.f.strip() | |
if args.d < 0.25: | |
TIME_STEP = 0.25 | |
else: | |
TIME_STEP = args.d | |
ser = serial.Serial(PORT, baudrate=1200, bytesize=7, parity='N', stopbits=1, timeout=2.0) | |
ser.flushInput() | |
ser.flushOutput() | |
if ser.isOpen(): | |
if not NO_HEADER: | |
print 'Reading from port '+PORT+' \n' | |
else: | |
print '!error, port not opened' | |
sys.exit(3) | |
# building the header | |
if NO_HEADER: | |
meas = [] | |
else: | |
if USE_TS: | |
meas = ['Time Range Value Unit'] | |
else: | |
meas = ['Range Value Unit'] | |
if USE_SEP: | |
meas[0].replace(' ',',') | |
print meas[0] | |
# running the measurement | |
st = time.time() | |
try: | |
while(NUM_MEAS): | |
if (NUM_MEAS > 0): | |
NUM_MEAS -= 1 | |
et = time.time() | |
ser.write('\r') | |
val=readlineCR(ser) | |
if val=='': | |
sys.exit(2) | |
if USE_TS: | |
meas.append('%0.2f ' %(et-st)+val.strip()) | |
else: | |
meas.append(val.strip()) | |
if USE_SEP: | |
meas[-1].replace(' ',',') | |
print meas[-1] | |
time.sleep(TIME_STEP) | |
except (KeyboardInterrupt): | |
if not NO_HEADER: | |
print '\nstopping..' | |
except (SystemExit): | |
print '\n! no data, connection lost?\nstopping..' | |
except Exception, e: | |
print '! some error' | |
ser.close() | |
if not ser.isOpen(): | |
if not NO_HEADER: | |
print 'Port '+PORT+' closed.' | |
else: | |
print '! closing error' | |
if OUTFILE != '': | |
with open(OUTFILE, 'w') as myof: | |
for e in meas: | |
myof.write(e+'\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment