# -*- coding: utf-8 -*-
"""
Created on Mon Jan 31 08:55:42 2022
@author: Martin.Mihaylov
"""
import sys
import pyvisa as visa
[docs]
def OSA():
from Instruments_Libraries.AQ6370D import AQ6370D
# import vxi11
# rm = vxi11.list_devices()
# for _ in rm:
# try:
# OSA = AQ6370D(str(_))
# # InstrOSA = _
# OSA.Close()
# except (visa.VisaIOError):
# print('Serial Number dont match!')
# return AQ6370D(_)
return AQ6370D('169.254.58.101')
[docs]
def CoBrite():
from Instruments_Libraries.CoBrite import CoBrite
rm = visa.ResourceManager()
CP = 0
dataInst = []
for data in list(rm.list_resources()):
while CP == 0:
try:
CO = CoBrite(str(data))
CP = CO.Identification().split(';')[0]
if CP == 'COBRITE CBDX-SC-SC-NN-NN-FA, SN 22060011, F/W Ver 1.2.1(160), HW Ver 1.20':
CO.Close()
CP = 1
dataInst = str(data)
else:
CP = 0
except (visa.VisaIOError):
print('Wrong Instrument!')
else:
break
break
return CoBrite(str(dataInst))
[docs]
def SourceMeter():
from Instruments_Libraries.KEITHLEY2612 import KEITHLEY2612
rm = visa.ResourceManager()
KM = 0
dataInst = []
for data in list(rm.list_resources()):
while KM == 0:
try:
KA = KEITHLEY2612(str(data))
KM = KA.getIdn().split('\n')[0]
if KM == 'Keithley Instruments Inc., Model 2612, 1152698, 1.4.2':
KA.Close()
KM = 1
dataInst = str(data)
else:
KM = 0
except (visa.VisaIOError):
print('Wrong Instrument!')
else:
break
break
return KEITHLEY2612(str(dataInst))
[docs]
def PowerSupply():
from Instruments_Libraries.RD3005 import RD3005
from Instruments_Libraries.KA3005 import KA3005
from Instruments_Libraries.KA3005p import KA3005p
SerialNum = ['KORAD KA3005P V5.8 SN:03379314' , 'KORAD KA3005P V5.8 SN:03379289' , 'RND 320-KA3005P V2.0']
#Prnt all instruments connected to the COM-Ports.
#Needed to set later
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
COM_List = []
Port_ = None
for port, desc, hwid in sorted(ports):
# print("{}: {} [{}]".format(port, desc, hwid))
COM_List.append(port)
PowerInstr = 0
for data in list(COM_List):
while PowerInstr == 0:
try:
PS = RD3005(data)
PowerInstr = PS.getIdn().split("\n")[0]
if PowerInstr in SerialNum:
PowerInstr = 1
Port_ = data
break
else:
PowerInstr = 0
print("Scanning COM Ports for Instrument !")
except serial.SerialException as e:
#There is no new data from serial port
print("Scanning COM Ports for Instrument !")
except TypeError as e:
#Disconnect of USB->UART occured
print("Scanning COM Ports for Instrument !")
except visa.VisaIOError as e:
print("Scanning COM Ports for Instrument !")
except AttributeError:
pass
else:
break
break
CheckInstrName = None
CheckInstrName = PS.getIdn().split("\n")[0]
PS.Close()
return RD3005(data)
# if CheckInstrName in SerialNum:
# return RD3005(Port_)
# else:
# raise ValueError("Instrument is not Valid Power Supply!")
# if CheckInstrName in SerialNum:
# if data == SerialNum[0]:
# PS.Close()
# return KA3005(Port_)
# elif data == SerialNum[1]:
# PS.Close()
# return KA3005p(Port_)
# elif data == SerialNum[2]:
# PS.Close()
# return RD3005(Port_)
# =============================================================================
# Old Power Supply Connect Function
# =============================================================================
# def PowerSupply():
# from Instruments_Libraries.RD3005 import RD3005
# from Instruments_Libraries.KA3005 import KA3005
# from Instruments_Libraries.KA3005p import KA3005p
# SerialNum = ['KORAD KA3005P V5.8 SN:03379314' , 'KORAD KA3005P V5.8 SN:03379289' , 'RND 320-KA3005P V2.0', 'GW INSTEK,GPP-4323,SN:GEW840790,V1.17']
# #Prnt all instruments connected to the COM-Ports.
# #Needed to set later
# import serial.tools.list_ports
# ports = serial.tools.list_ports.comports()
# for port, desc, hwid in sorted(ports):
# print("{}: {} [{}]".format(port, desc, hwid))
# print(' ')
# print('Check See the COM Port #####')
# Com = input('Give COM number from the Power Supply: ')
# # PS = kd3005pInstrument('COM'+Com)
# PS = RD3005('COM'+Com)
# data = PS.getIdn()
# if data in SerialNum:
# if data == SerialNum[0]:
# PS.Close()
# return KA3005('COM'+Com)
# elif data == SerialNum[1]:
# PS.Close()
# return KA3005p('COM'+Com)
# elif data == SerialNum[2]:
# PS.Close()
# return RD3005('COM'+Com)
[docs]
def PowerMeter(index: int = 0):
"""
Auto-detect a connected Thorlabs PM100-series power meter and return a PM100D instance.
Parameters
----------
index : int
If multiple PM100 meters are connected, choose which one to open (0 = first found).
Raises
------
RuntimeError
If no PM100-series device is detected or we cannot open it.
IndexError
If 'index' is out of range.
"""
import re
from pyvisa.errors import VisaIOError
from Instruments_Libraries.PM100D import PM100D
rm = visa.ResourceManager()
matches = []
# Look at VISA instruments (USB TMC shows up as USB...::INSTR)
for res in rm.list_resources("?*::INSTR"):
if not res.startswith("USB"): # PM100D is typically USBTMC
continue
try:
with rm.open_resource(res) as inst:
inst.timeout = 500 # ms
# Try query, fall back to write+read for quirky backends
try:
idn = inst.query("*IDN?").strip()
except Exception:
inst.write("*IDN?")
idn = inst.read().strip()
except Exception:
continue # not a responsive device for SCPI/*IDN?; skip
# Identify Thorlabs PM100 family by ID string
if ("PM100" in idn):
# Try to obtain a serial without ever printing/logging it
m = re.search(r"::([A-Z]\d{6,})::", res) # e.g. ::P00XXXXX::
serial = m.group(1) if m else None
if serial is None:
m2 = re.search(r"(P\d{6,})", idn) # fallback: parse from IDN
serial = m2.group(1) if m2 else None
matches.append({"resource": res, "idn": idn, "serial": serial})
if not matches:
raise RuntimeError("No Thorlabs PM100-series power meter found via VISA.")
if not (0 <= index < len(matches)):
raise IndexError(f"index {index} out of range (found {len(matches)} device(s)).")
picked = matches[index]
# Prefer constructing by serial (keeps resource backends swappable)
if picked["serial"] is not None:
try:
return PM100D(picked["serial"])
except (VisaIOError, Exception):
pass
# Fall back to VISA resource name
try:
return PM100D(resource_name=picked["resource"])
except TypeError:
raise RuntimeError("Detected a Thorlabs PM100 but could not open it.")
[docs]
def LU1000():
from Instruments_Libraries.LU1000 import LU1000_Cband
return LU1000_Cband("USB")
[docs]
def SpecAnalyser():
from Instruments_Libraries.MS2760A import MS2760A
# Source = '127.0.0.1'
# Ports = visa.ResourceManager().list_resources(query='TCP?*')
# for i in range(len(Ports)):
# if Ports[i].split("::")[1] == Source:
# _ = Ports[i]
# else:
# pass
# return MS2760A(_)
return MS2760A('127.0.0.1')
[docs]
def SigGen():
from Instruments_Libraries.MG3694C import MG3694C
print('''
########### Set the correct network settings ###########
Follow the instrictions to set the network
addapter and ip. After you are done confurm
to continuen!
########### Set the correct network settings ###########
''')
print('\n')
conf = input('Are you finish yes/no: ')
if conf == 'yes':
return MG3694C('192.168.0.254')
print('Instrument Connected as SG')
else:
pass
[docs]
def RnS_SMA100B():
from Instruments_Libraries.SMA100B import SMA100B
import vxi11
rm = vxi11.list_devices()
for _ in rm:
try:
SMA = SMA100B(str(_))
InstrSMA = _
SMA.Close()
except (visa.VisaIOError):
print('Serial Number dont match!')
return SMA100B(InstrSMA)
[docs]
def VNA():
from Instruments_Libraries.MS4647B import MS4647B
import vxi11
rm = vxi11.list_devices()
# rm = visa.ResourceManager('@py')
# list_rm = rm.list_resources()
IP = '169.254.100.85'
Str_IP = None
Set = 0
# for _ in range(len(list_rm)):
# test_ip = list_rm[_].split('::')[1]
# if test_ip == IP:
# while Set == 0:
# try:
# Str_IP = list_rm[_].split('::')[0] + '::' +list_rm[_].split('::')[1]
# VNA = MS4647B(Str_IP)
# data = VNA.getIdn()
# if data == 'ANRITSU,MS4647B,1416530,V2023.9.1':
# Set = 1
# VNA.RTL()
# VNA.Close()
# break
# else:
# print('Connecting')
# except (visa.VisaIOError):
# print('Serial Number dont match!')
#
# else:
# print('No matching Device detected !!')
for _ in rm:
try:
VNA = MS4647B('TCPIP::'+str(_))
InstrVNA = _
VNA.RTL()
VNA.Close()
except (visa.VisaIOError):
print('Serial Number dont match!')
# return MS4647B(Str_IP)
return MS4647B('TCPIP0::169.254.100.85')
#return MS4647B('TCPIP0::131.234.87.205')
[docs]
def APPH():
from Instruments_Libraries.APPH import APPH
# import vxi11
# rm = vxi11.list_devices()
import pyvisa as visa
rm = visa.ResourceManager()
list_rm = rm.list_resources()
for i in range(len(list_rm)):
if list_rm[i].split('::')[0] == 'USB0':
inst = list_rm[i]
else:
pass
try:
AP = APPH(inst)
InstrAPPH = inst
AP.Close()
except (visa.VisaIOError):
print('Serial Number dont match!')
# for _ in rm:
# try:
# AP = APPH('TCPIP0::'+str(_)+'::inst0::INSTR')
# InstrAPPH = _
# AP.Close()
# except (visa.VisaIOError):
# print('Serial Number dont match!')
return APPH(InstrAPPH)
# return APPH('TCPIP0::131.234.87.204::inst0::INSTR')
[docs]
def PowerSupply_GPP4323():
from Instruments_Libraries.GPP4323 import GPP4323
import serial.tools.list_ports
import re
# Regular expression to match the GPP4323 instrument ID
serial_regex = r'^GW INSTEK,GPP-4323.*'
# Get all COM ports
ports = list(serial.tools.list_ports.comports())
# Filter out Bluetooth devices based on description (ignore case)
filtered_ports = [
port for port in ports
if "bluetooth" not in port.description.lower()
]
# Sort ports so that those with "gpp" in their description are prioritized
filtered_ports.sort(key=lambda port: (0 if "gpp" in port.description.lower() else 1, port.device))
print(filtered_ports)
selected_port = None
# Iterate over filtered and sorted COM ports
for port in filtered_ports:
try:
GPP = GPP4323(port.device)
idn = GPP.getIdn()
if re.match(serial_regex, idn.upper()):
selected_port = port.device
break
except Exception as e:
# Log the error or print a message, then continue with next port
print(f"Error connecting to {port.device}: {e}")
finally:
try:
GPP.Close()
except Exception:
pass
if selected_port is None:
raise Exception("No suitable power supply found.")
# Return a new instance connected to the selected port
return GPP4323(selected_port)
[docs]
def UXR_1002A():
from Instruments_Libraries.UXR import UXR
try:
my_UXR = UXR("TCPIP0::KEYSIGH-Q75EBO9.local::hislip0::INSTR")
# Inital Settings UXR
my_UXR.system_header("off") # Defalt is off and should stay off!!!
my_UXR.waveform_byteorder("LSBFirst")
my_UXR.waveform_format("WORD") # Data Aquisition is only implemented for WORD yet.
my_UXR.waveform_streaming("off")
except visa.VisaIOError as e:
print('Caught VisaIOError: ', e)
return my_UXR
# =============================================================================
# Old GPP Function
# =============================================================================
# def PowerSupply_GPP4323():
# from Instruments_Libraries.GPP4323 import GPP4323
# import serial.tools.list_ports
# ports = serial.tools.list_ports.comports()
# for port, desc, hwid in sorted(ports):
# print("{}: {} [{}]".format(port, desc, hwid))
# print(' ')
# print('Check See the COM Port #####')
# Com = input('Give COM number from the Power Supply: ')
# GPP = GPP4323('COM'+Com)
# GPP.Close()
# return GPP4323('COM'+Com)
# =============================================================================
# Load Instrument Librarys
# =============================================================================
[docs]
def InstInit(Num):
if Num == " Anrtisu Spectrum Analyzer MS2760A ":
return SpecAnalyser()
elif Num == " Anritsu Signal Generator MG3694C ":
return SigGen()
elif Num == " Anritsu Vectro Analyzer MS4647B ":
return VNA()
elif Num == " Power Meter ThorLabs PM100D ":
return PowerMeter()
elif Num == " Novoptel Laser LU1000 ":
return LU1000()
elif Num == " Yokogawa Optical Spectrum Analyzer AQ6370D ":
return OSA()
elif Num == " KEITHLEY Source Meter 2612 ":
return SourceMeter()
elif Num == " Power Supply KA3005 ":
return PowerSupply()
elif Num == " CoBrite Tunable Laser ":
return CoBrite()
elif Num == " AnaPico AG,APPH20G ":
return APPH()
elif Num == " 4-Channels Power Suppy GPP4323 ":
return PowerSupply_GPP4323()
elif Num == " Rohde and Schwarz SMA100B ":
return RnS_SMA100B()
elif Num == " Keysight UXR0702A ":
return UXR_1002A()
else:
raise ValueError('Invalid Instrument Selected')