Extract data from serial port after you disconnect it
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
arushi
le 28 Août 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.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!