import re import math import serial # import pyserial import serial.tools.list_ports as sertools import time # serial_port_get = serial.Serial() # # print(serial_port_get.name) # check which port was really used # serial_port = serial.Serial('COM1') # serial_data = serial_port.read() # print(serial_data) # conductivity_coeffs_d27 = [1, 2, 3] # Conductivity calibration coefficients for D27 conductivity_coeffs = [-0.0434792, -15.7302001, 0.02209415 ] # conductivity_coeffs = [10.15469646, -26.440727796, 34.1546946 -12.40456] # P015 instrument_baudrate = 115200 ports = sertools.comports() ser_comport = ports[0].device ser = serial.Serial(ser_comport, baudrate=instrument_baudrate, bytesize=serial.EIGHTBITS, timeout = 0.5) # Test Execution setup test_execution_time = 30 start_time = time.time() def read_channels(ser, channels = (1, 2, 3, 4, 5, 6), delay = 1.0): readings = {} for ch in channels: ser.write(b'j {ch}\r\n'.encode()) time.sleep(delay) _ = ser.readline() # Flush response ser.write(b'd\r\n') # Read data from channel time.sleep(delay) data = ser.readline().decode().strip() readings[ch] = data return readings # sensor_data = read_channels(ser) # print(sensor_data) while time.time() - start_time <= test_execution_time: # print("Count is:", count) # Indented block inside the loop # Send command to read from multiplex channel ser.write(b'j 2\r\n') time.sleep(1.0) serial_data = ser.readline() #print(serial_data) # Read the data from the corresponding channel ser.write(b'd\r\n') time.sleep(1.0) serial_data = ser.readline() print(serial_data) # Decode the byte string to regular string serial_data_regular_string = serial_data.decode() # Sort the resistance values for conductivity and temp resistance_values = re.findall(r'RzMag:\s*([\d.]+)', serial_data_regular_string) print(resistance_values) # Convert string to float # resistance_values_float = [float(val) for val in resistance_values] # conductivity_resistance = resistance_values_float[0] # pt1000_resistance = resistance_values_float[1] - 10 # Calculation of Conductivity from resistance measurement: # conductivity_value = conductivity_coeffs[2] * (1000 / conductivity_resistance) + conductivity_coeffs[1] * ( # pt1000_resistance / conductivity_resistance) + conductivity_coeffs[0] * (pt1000_resistance / 1000) # + conductivity_coeffs[3] # Calculation of temperature from PT1000 resistance measurement: # temperature_value = - (math.sqrt(17.59246 - 0.00232 * pt1000_resistance) - 3.908) / 0.00116 # time_data = time.time() - start_time # print(f"Time (in Seconds) = {time_data}") # print(f"Conductivity (in us/cm) = {conductivity_value},\nTemperature (in Degree C) = {temperature_value}")