I want to check if I receive a dot ('.') on serial port in Matlab. For this I run for loop for 10 times in which I check if I got '.' on serial port then display "A dot is received" otherwise display whatever is receive. But on receiving '.' on serial port it is not displaying "A dot is receibed". Here is my code:-
s=serial('COM5', 'BaudRate',9600);%, 'DataBits',8, 'Terminator','');
fopen(s);
disp('Port succefully Opened');
count=0;
checkdot = '.';
for x = 1:10
recv= fscanf(s);
z = char(recv);
if (z== '.')
disp('A dot is received');
else
disp(z);
end
end
fclose(s);
And here is my output on command window:-
>> Serialcomm
Port succefully Opened
.
.
.
.
.
.
.
.
.
.
So, please tell me where is the mistake.
You can use
deblank
(removes all whitespace characters) orstrtrim
(removes only leading and trailing whitespace) to get rid of unwanted characters:Also, you're using
fscanf
to retrieve data from the serial port, try usingfgetl
instead.fgetl
gets one line and discards terminators, so should only retrieve the.
(in theory).