Effacer les filtres
Effacer les filtres

inconsistent connection to serial port

2 vues (au cours des 30 derniers jours)
Anders Enrico Krog
Anders Enrico Krog le 22 Avr 2024
I am trying to read the current values from a HP-34001A DMM. the code run fine the first time i run it, but if i try to run it again i get the warning:
Warning: The specified amount of data was not returned within the Timeout period for
'readline'.
'serialport' unable to read any data. For more information on possible reasons, see
serialport Read Warnings.
Failed to read data from the serial port.
If i restart MatLab and restart the DMM the code will run agian 1 time and i again get the error. The DMM also displays 'ERROR'
Here is my code:
function DMM_Collection()
clc
clear
% Create the serial port object
serialPort = '/dev/tty.usbserial-1430'; % Change this to your serial port
serialObject = serialport(serialPort, 9600);
try
configureTerminator(serialObject, "LF");
fopen(serialObject);
catch fopenError
fprintf('Error opening serial port: %s\n', fopenError.message);
return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the instrument in remote mode
try
writeline(serialObject, 'SYSTEM:REMOTE');
catch remoteModeError
fprintf('Error setting remote mode: %s\n', remoteModeError.message);
fclose(serialObject);
return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the time span and interval for data collection
stopTime = datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS') + seconds(10);
timeInterval = 0.005;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preallocate arrays
maxIterations = ceil(seconds(stopTime - datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS')) / timeInterval);
time = zeros(1, maxIterations);
current = zeros(1, maxIterations);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Collect data
count = 1;
while datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS') < stopTime
time(count) = now;
writeline(serialObject, 'MEAsure:CURRent:DC?');
% Read data from serial port
data = readline(serialObject);
% Check if data is read successfully
if ~isempty(data)
current(count) = str2double(data);
% Print timestamp and current
fprintf('Timestamp: %s, Current: %f\n', datestr(time(count), 'yyyy-MM-dd HH:mm:ss.FFF'), current(count));
count = count + 1;
else
fprintf('Failed to read data from the serial port.\n');
end
pause(timeInterval);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put the instrument in local mode
try
writeline(serialObject, 'SYSTEM:LOCAL');
catch localModeError
fprintf('Error setting local mode: %s\n', localModeError.message);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clean up the serial object
try
%fclose(serialObject);
delete(serialObject);
catch cleanupError
fprintf('Error cleaning up serial object: %s\n', cleanupError.message);
end
end

Réponses (1)

Anurag Ojha
Anurag Ojha le 30 Avr 2024
Hello Andres
I found this MATLAB documentation page that provides probable causes and troubleshooting methods of the warning message "Serialport Warning - Unable to Read Any Data". I am adding the documentation link below, I hope this helps you to resolve your query

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by