Serial Receive in ASCII
4 views (last 30 days)
Show older comments
I am sending and receiving data over serial port the data i am receiving is in ASCII one character after another. i conactenated the characters to get an endless string however I want the last two values from the string which in my case are 42.000 and 2.000
2 Comments
Aiswarya
on 19 Aug 2023
Do you want to create an endless string (of 42 and 2)? Or do you simply want to obtain the result as 42 and 2?
Answers (1)
Raj
on 6 Sep 2023
Hi,
I understand you are trying to exchange data and want to fetch just the last 2 characters of it. Follow the steps stated below to resolve the issue
- Open the serial port connection using the `serial` function in MATLAB. Assign the specific port to the variable
- Use the `fread` function to read the signal from the serial port. You can store the value in the buffer as the length of the data is unknown
- Extract the last 2 characters using MATLAB indexing
- Use the `fclose` function to close the serial port connection.
As I do not have access to your code, an indicative code is here, adapt it with your code accordingly
s = serialport('COM1'); % Replace 'COM1' with the appropriate port name
fopen(s);
b_Size = 1024; % Specify the buffer size
data = [];
while s.BytesAvailable > 0
data = [data; fread(s, b_Size)];
end
lastTwoChars = char(data(end-1:end)');
fclose(s);
Additionally refer to the documentation of fread and Serial receive for better understanding
“Serial receive”- https://www.mathworks.com/help/instrument/serialreceive.html?searchHighlight=serial%20receive&s_tid=srchtitle_support_results_1_serial%20receive
I hope this resolves your query!
0 Comments
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!