Last active
June 5, 2023 10:03
-
-
Save bluecmd/ffe24368e919271137fb214821d91add to your computer and use it in GitHub Desktop.
Dicon GP700 GPIB Scanning script for I-matrix
This file contains hidden or 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/env python3 | |
import pyvisa | |
import subprocess | |
import re | |
import time | |
def optical_data(iface): | |
# no json support in ethtool for eeprom dump yet :( | |
for _ in range(10): | |
try: | |
odraw = subprocess.check_output(['sudo', '/home/bluecmd/ethtool/ethtool', '-m', iface]).decode() | |
except subprocess.CalledProcessError: | |
# retry if netlink is acting up | |
time.sleep(0.1) | |
continue | |
od = [[y.strip() for y in x.split(':', maxsplit=1)] for x in odraw.strip().split('\n')] | |
rcvr_dbm = [] | |
xmit_dbm = [] | |
for key, value in od: | |
g = re.search(r'\/ ([-0-9.]+) dBm', value) | |
if key.startswith('Rcvr signal avg optical power'): | |
rcvr_dbm.append(float(g.group(1)) if g else -40.0) | |
elif key.startswith('Transmit avg optical power'): | |
xmit_dbm.append(float(g.group(1)) if g else -40.0) | |
return round(sum(xmit_dbm)/len(xmit_dbm), 2), round(sum(rcvr_dbm)/len(rcvr_dbm), 2) | |
def generate_paths(src, dst, loop_list): | |
# loopback | |
yield 'Loopback', ((src, dst),) | |
# 1 hop | |
for port in loop_list: | |
yield f'Via {port}', ((src, port), (port, dst)) | |
# 2 hop | |
for port1 in loop_list: | |
for port2 in loop_list: | |
if port2 == port1: | |
continue | |
yield f'Via {port1} -> {port2}', ((src, port1), (port1, port2), (port2, dst)) | |
# 3 hop | |
for port1 in loop_list: | |
for port2 in loop_list: | |
if port2 == port1: | |
continue | |
for port3 in loop_list: | |
if port3 == port2 or port3 == port1: | |
continue | |
yield f'Via {port1} -> {port2} -> {port3}', ((src, port1), (port1, port2), (port2, port3), (port3, dst)) | |
if __name__ == '__main__': | |
rm = pyvisa.ResourceManager() | |
gp700 = rm.open_resource('GPIB0::30::INSTR') | |
id_mf, id_model, id_serial, id_fw = [x.strip() for x in gp700.query('*IDN?').strip().split(',')] | |
if id_mf != 'DiCon Fiberoptics Inc' or id_model != 'GP700': | |
raise Exception(f'Unsupported device: {id_mf}, {id_model}') | |
print(f' [*] Connected to {id_model}') | |
loop_list = range(4, 16) | |
for src, dst, iface in ((1, 1, 'ens1f0'), (2, 2, 'ens1f1')): | |
with open(f'{iface}.{int(time.time())}.results', 'w') as res: | |
print(f' [*] Starting scan session for {iface}') | |
for descr, path in generate_paths(src, dst, loop_list=loop_list): | |
for port, sel in path: | |
gp700.write(f'I{port} {sel}') | |
time.sleep(0.1) | |
time.sleep(2) | |
xmit, rcv = optical_data(iface) | |
loss_dbm = rcv - xmit | |
print(' [*] Scenario:', descr, '=> {0:0.2f} dBm'.format(loss_dbm)) | |
res.write(','.join((str(x[0]) for x in path)) + ' {0:0.2f}'.format(loss_dbm) + '\n') | |
res.flush() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment