/Designs/Measuring_instruments/ISMS01A/SW/ISMS_read.py
7,6 → 7,8
import time
import datetime
import sys
import os
 
from pymlab import config
 
#### Script Arguments ###############################################
13,12 → 15,19
 
if len(sys.argv) != 3:
sys.stderr.write("Invalid number of arguments.\n")
sys.stderr.write("Usage: %s PORT_ADDRESS LOG_FILE.csv\n" % (sys.argv[0], ))
sys.stderr.write("Usage: %s PORT_ADDRESS SAMPLE_INTERVAL\n" % (sys.argv[0], ))
sys.exit(1)
 
 
 
port = eval(sys.argv[1])
log_file = sys.argv[2]
interval = eval(sys.argv[2])
 
if (interval<5) or (interval>3600):
sys.stderr.write("Invalid sample interval arguments.\n")
sys.stderr.write("The interval has to be in the range from 5 to 3600 seconds\n")
sys.exit(2)
 
#### Sensor Configuration ###########################################
 
cfg = config.Config(
41,39 → 50,54
cfg.initialize()
 
print "Current loop sensor example \r\n"
print "Time, channel #1, channel #2, channel #3 , channel #4 \r\n"
print "Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \r\n"
sensor1 = cfg.get_device("current_sensor1")
time.sleep(0.5)
sensor2 = cfg.get_device("current_sensor2")
#time.sleep(0.5)
 
#### Data Logging ###################################################
 
try:
with open(log_file, "a") as f:
while True:
try:
before = time.time()-interval
while True:
filename = time.strftime("%Y%m%d%H", time.gmtime())+".csv"
now = time.time()
if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
with open(filename, "a") as f:
 
sensor1.setADC(channel = 1, gain = 2, sample_rate = 3.75);
time.sleep(0.5)
channel1 = sensor1.readCurrent();
sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel1 = sensor1.readCurrent();
sensor1.setADC(channel = 2, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel2 = sensor1.readCurrent();
sensor1.setADC(channel = 3, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel3 = sensor1.readCurrent();
sensor1.setADC(channel = 4, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel4 = sensor1.readCurrent();
sensor2.setADC(channel = 1, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel5 = sensor2.readCurrent();
 
sensor1.setADC(channel = 2, gain = 1, sample_rate = 15);
time.sleep(0.5)
channel2 = sensor1.readCurrent();
sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \n" % (datetime.datetime.now().isoformat(), channel1, channel2, channel3, channel4, channel5))
 
sensor1.setADC(channel = 3, gain = 1, sample_rate = 15);
time.sleep(0.5)
channel3 = sensor1.readCurrent();
f.write("%d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n" % (time.time(), channel1, channel2, channel3, channel4, channel5))
f.flush()
 
sensor1.setADC(channel = 4, gain = 1, sample_rate = 15);
time.sleep(0.5)
channel4 = sensor1.readCurrent();
sys.stdout.flush()
before = time.time()
else:
time.sleep(0.1)
 
sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \n" % (datetime.datetime.now().isoformat(), channel1, channel2, channel3, channel4))
 
f.write("%d\t%d\t%d\t%d\t%d\n" % (time.time(), channel1, channel2, channel3, channel4))
f.flush()
 
sys.stdout.flush()
 
except KeyboardInterrupt:
f.close()
sys.exit(0)