Extract data from serial port after you disconnect it
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Aman Banthia
2023-9-21
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.
Best Regards,
Aman Banthia
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!