89 lines
2.3 KiB
Python
89 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
import pyvisa
|
|
import time
|
|
import asyncio
|
|
from src.api_finot_requests import ApiFinotCredentials
|
|
|
|
from core.config import Settings
|
|
|
|
config: Settings = Settings()
|
|
api_finot_obj = ApiFinotCredentials()
|
|
|
|
|
|
def connect_instrument(rm):
|
|
try:
|
|
my_instrument = rm.open_resource(
|
|
f'TCPIP::{config.agilent_ip}::inst0::INSTR', open_timeout=5)
|
|
my_instrument.timeout = 2
|
|
time.sleep(1)
|
|
return my_instrument
|
|
|
|
except Exception as err:
|
|
print("Error de conexión, reconectar.", err)
|
|
return False
|
|
|
|
async def agilent_visa():
|
|
rm = pyvisa.ResourceManager()
|
|
rm.list_resources()
|
|
print(rm.list_resources())
|
|
# connect to instrument
|
|
my_instrument = connect_instrument(rm)
|
|
while my_instrument is False:
|
|
my_instrument = connect_instrument(rm)
|
|
print("Reconectar...")
|
|
await asyncio.sleep(.5)
|
|
return False
|
|
|
|
# resetea el equipo
|
|
my_instrument.write("*RST")
|
|
|
|
# inicia canales por defecto como vdc
|
|
my_instrument.write('ROUT:SCAN (@101, 102, 103)')
|
|
|
|
try:
|
|
my_instrument.write('*IDN?')
|
|
print("IDN?", my_instrument.read())
|
|
time.sleep(.5)
|
|
except Exception as e:
|
|
print(e)
|
|
await asyncio.sleep(.5)
|
|
return False
|
|
|
|
# check channels estan iniciados
|
|
channels = my_instrument.query('ROUT:SCAN?')
|
|
channels = channels.strip()
|
|
print("Channels:", channels[channels.find('@')+1:-1].split(','))
|
|
|
|
while True:
|
|
finot_output = {}
|
|
try:
|
|
|
|
for mag, channel in config.agilent_output.items():
|
|
finot_output[mag] = float(
|
|
my_instrument.query(f"{config.agilent_requests[channel]} (@{channel})"))
|
|
await api_finot_obj.insert_values_api_finot(finot_output)
|
|
|
|
time.sleep(config.loop_time)
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
print("Reset connection...")
|
|
time.sleep(4)
|
|
my_instrument = connect_instrument(rm)
|
|
|
|
async def create_entitie():
|
|
api_finot_obj = ApiFinotCredentials()
|
|
await api_finot_obj.create_file_api_finot(config.agilent_output)
|
|
|
|
# main
|
|
if __name__ == "__main__":
|
|
|
|
# Create entitie if not exist
|
|
# asyncio.run(create_entitie())
|
|
|
|
try:
|
|
asyncio.run(agilent_visa())
|
|
|
|
except KeyboardInterrupt:
|
|
exit(0)
|
|
|