import re import math import serial # import pyserial import serial.tools.list_ports as sertools import time import struct import parsePackets from datetime import datetime loop_time = 10 ########################################################################################## # Execute initialization function def initializeAllSensors(ser): ser.reset_input_buffer() # Clears the input buffer printTimestamp() ser.write(b'a\r\n') time.sleep(0.2) while True: serial_data = ser.readline() if b'\x03' in serial_data: break print(serial_data) printTimestamp() ########################################################################################## # Get initialization status def getInitStatus(ser): ser.reset_input_buffer() # Clears the input buffer printTimestamp() ser.write(b'l\r\n') time.sleep(0.2) serial_data = ser.readline() print(serial_data) printTimestamp() ########################################################################################## # Read single sensor value Measurement Vales def readSingleSensorMeasurement(ser, sensorNum): ser.reset_input_buffer() # Clears the input buffer select_unit = f'j {sensorNum}\r\n' printTimestamp() ser.write(select_unit.encode()) time.sleep(0.2) printTimestamp() ser.write(b'h\r\n') # printTimestamp() time.sleep(0.2) # printTimestamp() serial_data = ser.readline() print(serial_data) # printTimestamp() parsePackets.parseMesaurementData(serial_data) ########################################################################################## # Function to read all the sensor values def getAllSensorMeasurements(ser,): # Read the data from the corresponding channel ser.reset_input_buffer() # Clears the input buffer printTimestamp() ser.write(b'g\r\n') printTimestamp() time.sleep(0.2) printTimestamp() serial_data = ser.readline() print(serial_data) printTimestamp() 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 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 ser.reset_input_buffer() # 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()) # Wait briefly for response time.sleep(0.2) # Read and print response serial_data = ser.readline() print("Response:", serial_data.decode().strip()) ########################################################################################## # Function to read all the sensor values def getMeasurementSettings(ser): # Read the data from the corresponding channel ser.reset_input_buffer() # Clears the input buffer printTimestamp() ser.write(b'k\r\n') time.sleep(0.2) serial_data = ser.readline() print(serial_data) printTimestamp() parsePackets.parseMeasurementSettings(serial_data) ########################################################################################## # Function to read all the sensor values def getEEPROMdata(ser): # Read the data from the corresponding channel ser.reset_input_buffer() # Clears the input buffer printTimestamp() ser.write(b'e\r\n') time.sleep(0.2) serial_data = ser.read(128) print(serial_data) printTimestamp() parsePackets.parseEEPROMdata(serial_data) ########################################################################################## # Function to read sensor data for 4 and 6 in loop for 10 seconds def loopSingleSensorReadings(ser): start_time = time.time() # sensor_nums = [4, 6] # index = 0 while time.time() - start_time <= loop_time: # sensor_num = sensor_nums[index % 2] # Alternate between 4 and 6 # readSingleSensorMeasurement(ser, str(sensor_num)) readSingleSensorMeasurement(ser, str(6)) # index += 1 ########################################################################################## # Function to read all sensor data in loop for 10 seconds def loopAllSensorReadings(ser): start_time = time.time() while time.time() - start_time <= loop_time: getAllSensorMeasurements(ser) ########################################################################################## def printTimestamp(): current_time = datetime.now() print("Current timestamp:", current_time) ########################################################################################## def getAllSensorData(ser): ser.reset_input_buffer() printTimestamp() ser.write(b'm\r\n') start_time = time.time() while time.time() - start_time < 100: if ser.in_waiting: serial_data = ser.readline() printTimestamp() parsePackets.parseMesaurementData(serial_data) ser.write(b'n\r\n') printTimestamp() ##########################################################################################