diff --git a/looper.py b/looper.py index b902825..5130cfd 100644 --- a/looper.py +++ b/looper.py @@ -6,11 +6,33 @@ import time import sys from pathlib import Path from datetime import datetime +import pyvisa +import vxi11 # Initial state toggle_state = 0 -ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200) +#ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200) + +VISA_ADDRESS = "TCPIP::192.168.45.3::INSTR" + +rm = pyvisa.ResourceManager() + +SPD = rm.open_resource( + VISA_ADDRESS, + write_termination='\n', + read_termination='\n' +) + +class DS1202ZE(vxi11.Instrument): + def __init__(self, host, *args, **kwargs): + super(DS1202ZE, self).__init__(host, *args, **kwargs) + def get_identification(self): + return self.ask("*IDN?") + def get_vavg_channel1(self): + return self.ask(":MEAS:ITEM? VAVG, CHAN1\n") + def get_vavg_channel2(self): + return self.ask(":MEAS:ITEM? VAVG, CHAN2\n") class Tee(object): def __init__(self, filename): @@ -56,6 +78,24 @@ def pack_integers_to_bytes(*integers: int) -> bytes: return struct.pack(format_string, *full_packet) +def set_voltage(): + global SPD + + voltage = random.randint(9, 30) + voltage_f = float(voltage) + + SPD.write('CH1:VOLT ' + str(voltage_f)) # Send power supply source voltage + + print(f"[{time.strftime('%H:%M:%S')}] VOLTAGE SET TO: " + str(voltage) + "V") + + # Schedule the NEXT random trigger + # random.uniform(60, 360) provides the random delay in seconds + delay = random.uniform(60, 360) + + # Create and start a new timer thread + timer = threading.Timer(delay, set_voltage) + timer.start() + def trigger_serial_command(): global toggle_state global ser @@ -65,19 +105,14 @@ def trigger_serial_command(): toggle_state = 1 - toggle_state if toggle_state == 0: - voltage = 0 - voltage_mv = 0 state = 0 - print("VOLTAGE SET TO: OFF") + print(f"[{time.strftime('%H:%M:%S')}] SWITCH SET TO: OFF") - else: - # Generate voltage level - voltage = random.randint(9, 30) - voltage_mv = voltage * 1000 + else: state = 1 - print("VOLTAGE SET TO: " + str(voltage) + "V") + print(f"[{time.strftime('%H:%M:%S')}] SWITCH SET TO: ON") # Serial logic print(f"[{time.strftime('%H:%M:%S')}] SENDING SERIAL COMMAND...") @@ -86,13 +121,13 @@ def trigger_serial_command(): data = (command, state) byte_data = pack_integers_to_bytes(*data) - ser.write(serial.to_bytes(byte_data)) + #ser.write(serial.to_bytes(byte_data)) # Schedule the NEXT random trigger # random.uniform(0.1, 100.0) provides the random delay in seconds delay = random.uniform(min_delay_input_f, max_delay_input_f) - print(f"NEXT COMMAND IN {delay:.2f} SECONDS...") - + print(f"[{time.strftime('%H:%M:%S')}] NEXT COMMAND IN {delay:.2f} SECONDS...") + # Create and start a new timer thread timer = threading.Timer(delay, trigger_serial_command) timer.start() @@ -106,27 +141,33 @@ if __name__ == '__main__': build_path = "~/Python/Automotive-Power-Simulator-App/LOGS/" + timestamp_str + ".txt" log_file = Path(build_path).expanduser() sys.stdout = Tee(log_file) - print("LOGGING TO: " + str(log_file)) + print(f"[{time.strftime('%H:%M:%S')}] LOGGING TO: " + str(log_file)) + + SPD.write('CH1:VOLT ' + str(24.0)) # Send power supply source voltage + SPD.write('CH1:CURR ' + str(1.5)) # Send power supply current limit + + # Enable power supply output source + SPD.write('OUTP CH1,ON') # Enable supply output # Get time constants - print ("PLEASE ENTER A MINIMUM DELAY VALUE...") + print (f"[{time.strftime('%H:%M:%S')}] PLEASE ENTER A MINIMUM DELAY VALUE...") min_delay_input = input() min_delay_input_f = float(min_delay_input) - print("MINIMUM TIME DELAY: " + str(min_delay_input_f)) + print(f"[{time.strftime('%H:%M:%S')}] MINIMUM TIME DELAY: " + str(min_delay_input_f)) # Get time constants - print ("PLEASE ENTER A MAXIMUM DELAY VALUE...") + print (f"[{time.strftime('%H:%M:%S')}] PLEASE ENTER A MAXIMUM DELAY VALUE...") max_delay_input = input() max_delay_input_f = float(max_delay_input) - print("MAXIMUM TIME DELAY: " + str(max_delay_input_f)) + print("f[{time.strftime('%H:%M:%S')}] MAXIMUM TIME DELAY: " + str(max_delay_input_f)) # Start the very first trigger trigger_serial_command() - # Setup serial port + set_voltage() try: while (1): @@ -134,16 +175,17 @@ if __name__ == '__main__': except KeyboardInterrupt: state = 0 - command = 83 # 0x53 'S' ASCII - voltage_mv = 0 - - b1 = (voltage_mv >> 24) & 0xFF # Most Significant Byte - b2 = (voltage_mv >> 16) & 0xFF - b3 = (voltage_mv >> 8) & 0xFF - b4 = voltage_mv & 0xFF + command = 115 # 0x73 's' ASCII - data = (command, state, b1, b2, b3, b4) + data = (command, state) byte_data = pack_integers_to_bytes(*data) - ser.write(serial.to_bytes(byte_data)) + #ser.write(serial.to_bytes(byte_data)) - ser.close() \ No newline at end of file + #ser.close() + + SPD.write('CH1:VOLT ' + str(0.0)) # Send power supply source voltage + SPD.write('CH1:CURR ' + str(0.0)) # Send power supply current limit + + # Disable power supply output source + SPD.write('OUTP CH1,OFF') + SPD.close() \ No newline at end of file