Index: Teensy_Python_scripts/sensorCommands.py =================================================================== diff -u -r449f6936bd3fd84d8c4da61448702ca24015d778 -rffd8133b92274cfd07e4d94068c938534a41813d --- Teensy_Python_scripts/sensorCommands.py (.../sensorCommands.py) (revision 449f6936bd3fd84d8c4da61448702ca24015d778) +++ Teensy_Python_scripts/sensorCommands.py (.../sensorCommands.py) (revision ffd8133b92274cfd07e4d94068c938534a41813d) @@ -21,8 +21,8 @@ while True: serial_data = ser.readline() if b'\x03' in serial_data: + print(serial_data) break - print(serial_data) printTimestamp() @@ -71,38 +71,77 @@ parsePackets.parseMesaurementData(serial_data) ########################################################################################## -def updateMeasurementSettings(ser): - ser.reset_input_buffer() - printTimestamp() - sendCfgCommand(ser, "sinfreq", 10000.0) - sendCfgCommand(ser, "dacpp", 300.0) - sendCfgCommand(ser, "bias", 100.0) +def packMeasurementSettingsCSV(): + # Define measurement settings values + SinFreq = 11000.0 + DacVoltPP = 400.0 + BiasVolt = 200.0 + HstiaRtiaSel = 7 + AdcPgaGain = 2 + DftNum = 256 + ADCAvgNum = 16 + # Create CSV string: mst,,,,,,, + csv_command = f"mst,{SinFreq},{DacVoltPP},{BiasVolt},{HstiaRtiaSel},{AdcPgaGain},{DftNum},{ADCAvgNum}\r\n" + + return csv_command + ########################################################################################## -def sendCfgCommand(ser, param_name, param_value): - """ - Sends a 'cfg' command over UART to configure a specific parameter. - Args: - ser: The serial.Serial object. - param_name: Name of the parameter to configure (e.g., 'SinFreq'). - param_value: Value to set for the parameter (e.g., 1000.0). - """ - # Clear input buffer before sending + +cfgSettingsParam = [ + "sinfreq", + "dacpp", + "bias", + "rtia", + "pga", + "dftnum", + "avgnum" +] + +UPDATE_CFG_STATUS = { + 0: "ERR_UNRECOGNIZED_PARAM", + 1: "NO_CHANGE_TO_TIA", + 2: "NO_CHANGE_TO_PGA", + 3: "NO_CHANGE_TO_DFT_NUM", + 4: "NO_CHANGE_TO_AVG_NUM", + 5: "SUCCESS" +} + +def updateMeasurementSettingsStruct(ser): ser.reset_input_buffer() + printTimestamp() - # Format and send the command - command_str = f"cfg,{param_name},{param_value}\r\n" - print(f"Sending command: {command_str.strip()}") - ser.write(command_str.encode()) + # Get CSV command + csv_command = packMeasurementSettingsCSV() - # Wait briefly for response + print(f"Sending mst command in CSV format: {csv_command.strip()}") + ser.write(csv_command.encode()) + + # Wait for response: expect 7 bytes (one per parameter) time.sleep(0.2) + response = ser.read(7) # MAX_CONDUCTIVITY_MST_PARAM_IDX = 7 - # Read and print response - serial_data = ser.readline() - print("Response:", serial_data.decode().strip()) + UPDATE_CFG_STATUS = { + 0: "ERR_UNRECOGNIZED_PARAM", + 1: "NO_CHANGE_TO_TIA", + 2: "NO_CHANGE_TO_PGA", + 3: "NO_CHANGE_TO_DFT_NUM", + 4: "NO_CHANGE_TO_AVG_NUM", + 5: "SUCCESS" + } + if len(response) == len(cfgSettingsParam): + print("Received update status for all parameters:") + for idx, status_byte in enumerate(response): + status_code = int(status_byte) + status_message = UPDATE_CFG_STATUS.get(status_code, "UNKNOWN_STATUS") + param_name = cfgSettingsParam[idx] + print(f"{param_name}: {status_message}") + else: + print(f"Expected {len(cfgSettingsParam)} bytes, received {len(response)} bytes. Response incomplete.") + + ########################################################################################## # Function to read all the sensor values def getMeasurementSettings(ser): @@ -111,12 +150,59 @@ printTimestamp() ser.write(b'k\r\n') time.sleep(0.2) - serial_data = ser.readline() + serial_data = ser.read(28) print(serial_data) printTimestamp() parsePackets.parseMeasurementSettings(serial_data) ########################################################################################## +def updateEEPROMData(ser, *values): + """ + Sends a 'save' command with 1 to N EEPROM values over UART in CSV format, + and interprets the response using Update_EEPROM_Status codes. + + Args: + ser: The serial.Serial object. + *values: A variable number of float or double values to be saved to EEPROM. + """ + UPDATE_EEPROM_STATUS = { + 0: "INVALID_CMD", + 1: "NO_VALID_VALUES", + 2: "TOO_MANY_VALUES", + 3: "WAITING_FOR_CONFIRMATION", + 4: "CONFIRMATION_TIMEOUT", + 5: "ERR_DOUBLE_VALUES", + 6: "ERR_FLOAT_VALUES", + 7: "VALUES_VERIFIED", + 8: "OPERATION_ABORTED", + 9: "INVALID_RESPONSE", + 10: "SUCCESS" + } + + if not values: + print("No values provided to update EEPROM.") + return + + # Construct the CSV command + command = "upe," + ",".join(f"{v:.6f}" for v in values) + "\r\n" + print(f"Sending command: {command.strip()}") + + # Send the command + ser.reset_input_buffer() + ser.write(command.encode()) + + # Wait for response + time.sleep(0.2) + response = ser.readline().decode(errors='ignore').strip() + + try: + status_code = int(response) + status_message = UPDATE_EEPROM_STATUS.get(status_code, "UNKNOWN_STATUS") + print(f"EEPROM Update Response Code: {status_code} - {status_message}") + except ValueError: + print(f"Unexpected response: {response}") + +########################################################################################## # Function to read all the sensor values def getEEPROMdata(ser): # Read the data from the corresponding channel