2021-02-22 17:43:48 +08:00
|
|
|
import socket
|
2021-03-09 12:40:51 +08:00
|
|
|
import asyncio
|
|
|
|
import argparse
|
|
|
|
import os
|
2021-10-06 14:58:52 +08:00
|
|
|
import datetime
|
2021-02-22 17:43:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RPSCPI:
|
2021-03-15 15:26:36 +08:00
|
|
|
def __init__(self, rp_name, rp_host, ch1_gain, ch2_gain):
|
2021-03-09 12:40:51 +08:00
|
|
|
self.name = rp_name
|
|
|
|
self.host = rp_host
|
2021-03-15 15:26:36 +08:00
|
|
|
self.ch1_gain = ch1_gain
|
|
|
|
self.ch2_gain = ch2_gain
|
2021-03-09 12:40:51 +08:00
|
|
|
|
|
|
|
def connect(self):
|
2021-02-22 17:43:48 +08:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2021-03-09 12:40:51 +08:00
|
|
|
self.sock.connect((self.host, 5000))
|
2021-02-22 17:43:48 +08:00
|
|
|
self.sock_f = self.sock.makefile()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.sock_f.close()
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
def sendmsg(self, msg):
|
|
|
|
self.sock.send(msg.encode() + b"\r\n")
|
|
|
|
|
|
|
|
def recvmsg(self):
|
|
|
|
return self.sock_f.readline().strip()
|
|
|
|
|
2021-03-09 12:40:51 +08:00
|
|
|
def trigger_prep(self):
|
|
|
|
self.sendmsg("ACQ:RST")
|
2021-03-15 15:26:36 +08:00
|
|
|
self.sendmsg("ACQ:SOUR1:GAIN {}".format(self.ch1_gain.upper()))
|
|
|
|
self.sendmsg("ACQ:SOUR2:GAIN {}".format(self.ch2_gain.upper()))
|
2021-02-22 17:43:48 +08:00
|
|
|
self.sendmsg("ACQ:START")
|
2021-03-09 12:40:51 +08:00
|
|
|
|
|
|
|
async def trigger(self):
|
2021-02-22 17:43:48 +08:00
|
|
|
self.sendmsg("ACQ:TRIG NOW")
|
|
|
|
while True:
|
|
|
|
self.sendmsg("ACQ:TRIG:STAT?")
|
|
|
|
if self.recvmsg() == "TD":
|
|
|
|
break
|
|
|
|
|
|
|
|
def get_data(self, channel):
|
|
|
|
self.sendmsg("ACQ:SOUR{}:DATA?".format(channel))
|
|
|
|
return self.recvmsg()[1:-1]
|
|
|
|
|
2021-03-09 12:40:51 +08:00
|
|
|
|
|
|
|
RP_IP_ADDRS = {
|
|
|
|
"creotech-1": "192.168.1.104",
|
|
|
|
"creotech-2": "192.168.1.105",
|
2021-05-13 10:27:16 +08:00
|
|
|
"mlabs-1": "rp-f05cc9",
|
|
|
|
"mlabs-2": "rp-f0612e",
|
2021-03-09 12:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def gather_trigger(rp_list):
|
|
|
|
await asyncio.gather(*[rp.trigger() for rp in rp_list])
|
|
|
|
|
|
|
|
|
2021-02-22 17:43:48 +08:00
|
|
|
def main():
|
2021-10-06 14:58:52 +08:00
|
|
|
# Get timestamp at script start
|
|
|
|
now = datetime.datetime.now().replace(
|
|
|
|
microsecond=0, # get rid of microseconds when printed
|
|
|
|
)
|
|
|
|
now_iso = now.isoformat(sep=' ')
|
|
|
|
|
2021-03-09 12:40:51 +08:00
|
|
|
parser = argparse.ArgumentParser(description="Data collection tool for Sayma DAC/TTL at RedPitaya")
|
|
|
|
parser.add_argument("dir", help="output directory", type=str)
|
2021-03-15 15:26:36 +08:00
|
|
|
parser.add_argument("rps_gains", metavar="RP_NAME:CH1_GAIN,CH2_GAIN",
|
|
|
|
help="choose LV (+-1V) gain or HV (+-20V) gain on each channel for RP named <RP_NAME>; "
|
|
|
|
"e.g. mlabs:LV,HV means LV for IN1 and HV for IN2 on RP mlabs;\n"
|
|
|
|
"<RP_NAME> must be one of: " + " ".join(list(RP_IP_ADDRS.keys())),
|
2021-03-09 12:40:51 +08:00
|
|
|
type=str, nargs='+')
|
|
|
|
parser.add_argument("--txt", help="save data as additional human-readable text files",
|
|
|
|
action="store_true")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-03-15 15:26:36 +08:00
|
|
|
gains = dict()
|
|
|
|
try:
|
|
|
|
for gain in args.rps_gains:
|
|
|
|
rp_name, ch1ch2_gains = gain.split(':')
|
|
|
|
ch1_gain, ch2_gain = ch1ch2_gains.split(',')
|
|
|
|
gains[rp_name] = ch1_gain, ch2_gain
|
|
|
|
if rp_name == "" or ch1_gain == "" or ch2_gain == "":
|
|
|
|
raise SyntaxError("Must specify IN1 and IN2 gains for each target RP.")
|
|
|
|
except ValueError:
|
|
|
|
raise SyntaxError("Must specify input gains in the format of: <RP_NAME>:<CH1_GAIN>,<CH2_GAIN>")
|
|
|
|
|
2021-03-09 12:40:51 +08:00
|
|
|
# Connect a socket to each RP
|
|
|
|
rps = []
|
2021-03-15 15:26:36 +08:00
|
|
|
for rp_name in gains:
|
|
|
|
rp = RPSCPI(rp_name, RP_IP_ADDRS[rp_name], *gains[rp_name])
|
2021-03-09 12:40:51 +08:00
|
|
|
rp.connect()
|
|
|
|
rps.append(rp)
|
|
|
|
|
2021-02-22 17:43:48 +08:00
|
|
|
try:
|
2021-03-09 12:40:51 +08:00
|
|
|
for rp in rps:
|
|
|
|
rp.trigger_prep()
|
|
|
|
|
|
|
|
asyncio.run(gather_trigger(rps))
|
|
|
|
|
|
|
|
for rp in rps:
|
|
|
|
y1_raw = rp.get_data(1)
|
|
|
|
y2_raw = rp.get_data(2)
|
2021-10-06 14:58:52 +08:00
|
|
|
print("Data collection started at {}.".format(now_iso))
|
2021-03-09 12:40:51 +08:00
|
|
|
|
|
|
|
with open(os.path.join(args.dir, 'rp_{}_y1_raw.bin'.format(rp.name)), 'wb') as f:
|
2021-10-06 14:58:52 +08:00
|
|
|
f.write("{}\n".format(now_iso).encode('utf-8'))
|
2021-03-09 12:40:51 +08:00
|
|
|
f.write(y1_raw.encode('utf-8'))
|
|
|
|
print("Succesfully written y1 raw data from RP {}.".format(rp.name))
|
|
|
|
with open(os.path.join(args.dir, 'rp_{}_y2_raw.bin'.format(rp.name)), 'wb') as f:
|
2021-10-06 14:58:52 +08:00
|
|
|
f.write("{}\n".format(now_iso).encode('utf-8'))
|
2021-03-09 12:40:51 +08:00
|
|
|
f.write(y2_raw.encode('utf-8'))
|
|
|
|
print("Succesfully written y2 raw data from RP {}.".format(rp.name))
|
|
|
|
|
|
|
|
if args.txt:
|
2021-10-06 14:37:53 +08:00
|
|
|
header = '''\
|
|
|
|
RedPitaya Oscilloscope Reading
|
|
|
|
|
|
|
|
RP Hostname: {} ({})
|
|
|
|
RP Channel: Input {} ({})
|
|
|
|
Date & Time: {}
|
|
|
|
------
|
|
|
|
'''
|
2021-03-09 12:40:51 +08:00
|
|
|
y1 = [float(i) for i in y1_raw.split(',')]
|
|
|
|
y2 = [float(i) for i in y2_raw.split(',')]
|
|
|
|
with open(os.path.join(args.dir, 'rp_{}_y1_raw.txt'.format(rp.name)), 'w') as f:
|
2021-10-06 14:37:53 +08:00
|
|
|
f.write(''.join([l.lstrip(' ') for l in header.format(
|
|
|
|
RP_IP_ADDRS[rp.name], rp.name, 1, ch1_gain.upper(), now_iso).splitlines(True)]))
|
2021-03-09 12:40:51 +08:00
|
|
|
for i in y1:
|
|
|
|
f.write(str(i) + '\n')
|
|
|
|
print("Succesfully written y1 human-readable data from {}.".format(rp.name))
|
|
|
|
with open(os.path.join(args.dir, 'rp_{}_y2_raw.txt'.format(rp.name)), 'w') as f:
|
2021-10-06 14:37:53 +08:00
|
|
|
f.write(''.join([l.lstrip(' ') for l in header.format(
|
|
|
|
RP_IP_ADDRS[rp.name], rp.name, 2, ch2_gain.upper(), now_iso).splitlines(True)]))
|
2021-03-09 12:40:51 +08:00
|
|
|
for i in y2:
|
|
|
|
f.write(str(i) + '\n')
|
|
|
|
print("Succesfully written y2 human-readable data from {}.".format(rp.name))
|
2021-02-22 17:43:48 +08:00
|
|
|
|
|
|
|
finally:
|
2021-03-09 12:40:51 +08:00
|
|
|
for rp in rps:
|
|
|
|
rp.close()
|
2021-02-22 17:43:48 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|