On-line pH monitoring of pH meter connected to the USB port

4 次查看(过去 30 天)
Hello everyone, I am quite new in coding and I am struggling in the generation of a robust script which monitors the pH values of a solutions over time. My setup is a pH probe and meter connected using the RS232 - USB port and I want to generate a table time (s) vs pH values as an output since I press a botton (which represents time 0).
Below my current code is pasted. Right now, it works sometimes but doesn't others - it is not robust. It is random, sometimes a time redout error message appears but later the same code has no problems with the redout time. Or it has problems comunicating with the USB port. Could anyone give me any hint on how to generate such a script whihc works every time I run it? I am working with a Jenway 3510 pH meter.
When it works, it is really slow to completely run the lines an show the final graph.
Thank you a lot beforehand!
Code-----------------------
% Define the serial port and baud rate
pH_meter = serialport("COM5", 9600);
% Set a longer timeout value (in seconds)
pH_meter.Timeout = 90; % Set the timeout to 60 seconds (adjust as needed)
% Initialize variables
pHValues = []; % Create an empty array to store pH values
timeValues = []; % Create an empty array to store time values
% Define the acquisition frequency (in seconds)
acquisitionFrequency = 2; % For example, acquire data every 5 seconds. 1 is too much. 5 okay
% Set the total acquisition time (in seconds)
totalAcquisitionTime = 60; % Record data for 60 seconds
% Start the timer
tic;
% Main data acquisition loop
while toc < totalAcquisitionTime
% Send a command to request pH data from the device
writeline(pH_meter, "D");
% Read and process pH data from the device
pH_value = readline(pH_meter, "D");
pH_value = erase(pH_value, "pH");
pH_value = split(pH_value);
pH_value = str2double(pH_value);
pH_value = pH_value(3);
% Store pH value
pHValues = [pHValues, pH_value];
% Record elapsed time in seconds
timeValues = [timeValues, toc];
% Pause for the specified acquisition frequency
pause(acquisitionFrequency);
end
% Close the serial port
%clear pH_meter;
% Create a table with pH and time values
dataTable = table(timeValues', pHValues', 'VariableNames', {'TimeInSeconds', 'pHValue'});
% Display and analyze the collected pH values
disp(dataTable);
% Optionally, you can save the data as a CSV file
writetable(dataTable, 'pH_data.csv');
% Optionally, you can plot the data
figure;
plot(timeValues, pHValues);
xlabel('Time (s)');
ylabel('pH Value');
title('pH vs. Time');

回答(1 个)

Pratyush Swain
Pratyush Swain 2023-10-6
Hi Pau,
I understand you want an efficient script which works successfully without delay. Here are some workarounds to improve performance:
Memory allocation: Preallocate memory for the 'pHValues' and 'timeValues' arrays before the data acquisition loop to improve performance. This can be done by estimating the maximum number of data points that will be collected and initializing the arrays with that size.
%Pre-allocating memory space for array
pHValues = zeros(1,totalAquisitionTime);
timeValues = zeros(1,totalAquisitionTime);
Serial Port Configuration: Verify that the serial port settings, such as baud rate and parity, match the configuration of your pH meter. Refer to the documentation of your pH meter to ensure that the serial port settings are correctly set.
Error Handling: Implement error handling mechanisms to handle potential errors or exceptions that may occur during communication with the pH meter or USB port. You can use try,catch statements for the same. For more information you can refer to https://in.mathworks.com/help/matlab/ref/try.html
Serial Port Cleanup: Make sure to properly close and release the serial port resources after the data acquisition loop is complete. You can try to write an empty matrix to close the port rather than deleting it like:
%Create the object with comport and baudrate. This will open the port.
pH_meter = serialport("COM5", 9600);
%Write an empty matrix to clear the port. This will close the port and let other processes to access it
pH_meter = []
Hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by