Unable to check received value on serial port in Matlab

170 views Asked by At

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.

1

There are 1 answers

0
nkjt On BEST ANSWER

You can use deblank (removes all whitespace characters) or strtrim (removes only leading and trailing whitespace) to get rid of unwanted characters:

a = sprintf('.\r\n');
disp(a)
.

strcmp(a,'.')
ans =

     0
strcmp(strtrim(a),'.')
ans =

     1

Also, you're using fscanf to retrieve data from the serial port, try using fgetl instead. fgetl gets one line and discards terminators, so should only retrieve the . (in theory).