Skip to content

Commit

Permalink
Added examples for test
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoAldea committed Feb 26, 2024
1 parent b28e4bd commit 63726ab
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
98 changes: 98 additions & 0 deletions code/drv_ea/tests/example_bk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/python3
'''
Example to ea power supply.
'''
####################### MANDATORY IMPORTS #######################
from __future__ import annotations
import sys
import os

####################### GENERIC IMPORTS #######################
import time

####################### THIRD PARTY IMPORTS #######################

####################### SYSTEM ABSTRACTION IMPORTS #######################
sys.path.append(os.getcwd())
from system_logger_tool import SysLogLoggerC, sys_log_logger_get_module_logger # pylint: disable=wrong-import-position
if __name__ == '__main__':
cycler_logger = SysLogLoggerC(file_log_levels= 'code/log_config.yaml')
log = sys_log_logger_get_module_logger(__name__)

####################### PROJECT IMPORTS #######################
from scpi_sniffer import DrvScpiSerialConfC

####################### MODULE IMPORTS #######################
sys.path.append(os.getcwd()+'/code/drv_ea/')
from src.wattrex_driver_ea.drv_ea import DrvEaDeviceC
from wattrex_driver_base import DrvBasePwrModeE
from wattrex_driver_bk import DrvBkDeviceC, DrvBkModeE
####################### ENUMS #######################


####################### CLASSES #######################
def main():
'''
Example usage of drv_ea with a source_ea device.
'''
ea_scpi_conf = DrvScpiSerialConfC(port = '/dev/ttyACM0', separator='\n',
baudrate=38200, timeout=1, write_timeout=1)
bk_scpi_conf = DrvScpiSerialConfC(port = '/dev/wattrex/bk/BK_0001', separator='\n',
baudrate=38200, timeout=1, write_timeout=1)

source = DrvEaDeviceC(config = ea_scpi_conf)
bk = DrvBkDeviceC(config= bk_scpi_conf)
try:
log.info(f"Source properties: {source.properties.__dict__}")
log.info(f"BK properties: {bk.properties.__dict__}")
bk.set_mode(meas_mode= DrvBkModeE.VOLT_DC)
source.set_cc_mode(curr_ref = 10, voltage_limit = 3000)
bk.read_buffer()
for _ in range(10):
#Obtain data
init = time.time()
data = source.get_data()
log.info((f"Voltage: {data.voltage}\tCurrent: {data.current}\tPower: {data.power}\n"
f"Mode: {data.mode}\tStatus: {data.status}"))
data = bk.get_data()
log.warning((f"Voltage: {data.voltage}\tCurrent: {data.current}\tPower: {data.power}\n"
f"Mode: {data.mode}\tStatus: {data.status}"))
log.critical(f"Time elapsed: {time.time() - init}")
time.sleep(2)
source.disable()
init = time.time()
while source.last_data.mode != DrvBasePwrModeE.WAIT:
source.read_buffer()
bk.read_buffer()
time.sleep(1)
log.info(f"Time elapsed to disable output: {time.time() - init}")
source.set_cv_mode(volt_ref = 5000, current_limit = 250)
for _ in range(10):
#Obtain data
init = time.time()
data = source.get_data()
log.info((f"Voltage: {data.voltage}\tCurrent: {data.current}\tPower: {data.power}\n"
f"Mode: {data.mode}\tStatus: {data.status}"))
data = bk.get_data()
log.warning((f"Voltage: {data.voltage}\tCurrent: {data.current}\tPower: {data.power}\n"
f"Mode: {data.mode}\tStatus: {data.status}"))
log.critical(f"Time elapsed: {time.time() - init}")
time.sleep(2)
source.disable()
init = time.time()
while source.last_data.mode != DrvBasePwrModeE.WAIT:
source.read_buffer()
bk.read_buffer()
time.sleep(1)
log.info(f"Time elapsed to disable output: {time.time() - init}")
source.close()
bk.close()
log.info('End of example of drv ea and bk')
except (KeyboardInterrupt, AttributeError, Exception) as err:
source.close()
bk.close()
log.error(f'End of example of drv ea and bk with error: {err}')
sys.exit(0)

if __name__ == '__main__':
main()
86 changes: 86 additions & 0 deletions code/drv_ea/tests/example_serial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/python3

'''
Example to bk precision.
'''

####################### MANDATORY IMPORTS #######################
from __future__ import annotations
import sys
import os
from serial import Serial, PARITY_ODD
####################### GENERIC IMPORTS #######################


####################### THIRD PARTY IMPORTS #######################


####################### SYSTEM ABSTRACTION IMPORTS #######################
from system_logger_tool import sys_log_logger_get_module_logger
if __name__ == '__main__':
from system_logger_tool import SysLogLoggerC
cycler_logger = SysLogLoggerC(file_log_levels= 'code/log_config.yaml')
log = sys_log_logger_get_module_logger(__name__)


####################### PROJECT IMPORTS #######################
from scpi_sniffer import DrvScpiSerialConfC

####################### MODULE IMPORTS #######################
sys.path.append(os.getcwd()+'/code/drv_bk/')

####################### ENUMS #######################

####################### CLASSES #######################
def main():
ea_scpi_conf = DrvScpiSerialConfC(port = '/dev/ttyACM0', 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)
try:
#Create driver
input("Press Enter to lock source...")
ea_serial.write(b':SYSTem:LOCK: ON\n')
ea_serial.write(b"OUTPut?\n")
# time.sleep(2)
ea_serial.write(b":SYST:ERR?\n")
log.info(ea_serial.readlines())
ea_serial.write(b'*IDN?\n')
# time.sleep(2)
log.info(ea_serial.readlines())
input("Press Enter to set output...")
ea_serial.write(b":CURRent 0.5;:Voltage 7;:OUTPut ON\n")
# time.sleep(2)
ea_serial.write(b":SYST:ERR?\n")
log.info(ea_serial.readlines())
ea_serial.write(b"MEASure:ARRay?\n")
# time.sleep(2)
ea_serial.write(b":SYST:ERR?\n")
log.info(ea_serial.readlines())
ea_serial.write(b"SYSTem:NOMinal:CURRent?\n")
ea_serial.write(b"SYSTem:NOMinal:VOLTage?\n")
ea_serial.write(b"SYSTem:NOMinal:POWer?\n")
ea_serial.write(b"OUTPut?\n")
# time.sleep(2)
ea_serial.write(b":SYST:ERR?\n")
log.info(ea_serial.readlines())
sys.exit(0)
except KeyboardInterrupt:
log.info('SCPI node stopped')
except Exception as e:
log.error(f"Error creating handler: {e}")
finally:
ea_serial.write(b":OUTPut OFF\n")
ea_serial.write(b":SYSTem:LOCK: OFF\n")
ea_serial.close()
sys.exit()

if __name__ == '__main__':
main()

0 comments on commit 63726ab

Please sign in to comment.