commit old stuff from lab

This commit is contained in:
Silas Labor Zizou
2025-04-16 09:55:32 +02:00
28 changed files with 728 additions and 60 deletions

View File

@@ -0,0 +1,146 @@
classdef OptLaserN7714A < handle
% Minimal controller for Keysight N7714A tunable laser over VISA-LAN
% Methods: getLaserInfo, setPower, setWavelength
%
% Examples:
% L = OptLaserN7714A('TCPIP::192.168.0.50::INSTR');
% L.setWavelength(1550); % nm
% L.setPower(-3.0, true); % dBm, also turns output ON
% info = L.getLaserInfo();
properties (Access=private)
v % visadev handle
resource char % VISA resource string, e.g. 'TCPIP::...::INSTR'
end
methods
function obj = OptLaserN7714A(resource)
arguments
resource (1,:) char
end
obj.resource = resource;
% Robust connect with small retry loop
maxRetries = 3; pauseTime = 1.0; lastErr = [];
for k = 1:maxRetries
try
obj.v = visadev(obj.resource);
% Identify instrument (SCPI *IDN?)
writeline(obj.v, '*IDN?'); % SCPI common, identification
idn = strtrim(readline(obj.v)); %#ok<NASGU>
break
catch ME
lastErr = ME;
pause(pauseTime);
end
end
if isempty(obj.v) || ~isvalid(obj.v)
error('OptLaserN7714A:ConnectFailed', ...
'Could not connect to %s. Last error: %s', obj.resource, lastErr.message);
end
% Set power unit to dBm for SOURce tree to be explicit (optional)
% [:SOUR]:POWer:UNIT dBm (laser power unit)
% Ref: SOURce:POWer:UNIT. :contentReference[oaicite:0]{index=0}
try
writeline(obj.v, 'SOUR:POW:UNIT DBM');
catch
% non-fatal
end
end
function delete(obj)
if ~isempty(obj.v) && isvalid(obj.v); delete(obj.v); end
end
function setPower(obj, power_dBm, outputOn, channel)
arguments
obj
power_dBm (1,1) double
outputOn (1,1) logical = true
channel (1,1) double {mustBeMember(channel,0:4)} = 1
end
v = obj.v;
cmd = sprintf('SOUR%d:POW:LEV:IMM:AMPL %g DBM', channel, power_dBm);
writeline(v, cmd);
if outputOn
writeline(v, sprintf('SOUR%d:POW:STAT 1', channel)); %SOUR1:POW:STAT 0
else
writeline(v, sprintf('SOUR%d:POW:STAT 0', channel));
end
writeline(v, '*OPC?');
a=readline(v);
end
function setWavelength(obj, wavelength_nm, channel)
arguments
obj
wavelength_nm (1,1) double {mustBePositive}
channel (1,1) double {mustBeMember(channel,0:4)} = 1
end
v = obj.v;
cmd = sprintf('SOUR%d:WAV %gNM', channel, wavelength_nm);
writeline(v, cmd);
writeline(v, '*OPC?'); readline(v);
end
function setDither(obj, enable, port)
% Enable or disable laser frequency dither (stabilization).
% enable = false no dither (FIX), true stabilization (STAB).
% port = 1..4 (default 1).
if nargin < 3
port = 1;
end
v = obj.v;
if enable
writeline(v, sprintf('SOUR%d:CONTrol:FCDither ON', port)); %[:SOURce[n]][:CHANnel[m]]:CONTrol:FCDither
else
writeline(v, sprintf('SOUR%d:CONTrol:FCDither OFF', port));
end
% optional confirmation
writeline(v, sprintf('SOUR%d:CONTrol:FCDither?', port));
modeStr = strtrim(readline(v));
fprintf('Port %d dither mode set to %s\n', port, modeStr);
end
function info = getLaserInfo(obj, port)
% Returns struct with IDN, output state, wavelength (nm), power (dBm)
% port = 1..4 (for N7714A with 4 outputs). Default = 1.
if nargin < 2
port = 1;
end
v = obj.v;
% *IDN?
writeline(v, '*IDN?');
idn = strtrim(readline(v));
% Output state: :SOURn:POW:STAT?
writeline(v, sprintf('SOUR%d:POW:STAT?', port));
out_state = str2double(readline(v)) ~= 0;
% Wavelength query (returns meters convert to nm)
writeline(v, sprintf('SOUR%d:WAV?', port));
wav_m = str2double(readline(v));
wav_nm = wav_m * 1e9;
% Power (set level) query (dBm)
writeline(v, sprintf('SOUR%d:POW:LEV:IMM:AMPL?', port));
p_dBm = str2double(readline(v));
info = struct('idn', idn, ...
'output_on', out_state, ...
'wavelength_nm', wav_nm, ...
'power_dBm', p_dBm);
end
end
end