Clear Filters
Clear Filters

inconsistent connection to serial port

2 views (last 30 days)
Anders Enrico Krog
Anders Enrico Krog on 22 Apr 2024
Answered: Anurag Ojha on 30 Apr 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

Answers (1)

Anurag Ojha
Anurag Ojha on 30 Apr 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

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!