diff --git a/code/drv_bk/src/wattrex_driver_bk/context.py b/code/drv_bk/src/wattrex_driver_bk/context.py index 109e7c3..87e3b63 100644 --- a/code/drv_bk/src/wattrex_driver_bk/context.py +++ b/code/drv_bk/src/wattrex_driver_bk/context.py @@ -24,15 +24,16 @@ ###################### CONSTANTS ###################### # For further information check out README.md DEFAULT_MAX_MSG : int = 100 # Max number of allowed message per chan -DEFAULT_MAX_MESSAGE_SIZE : int = 150 # Size of message sent through IPC message queue -DEFAULT_TX_CHAN : str = 'tx_scpi' #'TX_SCPI' # Name of the TX channel in CAN +DEFAULT_MAX_MESSAGE_SIZE : int = 400 # Size of message sent through IPC message queue +DEFAULT_TX_CHAN : str = 'TX_SCPI' #'TX_SCPI' # Name of the TX channel in CAN DEFAULT_RX_CHAN: str = 'RX_SCPI_BK' #Name of the RX channel for epc DEFAULT_MAX_VOLT: int =1000 #V # Max voltage allowed DEFAULT_MAX_CURR: int = 20 #A # Max current allowed DEFAULT_MAX_WAIT_TIME: int = 3 DEFAULT_TIME_BETWEEN_ATTEMPTS: float = 0.1 +DEFAULT_MAX_READS: int = 10 -CONSTANTS_NAMES = ('DEFAULT_MAX_VOLT', 'DEFAULT_MAX_CURR', +CONSTANTS_NAMES = ('DEFAULT_MAX_VOLT', 'DEFAULT_MAX_CURR', 'DEFAULT_MAX_READS', 'DEFAULT_MAX_MSG', 'DEFAULT_MAX_MESSAGE_SIZE', 'DEFAULT_TX_CHAN', 'DEFAULT_RX_CHAN', 'DEFAULT_MAX_WAIT_TIME') sys_conf_update_config_params(context=globals(), diff --git a/code/drv_bk/src/wattrex_driver_bk/drv_bk.py b/code/drv_bk/src/wattrex_driver_bk/drv_bk.py index 6a1089f..5b9b22f 100755 --- a/code/drv_bk/src/wattrex_driver_bk/drv_bk.py +++ b/code/drv_bk/src/wattrex_driver_bk/drv_bk.py @@ -27,7 +27,7 @@ ###################### CONSTANTS ###################### from .context import (DEFAULT_MAX_VOLT, DEFAULT_MAX_CURR, DEFAULT_TX_CHAN, DEFAULT_RX_CHAN, DEFAULT_MAX_MSG, DEFAULT_MAX_MESSAGE_SIZE, DEFAULT_MAX_WAIT_TIME, - DEFAULT_TIME_BETWEEN_ATTEMPTS) + DEFAULT_TIME_BETWEEN_ATTEMPTS, DEFAULT_MAX_READS) ####################### ENUMS ####################### _MILI_UNITS = 1000 @@ -35,18 +35,32 @@ class DrvBkModeE(Enum): "Modes of the device" - VOLT_AUTO = 'VOLT:DC:RANGE:AUTO ON' - VOLT_R200_MILI_V = 'VOLT:DC:RANGE 0.2' - VOLT_R2_V = 'VOLT:DC:RANGE 2' - VOLT_R20_V = 'VOLT:DC:RANGE 20' - VOLT_R200_V = 'VOLT:DC:RANGE 200' - VOLT_R1000_V = 'VOLT:DC:RANGE 1000' - CURR_AUTO = 'CURR:DC:RANGE:AUTO ON' - CURR_R2_MILI_A = 'CURR:DC:RANGE 0.002' - CURR_R20_MILI_A = 'CURR:DC:RANGE 0.02' - CURR_R200_MILI_A = 'CURR:DC:RANGE 0.2' - CURR_R2_A = 'CURR:DC:RANGE 2' - CURR_R20_A = 'CURR:DC:RANGE 20' + VOLT_DC = 1 + CURR_DC = 2 + VOLT_AC = 3 + CURR_AC = 4 + RESISTANCE = 5 +class DrvBkModeCmdE(Enum): + "Commands to change between modes of the device" + VOLT_DC = 'VOLT:DC' + CURR_DC = 'CURR:DC' + VOLT_AC = 'VOLT:AC' + CURR_AC = 'CURR:AC' + ### Trabajo futuro no aplicable al ciclador + RESISTANCE = 'RES' + ## TODO: Cambiar todas los valores de los modos a los correctos + +class DrvBkRangeE(Enum): + "Modes of the device" + AUTO = ':RANGE:AUTO ON' + R2_MILI_A = ':RANGE 0.002' + R20_MILI_A = ':RANGE 0.02' + R200_MILI = ':RANGE 0.2' + R2 = ':RANGE 2' + R20 = ':RANGE 20' + R200_V = ':RANGE 200' + R1000_V = ':RANGE 1000' + ## TODO: Cambiar todas los valores de los modos a los correctos class _DrvBkIntegrationRateE(Enum): "Integration rate of the device" @@ -55,9 +69,11 @@ class _DrvBkIntegrationRateE(Enum): FAST = '0.1' class _ScpiCmds(Enum): - INIT_DEV_SPEED = 'VOLT:DC:NPLC '+ _DrvBkIntegrationRateE.MEDIUM.value - READ_INFO = ':IDN*?' - CHANGE_MODE = 'FUNC ' + INIT_DEV_SPEED= ":VOLT:DC:NPLC "+ str(_DrvBkIntegrationRateE.FAST.value) + READ_INFO= "*IDN?" + CHANGE_MODE= ":FUNC " + READ_MODE= ":FUNC?" + READ_DATA= ":FETC?" ####################### CLASSES ####################### class DrvBkPropertiesC(DrvBasePwrPropertiesC): @@ -70,8 +86,9 @@ def __init__(self, model: str|None = None, serial_number: str|None = None, class DrvBkDataC(DrvBasePwrDataC): "Data class of bk device" - def __init__(self, mode: DrvBkModeE, status: DrvBaseStatusC, + def __init__(self, mode: DrvBkModeE, range: DrvBkRangeE, status: DrvBaseStatusC, voltage: int, current: int, power: int) -> None: + self.range: DrvBkRangeE = range super().__init__(status = status, mode = mode, voltage = voltage, current = current, power = power) @@ -81,17 +98,17 @@ class DrvBkDeviceC(DrvBasePwrDeviceC): def __init__(self, config: DrvScpiSerialConfC) -> None: super().__init__() self.__tx_chan = SysShdIpcChanC(name = DEFAULT_TX_CHAN) - self.__rx_chan = SysShdIpcChanC(name = DEFAULT_RX_CHAN+'_'+config.port, + self.__rx_chan = SysShdIpcChanC(name = DEFAULT_RX_CHAN+'_'+config.port.split('/')[-1], max_msg= DEFAULT_MAX_MSG, max_message_size= DEFAULT_MAX_MESSAGE_SIZE) self.__port = config.port add_msg = DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.ADD_DEV, port = config.port, payload = config, - rx_chan_name = DEFAULT_RX_CHAN+'_'+config.port) + rx_chan_name = DEFAULT_RX_CHAN+'_'+config.port.split('/')[-1]) self.__rx_chan.delete_until_last() self.__tx_chan.send_data(add_msg) - self.last_data: DrvBkDataC = DrvBkDataC(mode = DrvBkModeE.VOLT_AUTO, + self.last_data: DrvBkDataC = DrvBkDataC(mode = DrvBkModeE.VOLT_DC, range= DrvBkRangeE.AUTO, status = DrvBaseStatusC(DrvBaseStatusE.OK), voltage = 0, current = 0, power = 0) @@ -99,7 +116,42 @@ def __init__(self, config: DrvScpiSerialConfC) -> None: serial_number= None, MAX_VOLT= 0, MAX_CURR= 0, MAX_PWR= 0) self.__initialize_control() + self.read_buffer() self.__read_device_properties() + self.read_buffer() + + def read_buffer(self) -> None: + '''Read the buffer of the device. + Args: + - None. + Returns: + - None. + Raises: + - None. + ''' + i = 0 + while i < DEFAULT_MAX_READS or not self.__rx_chan.is_empty(): + self.__parse_msg() + i += 1 + + def __exp_number(self,str_msg) -> int|None: + """ + Converts a string representation of a number in scientific notation to an integer. + + Args: + str_msg (str): The string representation of the number in scientific notation. + + Returns: + int|None: The converted integer value if successful, None otherwise. + """ + response = None + if all([var.isnumeric() for var in str_msg.split('e')]): + msg_sci = str_msg.split('e') + if len(msg_sci) == 2: + response = float(msg_sci[0]) * 10 ** int(msg_sci[1]) + else: + response = float(msg_sci[0]) + return int(response*_MILI_UNITS) def __parse_msg(self) -> None: '''Parse the message received from the device. @@ -113,36 +165,95 @@ def __parse_msg(self) -> None: msg: DrvScpiCmdDataC = self.__rx_chan.receive_data_unblocking() if msg is not None and msg.data_type == DrvScpiCmdTypeE.RESP: log.critical(f"Message received: {msg.payload}") - if 'ERROR' not in msg.payload[0]: - if 'IDN' in msg.payload[0]: - info = msg.payload[0].split(',') - model = info[0] - serial_number = info[-1] - self.properties = DrvBkPropertiesC(model = model, serial_number = serial_number, - MAX_VOLT = DEFAULT_MAX_VOLT * _MILI_UNITS, - MAX_CURR = DEFAULT_MAX_CURR * _MILI_UNITS, - MAX_PWR = _MAX_PWR * _MILI_UNITS) - elif 'FETC' in msg.payload[0]: - response: list = findall(r"-?\d*\.?\d+", msg.payload[0]) - if len(response) < 2: - response = float(response[0]) + for data in msg.payload: + if len(data) >0 and not str(data).startswith(":"): + data = self.__exp_number(data) if self.__exp_number(data) is not None else data + if isinstance(data, str): + if 'volt' in data: + if 'ac' in data: + self.last_data.mode = DrvBkModeE.VOLT_AC + else: + self.last_data.mode = DrvBkModeE.VOLT_DC + elif 'curr' in data: + if 'ac' in data: + self.last_data.mode = DrvBkModeE.CURR_AC + else: + self.last_data.mode = DrvBkModeE.CURR_DC + elif 'NO ERROR' in data: + self.last_data.status = DrvBaseStatusE.OK + elif 'ERROR' in data: + self.last_data.status = DrvBaseStatusE.COMM_ERROR + elif len(data.split(',')) == 3: + data = data.split(',') + self.properties = DrvBkPropertiesC(model = data[0], + serial_number = data[1], + MAX_VOLT = DEFAULT_MAX_VOLT * _MILI_UNITS, + MAX_CURR = DEFAULT_MAX_CURR * _MILI_UNITS, + MAX_PWR = _MAX_PWR * _MILI_UNITS) + log.info(f"Serial number: {data[-1]}") + log.info(f"Model: {data[0]}") + log.info(f"Response: {data}") else: - response = float(response[0]) * 10 ** int(response[1]) - response = int(response * _MILI_UNITS) - status = DrvBaseStatusC(DrvBaseStatusE.OK) + if self.last_data.mode in (DrvBkModeE.VOLT_DC, DrvBkModeE.VOLT_AC): + self.last_data.voltage = data + else: + self.last_data.current = data + log.info(f"Value read: {data}") + # if 'ERROR' not in msg.payload[0]: + # if all([var.isnumeric() for var in msg.payload[0].split('e')]): + # msg_sci = msg.payload[0].split('e') + # if len(msg_sci) == 2: + # response = float(msg_sci[0]) * 10 ** int(msg_sci[1]) + # else: + # response = float(msg_sci[0]) + # if self.last_data.mode in (DrvBkModeE.VOLT_DC, DrvBkModeE.VOLT_AC): + # self.last_data.voltage = int(response * _MILI_UNITS) + # else: + # self.last_data.current = int(response * _MILI_UNITS) + # if _ScpiCmds.READ_INFO.value in msg.payload[0]: + # info = msg.payload[1].split(',') + # model = info[0] + # serial_number = info[-1] + # self.properties = DrvBkPropertiesC(model = model, + # serial_number = serial_number, + # MAX_VOLT = DEFAULT_MAX_VOLT * _MILI_UNITS, + # MAX_CURR = DEFAULT_MAX_CURR * _MILI_UNITS, + # MAX_PWR = _MAX_PWR * _MILI_UNITS) + # if _ScpiCmds.READ_DATA.value in msg.payload[0]: + # response: list = findall(r"-?\d*\.?\d+", msg.payload[0]) + # if len(response) < 2: + # response = float(response[0]) + # else: + # response = float(response[0]) * 10 ** int(response[1]) + # response = int(response * _MILI_UNITS) + # status = DrvBaseStatusC(DrvBaseStatusE.OK) - if self.last_data.mode.value.split(':')[0] == 'VOLT': - voltage = response - elif self.last_data.mode.value.split(':')[0] == 'CURR': - current = response - self.last_data = DrvBkDataC(mode = self.last_data.mode, status = status, - voltage = voltage, current = current, power = 0) - else: - log.error(f'Unknown message received: {msg.payload[0]}') - else: - log.error(msg.payload[0]) + # if self.last_data.mode.value.split(':')[0] == 'VOLT': + # voltage = response + # elif self.last_data.mode.value.split(':')[0] == 'CURR': + # current = response + # self.last_data = DrvBkDataC(mode = self.last_data.mode, status = status, + # voltage = voltage, current = current, power = 0) + # if _ScpiCmds.READ_MODE.value in msg.payload[0]: + # mode_loc= msg.payload[0].replace("b'", + # "").replace("\\n'","").split(";").index(_ScpiCmds.READ_MODE.value) + 1 + # if "volt:dc" in msg.payload[mode_loc]: + # self.last_data.mode = DrvBkModeE.VOLT_DC + # elif "curr:dc" in msg.payload[mode_loc]: + # self.last_data.mode = DrvBkModeE.CURR_DC + # elif "volt:ac" in msg.payload[mode_loc]: + # self.last_data.mode = DrvBkModeE.VOLT_AC + # elif "curr:ac" in msg.payload[mode_loc]: + # self.last_data.mode = DrvBkModeE.CURR_AC + # log.info(f"Mode set to: {self.last_data.mode}") + # if _ScpiCmds.INIT_DEV_SPEED.value in msg.payload[0]: + # log.info(f"Device speed set to: {msg.payload[0].split(' ')[-1]}") + # else: + # log.error(msg.payload[0]) + elif msg is None: + pass else: - log.error('Unknown message type received') + log.error(f'Unknown message type received: {msg.__dict__}') def __initialize_control(self) -> None: '''Initialize the device control. @@ -156,12 +267,12 @@ def __initialize_control(self) -> None: exception = True #Initialize device speed msg = DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, port= self.__port, - payload= _ScpiCmds.INIT_DEV_SPEED) + payload= _ScpiCmds.INIT_DEV_SPEED.value) self.__tx_chan.send_data(msg) time_init = time() while (time()-time_init) < DEFAULT_MAX_WAIT_TIME: sleep(DEFAULT_TIME_BETWEEN_ATTEMPTS) - self.__parse_msg() + self.read_buffer() # if not self.__rx_chan.is_empty(): # command_rec : DrvScpiCmdDataC = self.__rx_chan.receive_data() # msg_rcv = command_rec.payload[0] @@ -173,9 +284,13 @@ def __initialize_control(self) -> None: # if exception: # raise ConnectionError("Device not found") #Initialize device mode in auto voltage - msg = DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE, port= self.__port, - payload= DrvBkModeE.VOLT_AUTO.value) + msg = DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, port= self.__port, + payload= f"{_ScpiCmds.CHANGE_MODE.value}{DrvBkModeCmdE.VOLT_DC.value}") self.__tx_chan.send_data(msg) + msg = DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, port= self.__port, + payload=f"{DrvBkModeCmdE.VOLT_DC.value}{DrvBkRangeE.AUTO.value}") + self.__tx_chan.send_data(msg) + self.read_buffer() def __read_device_properties(self) -> None: '''Read the device properties . @@ -189,23 +304,14 @@ def __read_device_properties(self) -> None: # info = self.device_handler.read_device_info() # info = info[1].split() msg = DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, port= self.__port, - payload= _ScpiCmds.READ_INFO) + payload= _ScpiCmds.READ_INFO.value) self.__tx_chan.send_data(msg) - info: DrvScpiCmdDataC = self.__rx_chan.receive_data() - if hasattr(info, 'payload'): - info = info.payload.split(',') - model = info[0] - serial_number = info[-1] - else: - model = None - serial_number = None - self.properties = DrvBkPropertiesC(model = model, serial_number = serial_number, - MAX_VOLT = DEFAULT_MAX_VOLT * _MILI_UNITS, - MAX_CURR = DEFAULT_MAX_CURR * _MILI_UNITS, - MAX_PWR = _MAX_PWR * _MILI_UNITS) + self.read_buffer() + if self.properties.model is None: + raise ConnectionError("Device not found") - def set_mode(self, meas_mode: DrvBkModeE) -> None: + def set_mode(self, meas_mode: DrvBkModeE, range: DrvBkRangeE = DrvBkRangeE.AUTO) -> None: '''Set the device mode. Args: - meas_mode (DrvBkModeE): Mode to set. @@ -214,14 +320,19 @@ def set_mode(self, meas_mode: DrvBkModeE) -> None: Raises: - None. ''' - mode_cod = meas_mode.value.split(':')[0]+':' + meas_mode.value.split(':')[1] - #Change mode to voltage or current + self.read_buffer() + mode_cod = DrvBkModeCmdE[meas_mode.name] + #Change mode to voltage or current and range all in one + self.__tx_chan.send_data(DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, + port= self.__port, + payload= _ScpiCmds.CHANGE_MODE.value + mode_cod.value)) self.__tx_chan.send_data(DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, - port= self.__port, payload= _ScpiCmds.CHANGE_MODE + mode_cod)) - #Change range of the mode + port= self.__port, + payload=":"+mode_cod.value+range.value)) self.__tx_chan.send_data(DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, - port= self.__port,payload= meas_mode.value)) - self.last_data.mode = meas_mode + port= self.__port, + payload= _ScpiCmds.READ_MODE.value)) + self.read_buffer() def get_data(self) -> DrvBkDataC: @@ -230,34 +341,12 @@ def get_data(self) -> DrvBkDataC: - None. Returns: - (DrvBkDataC): Returns the device measures. - Raises: - - None ''' self.__tx_chan.send_data(DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.WRITE_READ, - port= self.__port, payload= 'FETC?')) + port= self.__port, + payload= _ScpiCmds.READ_DATA.value+";"+_ScpiCmds.READ_MODE.value)) + self.read_buffer() return self.last_data - # current = 0 - # voltage = 0 - # status = DrvBaseStatusE(DrvBaseStatusE.COMM_ERROR) - # #Read measure - # self.device_handler.send_and_read('FETC?') - # response = self.device_handler.receive_msg()[0] - # if response != '': - # response = self.device_handler.decode_numbers(response) - # response = int(response * _MILI_UNITS) - # status = DrvBaseStatusE(DrvBaseStatusE.OK) - - # if self.last_data.mode.value.split(':')[0] == 'VOLT': - # voltage = response - # elif self.last_data.mode.value.split(':')[0] == 'CURR': - # current = response - # else: - # status = DrvBaseStatusE(DrvBaseStatusE.COMM_ERROR) - - # self.last_data = DrvBkDataC(mode = self.last_data.mode, status = status, - # voltage = voltage, current = current, power = 0) - # return self.last_data - def get_properties(self) -> DrvBkPropertiesC: '''Read the device properties. @@ -265,12 +354,10 @@ def get_properties(self) -> DrvBkPropertiesC: - None. Returns: - (DrvBkPropertiesC): Returns the device properties. - Raises: - - None. ''' + self.read_buffer() return self.properties - def close(self) -> None: '''Close communication with the device. Args: @@ -280,7 +367,9 @@ def close(self) -> None: Raises: - None. ''' + self.read_buffer() self.__tx_chan.send_data(DrvScpiCmdDataC(data_type = DrvScpiCmdTypeE.DEL_DEV, port= self.__port)) self.__tx_chan.close() + self.read_buffer() self.__rx_chan.close() diff --git a/code/drv_bk/tests/example.py b/code/drv_bk/tests/example.py index 47174db..7c0ff56 100755 --- a/code/drv_bk/tests/example.py +++ b/code/drv_bk/tests/example.py @@ -9,7 +9,8 @@ import sys import os import time - +from threading import Event +from serial import Serial, PARITY_ODD ####################### GENERIC IMPORTS ####################### @@ -25,38 +26,97 @@ ####################### PROJECT IMPORTS ####################### -from scpi_sniffer import DrvScpiSerialConfC +from scpi_sniffer import DrvScpiSerialConfC, DrvScpiNodeC, DrvScpiHandlerC ####################### MODULE IMPORTS ####################### sys.path.append(os.getcwd()+'/code/drv_bk/') -from src.wattrex_driver_bk.drv_bk import DrvBkModeE, DrvBkDeviceC +from src.wattrex_driver_bk.drv_bk import DrvBkModeE, DrvBkDeviceC, DrvBkRangeE ####################### ENUMS ####################### ####################### CLASSES ####################### +def set_source(current:float, voltage: float, serial: Serial): + serial.write(f":CURRent {current};:Voltage {voltage};:OUTPut ON\n".encode()) + time.sleep(2) + serial.write(b":SYST:ERR?\n") + log.info(serial.readlines()) +def disable_source(serial: Serial): + serial.write(b":OUTP OFF\n") + log.info(serial.readlines()) + + + def main(): - "Main function" - init = time.time() - - #Create driver - scpi = DrvScpiSerialConfC(port = '/dev/ttyUSB0', separator='\n', baudrate=38400, \ - timeout=1, write_timeout=1) - drv = DrvBkDeviceC(config= scpi) - - #Set properties - drv.set_mode(DrvBkModeE.CURR_AUTO) - - #Obtain data - data = drv.get_data() - log.info(f"Voltage: {data.voltage}\tCurrent: {data.current}\tPower: {data.power}\n\ - Mode: {data.mode}\tStatus: {data.status}") - - #Obtain properties - properties = drv.get_properties() - log.info(f"Voltage: {properties.max_volt_limit}\tCurrent: {properties.max_current_limit}\t\ - Power: {properties.max_power_limit}\n\ - Model: {properties.model}\tSerial number: {properties.serial_number}") - log.info(f"Time elapsed: {time.time() - init}") + # working_flag = Event() + # working_flag.set() + # scpi = DrvScpiNodeC(working_flag=working_flag, cycle_period=500) + bk_scpi_conf = DrvScpiSerialConfC(port = '/dev/wattrex/bk/BK_0001', separator='\n', + baudrate=38200, timeout=1, write_timeout=1) + ea_scpi_conf = DrvScpiSerialConfC(port = '/dev/wattrex/source/EA_2963640425', separator='\n', + baudrate=9600, timeout=1, write_timeout=1, parity= PARITY_ODD) + ea_serial: Serial = Serial(port = ea_scpi_conf.port, + baudrate = ea_scpi_conf.baudrate, + bytesize = ea_scpi_conf.bytesize, + parity = ea_scpi_conf.parity, + stopbits = ea_scpi_conf.stopbits, + timeout = ea_scpi_conf.timeout, + write_timeout = ea_scpi_conf.write_timeout, + inter_byte_timeout = ea_scpi_conf.inter_byte_timeout, + xonxoff=True, rtscts=False, dsrdtr=False) + drv = DrvBkDeviceC(config= bk_scpi_conf) + try: + "Main function" + init = time.time() + + input("Press Enter to start...") + #Set properties + drv.set_mode(DrvBkModeE.CURR_DC, DrvBkRangeE.AUTO) + time.sleep(2) + try: + while drv.last_data.mode != DrvBkModeE.CURR_DC: + drv.__parse_msg() + log.info("Waiting to set mode to CURR_AUTO") + time.sleep(2) + log.info("Mode set correctly to CURR_AUTO") + drv.set_mode(meas_mode = DrvBkModeE.VOLT_DC, range= DrvBkRangeE.AUTO) + while drv.last_data.mode != DrvBkModeE.VOLT_DC: + log.info("Waiting to set mode to VOLT_AUTO") + time.sleep(2) + except KeyboardInterrupt: + drv.close() + time.sleep(3) + sys.exit(1) + input("Press Enter to set source...") + log.info("Mode set correctly to VOLT_AUTO") + set_source(0.1, 4.0, ea_serial) + for _ in range(10): + #Obtain data + init = time.time() + data = drv.get_data() + log.info(f"Voltage: {data.voltage}\tCurrent: {data.current}\tPower: {data.power}\n\ + Mode: {data.mode}\tStatus: {data.status}") + log.info(f"Time elapsed: {time.time() - init}") + time.sleep(1) + + #Obtain properties + properties = drv.get_properties() + log.info("Properties: ") + log.info(f"Voltage: {properties.max_volt_limit}\tCurrent: {properties.max_current_limit}\t\ + Power: {properties.max_power_limit}\n\ + Model: {properties.model}\tSerial number: {properties.serial_number}") + log.info(f"Time elapsed: {time.time() - init}") + except (KeyboardInterrupt, AttributeError, Exception): + disable_source(ea_serial) + drv.close() + time.sleep(2) + # working_flag.clear() + finally: + disable_source(ea_serial) + log.info(f'SCPI node stopped') + sys.exit(0) + # working_flag.clear() + log.info('SCPI node stopped') + time.sleep(2) if __name__ == '__main__':