import csv import time from hil_lib import HILController # --- SET YOUR PORTS HERE --- EMULATOR_PORT = 'COM4' # The Bridge TARGET_PORT = 'COM5' # The App FILENAME = 'hil_test_results.csv' hil = HILController(EMULATOR_PORT, TARGET_PORT) print(f"Starting HIL Test... Saving to {FILENAME}") with open(FILENAME, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(["Simulated_CO2", "Target_Read_Output", "Timestamp"]) try: # Test values to cycle through test_values = [400.0, 600.5, 850.0, 1200.0, 2500.0] for val in test_values: print(f"\nTesting CO2: {val} ppm") # 1. Put data on the bus (Wait for emulator to request it) start_time = time.time() success = False while time.time() - start_tick < 5: # 5 second timeout per sample if hil.set_emulator_data(co2=val, temp=25.0, hum=50.0): success = True break # 2. Read what the Target ESP32 saw on its Serial Monitor time.sleep(1) # Give the Target a moment to process and print target_data = hil.read_target_output() if target_data: print(f"Target Received: {target_data}") # 3. Store in CSV writer.writerow([val, target_data, time.ctime()]) file.flush() # Ensure data is written to disk else: print("Warning: Target did not report data in time.") except KeyboardInterrupt: print("Test stopped by user.") finally: hil.close()