Serial - only reads one string.

4 views (last 30 days)
Kristoffer
Kristoffer on 17 Apr 2013
Commented: siva on 31 Jul 2014
Hi
I have a microcontroller with a sensor. The uC sends out a string every tenth of a second to the COM-port. The string is filled with data XXX (Where XXX is 000-999)
Looks like this:
char output_verbose[] = {"XXX\r\n"};
The matlab-code is like this:
s1=serial('COM7','Baudrate',9600);
fopen(s1);
value=0;
str='';
sen=0;
j=1;
while(j<1000)
str=fscanf(s1);
sen=str2num(str);
accX(j)=sen;
plot(j,value);
drawnow;
j=j+1;
end;
fclose(s1);
delete(s1);
clear s1;
The probem after i have debugged a bit, is that when i want to read value number 2, the string is empty. It wont get the string from the COM-port again. And the uC sends the values 10 times a second, this is checked via PuTTY. I also have a LED that blinks everytime it sends.
Please help me

Accepted Answer

Walter Roberson
Walter Roberson on 17 Apr 2013
You have not configured the line terminator for the serial object.
Also your code
while(while(j<1000)
is invalid syntax.
  7 Comments
Walter Roberson
Walter Roberson on 18 Apr 2013
Note: in C, "XXX 000 \r\n" the 000 part is '0' '0' '0', but when you use 'Terminator', 000 in MATLAB, that is the same as 'Terminator', 0 -- a binary 0; in C that would be coded as \0, "XXX \0\r\n"
siva
siva on 31 Jul 2014
hi i also tried the same to interface uc with matlab but the terminator error persists even after i made the above changes coudl u clarify this please

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!