intro = \
"""
ReadSCPI_18c.py
ProfHuster
2018-02-05
This program collects data from an Arduino (or Arduino-clone) that uses
a SCPI-style command structure and saves it to a file.
Versions:
18c - Various mod’s including saving CONF? query to the data file
18b - Flushes stdout
"""
print intro
import serial, io, re, sys, time
from serialPorts import serial_ports
FILE_NAME_FORMAT = r"SCPI-%04d-%02d-%02d-%02d%02d.csv"
EOL = '\n'
TIMEOUT = 1.0
DO_PRINT = True
DO_PRINT = False
t = time.localtime()
fileName = FILE_NAME_FORMAT % (t.tm_year, t.tm_mon, t.tm_mday, \
t.tm_hour, t.tm_min)
print 'Opening file "%s"' % (fileName)
fpData = open(fileName, 'w')
while True:
comment = raw_input("Enter a comment (return to end): ")
if len(comment.strip()) == 0:
break
fpData.write("# " + comment.strip()+'\n')
fpData.flush()
ports = serial_ports()
print "The available ports are:"
for (i, port) in enumerate(ports):
print "%d, %s" % (i, port)
iPort = int(raw_input("Enter serial port number: "))
ser = serial.Serial(ports[iPort], 115200, timeout=TIMEOUT)
time.sleep(TIMEOUT)
line = ser.readline()
while len(line) > 0:
if DO_PRINT: print ">%s<" % (line.strip())
line = ser.readline()
print "Enter command line (help? for help)"
print "Enter 'q' to quit"
while True:
command = raw_input(": ")
if DO_PRINT: print "Command:%s:" % (command.strip())
if len(command)>0 and command[0].lower() == 'q':
break
command += EOL
if re.search(r'data?', command, flags=re.I):
ser.write('conf?\n')
line = ser.readline()
while len(line) > 0:
if DO_PRINT: print ">%s<" % (line.strip())
sys.stdout.flush()
re.sub(r'^', r'# ', line)
line = re.sub(r'$', r'', re.sub(r'<', '', re.sub(r'>', r'', re.sub(r'^', r'# ', line))))
fpData.write(line)
fpData.flush()
line = ser.readline()
ser.write(command)
if re.search(r'data?', command, flags=re.I):
line = ser.readline()
if DO_PRINT: print ">%s<" % (line.strip())
line = ser.readline()
while len(line) > 0:
if DO_PRINT: print ">%s<" % (line.strip())
sys.stdout.flush()
fpData.write(line.strip()+'\n')
fpData.flush()
line = ser.readline()
if not DO_PRINT: print "Done"
else:
line = ser.readline()
for line in ser.readlines():
lineStrip = line.strip()
print ">%s<" % (lineStrip)
ser.close()
fpData.close()