Extract data from serial port after you disconnect it

1 visualización (últimos 30 días)
Nikan Fakhari
Nikan Fakhari el 5 de Mayo de 2022
Respondida: arushi el 28 de Ag. de 2024
Hi there,
Is there any way to extract data from serial port after you disconnect it?
I have data trasnfering from Arduino to Matlab contniously, however I would like to discconect at some point
and extract the data from the serial object that I made in matlab (with the current BytesAvailable).
I would appreciate the help. Also for this i need to be using the Matlab 2019 version
Nikan

Respuestas (1)

arushi
arushi el 28 de Ag. de 2024
Hi Nikan,
I understand that you have continuous data transfer from an Arduino board to MATLAB, and you want to disconnect at some point but still be able to extract the data that was received up until that point.
In MATLAB 2019, you can extract data from the serial port even after disconnecting it by using the ‘fread’ function. The following code might help, the code has not be tested please make changes accordingly:
% Create a serial port object
s = serial('COM7'); % Replace 'COM7' with your specific port
s.BaudRate = 9600; % Set the baud rate as per your Arduino configuration
% Open the serial port
fopen(s);
% Read data continuously until you want to disconnect
while true
% Check if data is available
if s.NumBytesAvailable > 0
% Read the available data
data = fread(s, s.NumBytesAvailable);
% Process the data as needed
disp(data);
end
% Check if you want to disconnect
if <condition to disconnect>
break;
end
end
% Close the serial port
fclose(s);
In this code, the ‘fread’ function is used to read the available data from the serial port. The ‘BytesAvailable’ property of the serial port object is used to check if there is data available for reading.
You can modify the ‘<condition to disconnect>’ part of the code to specify when you want to disconnect from the serial port. Once the condition is met, the loop will break, and you can continue processing the extracted data.
Please refer to the following MATLAB documentation to know more about ‘fread’ and properties of Serial Port:
Hope the above solution helps you.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by