Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Planck data

Views: 110
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
intro = \
4
"""
5
ReadSCPI_18c.py
6
ProfHuster
7
2018-02-05
8
9
This program collects data from an Arduino (or Arduino-clone) that uses
10
a SCPI-style command structure and saves it to a file.
11
12
Versions:
13
18c - Various mod’s including saving CONF? query to the data file
14
18b - Flushes stdout
15
"""
16
print intro
17
import serial, io, re, sys, time
18
from serialPorts import serial_ports
19
20
# The next line defines the file name format
21
# Note that the format automatically include the
22
# year-month-date-hour-minute. This is incredibly useful to help
23
# keep data files orgnized. The alphabetical order of the files
24
# is also the chrononlogical order.
25
FILE_NAME_FORMAT = r"SCPI-%04d-%02d-%02d-%02d%02d.csv"
26
EOL = '\n'
27
TIMEOUT = 1.0
28
DO_PRINT = True # print everything out as you go
29
DO_PRINT = False
30
31
# make file name and open output data file
32
t = time.localtime()
33
fileName = FILE_NAME_FORMAT % (t.tm_year, t.tm_mon, t.tm_mday, \
34
t.tm_hour, t.tm_min)
35
print 'Opening file "%s"' % (fileName)
36
fpData = open(fileName, 'w')
37
38
# Let user put in comments
39
while True:
40
comment = raw_input("Enter a comment (return to end): ")
41
if len(comment.strip()) == 0:
42
break
43
fpData.write("# " + comment.strip()+'\n')
44
fpData.flush()
45
46
# First get the name of and open the serial port
47
ports = serial_ports()
48
print "The available ports are:"
49
for (i, port) in enumerate(ports):
50
print "%d, %s" % (i, port)
51
iPort = int(raw_input("Enter serial port number: "))
52
#select serial port, baudrate (default = 9600), timeout (default = 1)
53
ser = serial.Serial(ports[iPort], 115200, timeout=TIMEOUT)
54
55
# wait for Arduino to boot and print any output
56
time.sleep(TIMEOUT)
57
58
# This code is used later also
59
# Read a line form Arduino
60
line = ser.readline()
61
# While line has some length. If length is zero, there is no more to read
62
while len(line) > 0:
63
if DO_PRINT: print ">%s<" % (line.strip())
64
line = ser.readline()
65
66
print "Enter command line (help? for help)"
67
print "Enter 'q' to quit"
68
# Loop until a break statement
69
while True:
70
# Get line to write
71
command = raw_input(": ")
72
if DO_PRINT: print "Command:%s:" % (command.strip())
73
# Check if a q for quit was entered
74
if len(command)>0 and command[0].lower() == 'q':
75
break
76
# Add a newline before sending command to Arduino
77
command += EOL
78
# If it is a data command, send config query and write response to the data file
79
if re.search(r'data?', command, flags=re.I):
80
ser.write('conf?\n')
81
line = ser.readline()
82
while len(line) > 0:
83
if DO_PRINT: print ">%s<" % (line.strip())
84
sys.stdout.flush()
85
re.sub(r'^', r'# ', line)
86
line = re.sub(r'$', r'', re.sub(r'<', '', re.sub(r'>', r'', re.sub(r'^', r'# ', line))))
87
fpData.write(line)
88
fpData.flush()
89
line = ser.readline()
90
ser.write(command)
91
92
# look for the data command. If found, save to the file
93
if re.search(r'data?', command, flags=re.I):
94
# Read and print first line
95
line = ser.readline()
96
if DO_PRINT: print ">%s<" % (line.strip())
97
# Now save the data output
98
line = ser.readline()
99
while len(line) > 0:
100
if DO_PRINT: print ">%s<" % (line.strip())
101
sys.stdout.flush()
102
fpData.write(line.strip()+'\n')
103
fpData.flush()
104
line = ser.readline()
105
if not DO_PRINT: print "Done"
106
else:
107
# skip one line
108
line = ser.readline()
109
for line in ser.readlines():
110
lineStrip = line.strip()
111
print ">%s<" % (lineStrip)
112
ser.close()
113
fpData.close()
114
115