86 lines
3.3 KiB
Python
86 lines
3.3 KiB
Python
import csv
|
|
import time
|
|
from hil_lib import SCD30_HIL_Lib
|
|
|
|
# Device paths from your environment
|
|
Device = 'PC'
|
|
|
|
if Device == 'PC':
|
|
EMULATOR = 'COM11'#'/dev/i2c_emulator'
|
|
TARGET = 'COM10'#'/dev/esp_sensor_test'
|
|
elif Device == 'HIL':
|
|
EMULATOR = '/dev/i2c_emulator'
|
|
TARGET = '/dev/esp_sensor_test'
|
|
LOG_FILE = 'hil_validation_results.csv'
|
|
|
|
# Initialize HIL
|
|
hil = SCD30_HIL_Lib(EMULATOR, TARGET)
|
|
|
|
print(f"--- SCD30 HIL Test Started ---")
|
|
print(f"Logging to: {LOG_FILE}")
|
|
|
|
with open(LOG_FILE, mode='w', newline='') as f:
|
|
writer = csv.writer(f)
|
|
writer.writerow(["Timestamp", "Simulated_CO2", "Target_Received_Msg", "Status"])
|
|
|
|
# Test cases: varying CO2 levels
|
|
test_points = [400.0, 850.5, 1200.0, 2500.0, 5000.0]
|
|
|
|
try:
|
|
for co2 in test_points:
|
|
print(f"\n[PHASE] Setting Simulated CO2 to {co2} ppm")
|
|
|
|
# Flush buffers to ensure we don't read old data
|
|
hil.emulator.reset_input_buffer()
|
|
hil.target.reset_input_buffer()
|
|
|
|
# 1. Feed the bridge AND listen for Target output simultaneously
|
|
# The Master might retry multiple times, so we must keep the bridge alive
|
|
# until we see a final result from the Target.
|
|
timeout = time.time() + 10
|
|
data_served = False
|
|
target_msg = ""
|
|
|
|
while time.time() < timeout:
|
|
# Service the Bridge
|
|
if hil.process_bridge(co2, 24.0, 50.0):
|
|
data_served = True
|
|
|
|
# Check for Target Output
|
|
if hil.target.in_waiting:
|
|
line = hil.get_target_reading()
|
|
if line:
|
|
# We are looking for either a successful reading or a final failure message
|
|
# Success example: "CO2: 400.00 ppm..."
|
|
# Warning example: "W (123) SCD30: CRC failed..." (We might want to keep going if it's just a warning)
|
|
# Error example: "E (123) SCD30: ..."
|
|
|
|
# Ideally, we wait for the formatted output
|
|
if "CO2:" in line or "Read data failed" in line:
|
|
target_msg = line
|
|
break
|
|
else:
|
|
# It might be a warning retry log, let's capture it but keep going
|
|
# If we overwrite target_msg, we might miss the final success
|
|
# But for debugging, let's print it
|
|
print(f"[LOG] {line}")
|
|
|
|
if not target_msg:
|
|
if not data_served:
|
|
print("Error: Target didn't request data within 10s.")
|
|
else:
|
|
print("Error: Target requested data but yielded no final result.")
|
|
continue
|
|
|
|
# 3. Simple Validation
|
|
status = "PASS" if str(int(co2)) in target_msg else "CHECK"
|
|
|
|
print(f"[RESULT] Target Output: {target_msg}")
|
|
writer.writerow([time.strftime("%H:%M:%S"), co2, target_msg, status])
|
|
f.flush()
|
|
|
|
except KeyboardInterrupt:
|
|
print("\nTest Aborted.")
|
|
finally:
|
|
hil.close()
|
|
print("--- HIL Terminated ---") |