Commit f3433057 authored by Romain Baptiste Dominique Albert's avatar Romain Baptiste Dominique Albert
Browse files

Modification of the server/client for the use of different SignalHound...

Modification of the server/client for the use of different SignalHound instrument. Start of the implementation of the SM200C.
parent a70daeeb
# Version
import numpy as np
import Instruments.Drivers.SignalHound.api_sm as api
version = '1.0.0'
print('SM200C {}'.format(version))
class SM200C(object):
global version
def __init__(self, addr_host = '192.168.2.2', addr_device = '192.168.2.10', port=51665):
self.addr_host = addr_host
self.addr_device = addr_device
self.port = port
self.device = None
self.open_device()
print('End init SM200C')
def close_driver(self):
""" Close the driver. """
print('Close')
if self.device is not None:
api.smCloseDevice(self.device)
self.device = None
def open_device(self):
""" Open the driver. """
self.device = api.smOpenNetworkedDevice('192.168.2.2', '192.168.2.10', 51665)
def raise_exception(self):
raise Exception('Test')
def identify(self):
""" Return the device type, its serial number and the firmware version."""
info_device_serial = api.smGetDeviceInfo(self.device)
return 'Device type: '+info_device_serial['Device type']+\
', serial number: '+str(info_device_serial['Serial'])+\
', Firmware: '+api.smGetFirmwareVersion(self.device)
This diff is collapsed.
......@@ -10,9 +10,6 @@ import socket
from types import FunctionType
from IPLIST import IPList
from SIGNALHOUND import SignalHound
class SignalHoundClient(object):
"""
Communicates with a Signalhound Server.
......@@ -28,13 +25,20 @@ class SignalHoundClient(object):
# Obtain host ip and port
ip = IPList[id]
self.host = ip.split(":")[0]
self.port = int(ip.split(":")[1])
self.host = ip.split("::")[0]
self.port = int(ip.split("::")[1])
if id == 'SM200C':
from Instruments.Drivers.SignalHound.SM200C import SM200C as SignalHound
else:
from SIGNALHOUND import SignalHound
# Inherit methods from instrument class
methods = [x for x,y in SignalHound.__dict__.items() if
isinstance(y, FunctionType) and x[0] != "_"]
for m in methods:
# Get all args
sig = inspect.signature(SignalHound.__dict__[m])
args = str(sig).strip('()')
......@@ -70,7 +74,13 @@ class SignalHoundClient(object):
if not block: break
rec_data += block
return pickle.loads(rec_data)
result = pickle.loads(rec_data)
try:
if result['Traceback']:
raise Exception('The following Exception occurs in the server :\n'\
+result['Traceback'])
except (KeyError, TypeError):
return result
def close(self):
"""Dummy function"""
......
......@@ -6,6 +6,7 @@ import socketserver
import re
import pickle
import socket
import traceback
parser = argparse.ArgumentParser(description='Server for an instrument driver')
parser.add_argument('id', metavar='SH1',
......@@ -24,7 +25,7 @@ class Server(socketserver.StreamRequestHandler):
"""Handle incoming function calls"""
# Get command
data = self.rfile.readline().strip().decode('utf-8')
print(data)
# get name and arguments by using regular expressions
p = r'^(?P<function>[\w\d_\-]+)\((?P<args>[\w\s\?\-\,=.\[\]\'\"]*)\)$'
m = re.match(p, data).groupdict()
......@@ -33,7 +34,14 @@ class Server(socketserver.StreamRequestHandler):
if m['function'] in Instrument.__dir__():
command = 'Instrument.' + data
# Execute command
try:
resp = eval(command)
except Exception as e:
print('----------------------------------')
print('Error happened:')
print(traceback.format_exc())
self.wfile.write(pickle.dumps({'Traceback': traceback.format_exc()}))
else:
# Send response back to client
self.wfile.write(pickle.dumps(resp))
......@@ -47,12 +55,31 @@ if __name__ == '__main__':
print("Starting up server...")
print("To stop the server press Ctrl+C ")
print(args.id)
print(host)
print(port)
if args.id =='SM200C':
from Instruments.Drivers.SignalHound.SM200C import SM200C
## The parameter for the SM200C is fixed by the configuration of the network
Instrument = SM200C()
print("Driver connected to Signalhound:")
print(Instrument.identify())
else:
# Init driver
from SIGNALHOUND import SignalHound
Instrument = SignalHound(args.id)
print("Driver connected to Signalhound:")
print(Instrument.GetSerialNumber())
print("SA124B, serial number:"+str(Instrument.GetSerialNumber()))
# Listen to specified port
try:
with socketserver.TCPServer((host, port), Server) as server:
server.serve_forever()
except KeyboardInterrupt:
if args.id =='SM200C':
Instrument.close_driver()
else:
## Might useful to cleanly close the SA124B ?
pass
print('Server closed!')
import numpy as np
import Instruments.Drivers.SignalHound.api_sm as api
import matplotlib.pyplot as plt
import time
def compute_frequency(param_sweep):
return param_sweep['Start frequency']+param_sweep['Step frequency']*np.arange(param_sweep['Sweep size'], dtype=np.float64)
device = api.smOpenNetworkedDevice('192.168.2.2', '192.168.2.10', 51665)
print('---------- Device connected -----------')
print(api.smGetDeviceInfo(device))
print('Firmware: '+api.smGetFirmwareVersion(device))
print(api.smGetFullDeviceDiagnostics(device))
print(api.smGetSFPDiagnostics(device))
## Basics configuration
api.smSetPowerState(device, 'on')
api.smSetAttenuator(device, 0)
api.smSetRefLevel(device, 0)
api.smSetPreselector(device, 'on')
api.smSetReference(device, 'external')
api.smConfigure(device, 'sweep')
def simple_sweep_acquisition(plot=True):
print('--------------------------------------------------')
print('Simple sweep acquisition')
api.smSetSweepSpeed(device, 'auto')
api.smSetSweepStartStop(device, 1e9, 20e9)
api.smSetSweepCoupling(device, 100e3, 10e3)
api.smSetSweepDetector(device, 'average', 'power')
api.smSetSweepScale(device, 'dBm')
api.smSetSweepWindow(device, 'flat_top')
api.smSetSweepSpurReject(device, 'on')
api.smConfigure(device, 'sweep') # Mandatory to apply the parameters
param_sweep = api.smGetSweepParameters(device)
print('Param sweep: ')
print(param_sweep)
frequency = compute_frequency(param_sweep)
nbr_test = 10
init_time = time.time()
for i in range(nbr_test):
result = api.smGetSweep(device, 'average', param_sweep['Sweep size'])
print('Acquisiton time with single sweep: %f'%((time.time()-init_time)/nbr_test))
init_time = time.time()
api.smStartSweep(device, 0)
for i in range(1, nbr_test):
j = i%16
jm = (i-1)%16
if i < nbr_test:
api.smStartSweep(device, j)
result = api.smFinishSweep(device, jm, 'average', param_sweep['Sweep size'])
print('Acquisiton time with qeued sweep: %f'%((time.time()-init_time)/nbr_test))
if plot:
fig, ax = plt.subplots(1,1)
ax.plot(frequency, result['Average'])
def fast_sweep_acquisition(plot=True):
print('--------------------------------------------------')
print('Fast sweep acquisition')
api.smSetSweepSpeed(device, 'fast')
api.smSetSweepCenterSpan(device, 10e9, 160e6)
api.smSetSweepCoupling(device, 100e3)
api.smSetSweepDetector(device, 'average', 'power')
api.smSetSweepScale(device, 'dBm')
api.smSetSweepWindow(device, 'flat_top')
api.smSetSweepSpurReject(device, 'on')
api.smConfigure(device, 'sweep') # Mandatory to apply the parameters
param_sweep = api.smGetSweepParameters(device)
print('Param sweep: ')
print(param_sweep)
frequency = compute_frequency(param_sweep)
nbr_test = 10
init_time = time.time()
for i in range(nbr_test):
result = api.smGetSweep(device, 'average', param_sweep['Sweep size'])
print('Acquisiton time with single sweep: %f'%((time.time()-init_time)/nbr_test))
init_time = time.time()
api.smStartSweep(device, 0)
for i in range(1, nbr_test):
j = i%16
jm = (i-1)%16
if i < nbr_test:
api.smStartSweep(device, j)
result = api.smFinishSweep(device, jm, 'average', param_sweep['Sweep size'])
print('Acquisiton time with qeued sweep: %f'%((time.time()-init_time)/nbr_test))
if plot:
fig, ax = plt.subplots(1,1)
ax.plot(frequency, result['Average'])
def test_acquisition_spur(plot=True):
print('--------------------------------------------------')
print('Spur acquisition test')
api.smSetSweepSpeed(device, 'auto')
api.smSetSweepStartStop(device, 1e9, 20e9)
api.smSetSweepCoupling(device, 30e3, 10e3)
api.smSetSweepDetector(device, 'average', 'power')
api.smSetSweepScale(device, 'dBm')
api.smSetSweepWindow(device, 'flat_top')
api.smSetSweepSpurReject(device, 'on')
api.smConfigure(device, 'sweep') # Mandatory to apply the parameters
param_sweep = api.smGetSweepParameters(device)
print('Param sweep: ')
print(param_sweep)
frequency = compute_frequency(param_sweep)
nbr_test = 10
init_time = time.time()
for i in range(nbr_test):
result_on = api.smGetSweep(device, 'average', param_sweep['Sweep size'])
print('Acquisiton time with spur on: %f'%((time.time()-init_time)/nbr_test))
api.smSetSweepSpurReject(device, 'off')
api.smConfigure(device, 'sweep') # Mandatory to apply the parameters
nbr_test = 10
init_time = time.time()
for i in range(nbr_test):
result_off = api.smGetSweep(device, 'average', param_sweep['Sweep size'])
print('Acquisiton time with spur off: %f'%((time.time()-init_time)/nbr_test))
if plot:
fig, ax = plt.subplots(1,1)
ax.plot(frequency, result_on['Average'], label='Spur reject on')
ax.plot(frequency, result_off['Average'], label='Spur reject off')
ax.legend()
def sweep_average_min_max(plot=True):
print('--------------------------------------------------')
print('Sweep: comparison Average and Min/Max mode')
print('Notice that they are different only when the sweep time is use increase average.')
api.smSetSweepSpeed(device, 'auto')
api.smSetSweepStartStop(device, 1e9, 20e9)
api.smSetSweepCoupling(device, 100e3, 100e3, 1)
api.smSetSweepDetector(device, 'average', 'power')
api.smSetSweepScale(device, 'dBm')
api.smSetSweepWindow(device, 'flat_top')
api.smSetSweepSpurReject(device, 'on')
api.smConfigure(device, 'sweep') # Mandatory to apply the parameters
param_sweep = api.smGetSweepParameters(device)
print('Param sweep: ')
print(param_sweep)
frequency = compute_frequency(param_sweep)
result_average = api.smGetSweep(device, 'average', param_sweep['Sweep size'])
api.smSetSweepDetector(device, 'min/max', 'power')
api.smConfigure(device, 'sweep') # Mandatory to apply the parameters
result_min_max= api.smGetSweep(device, 'min/max', param_sweep['Sweep size'])
if plot:
fig, ax = plt.subplots(1,1)
ax.plot(frequency, result_average['Average'], label='Average')
ax.plot(frequency, result_min_max['Min'], label='Min')
ax.plot(frequency, result_min_max['Max'], label='Max')
ax.legend()
def real_time_simple_sweep(plot=True):
print('--------------------------------------------------')
print('Sweep: comparison Average and Min/Max mode')
print('Notice that they are different only when the sweep time is use increase average.')
api.smSetRealTimeCenterSpan(device, 10e9, 40e6)
api.smSetRealTimeRBW(device, 10e3)
api.smSetRealTimeDetector(device, 'average')
api.smSetRealTimeScale(device, 'dBm', 0, 200)
api.smSetRealTimeWindow(device, 'flat_top')
api.smConfigure(device, 'real_time') # Mandatory to apply the parameters
param_sweep = api.smGetRealTimeParameters(device)
print('Param sweep: ')
print(param_sweep)
frequency = compute_frequency(param_sweep)
init_time = time.time()
result_average = api.smGetRealTimeFrame(device, 'average', param_sweep['Sweep size'])
print('Time average: %f'%(time.time()-init_time))
print(result_average['Frame count'])
init_time = time.time()
result_average = api.smGetRealTimeFrame(device, 'average', param_sweep['Sweep size'])
print('Time average: %f'%(time.time()-init_time))
print(result_average['Frame count'])
init_time = time.time()
result_average = api.smGetRealTimeFrame(device, 'average', param_sweep['Sweep size'])
print('Time average: %f'%(time.time()-init_time))
print(result_average['Frame count'])
time.sleep(10.)
init_time = time.time()
result_average = api.smGetRealTimeFrame(device, 'average', param_sweep['Sweep size'])
print('Time average: %f'%(time.time()-init_time))
print(result_average['Frame count'])
init_time = time.time()
result_average = api.smGetRealTimeFrame(device, 'average', param_sweep['Sweep size'])
print('Time average: %f'%(time.time()-init_time))
print(result_average['Frame count'])
api.smSetRealTimeDetector(device, 'min/max')
api.smConfigure(device, 'real_time') # Mandatory to apply the parameters
init_time = time.time()
result_min_max = api.smGetRealTimeFrame(device, 'min/max', param_sweep['Sweep size'])
print('Time min/max: %f'%(time.time()-init_time))
print(result_average['Frame count'])
if plot:
fig, ax = plt.subplots(1,1)
ax.plot(frequency, result_average['Average'], label='Average')
ax.plot(frequency, result_min_max['Min'], label='Min')
ax.plot(frequency, result_min_max['Max'], label='Max')
ax.legend()
"""To activate the following test."""
#simple_sweep_acquisition(True)
#fast_sweep_acquisition(True)
#test_acquisition_spur(True)
#sweep_average_min_max(True)
real_time_simple_sweep(True)
api.smCloseDevice(device)
plt.show()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment