Thread Subject: MATLAB - serial communication problem..loss of data

Subject: MATLAB - serial communication problem..loss of data

From: Shrey

Date: 23 Nov, 2009 14:48:23

Message: 1 of 5

I have to move a robot using serial communication. I am accepting 3 bits in my controller. An LED lights up when my controller recieves the 3rd bit.
This works fine when i use it with X-CTU or FlashMagic to communicate serially. But when i do the following in an m-file, the LED glows after recievng the 2nd bit. Thus, its not waiting for the 3rd bit.

o=serial('COM11','BaudRate',9600);
fopen(o);
fprintf(o,'[');
fprintf(o,'F');
fprintf(o,'2');

The LED glows after recieving 'F' in matlab. On the other hand, if i use some other software to communicate serially it glows after recievin '2'. Can anyone please tell me why this is happening??? Really need help and quickly. I have an image processing event coming up and need matlab to communicate serially as well.

Subject: MATLAB - serial communication problem..loss of data

From: Walid Abdelfatah

Date: 23 Nov, 2009 18:23:19

Message: 2 of 5

Hello Shrey,

From your code, you didn't specify the Terminator character which is by default 'LF' (newline), that means that when you use fprintf, '\n' is by default appended to the message you're sending.

You can simply, specify the Terminator character to be '' by adding the following line to your code:
set(o,'Terminator','');
before
fopen(o);

Note : You could aslo use another functions which send data to the serial port without appending the terminator character like fwrite() "I guess"

You can find more about fprintf in the following link.
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/serial.fprintf.html

Regards,
 Walid F. Abdelfatah

"Shrey " <rockingsa1991@gmail.com> wrote in message <hee7bm$l18$1@fred.mathworks.com>...
> I have to move a robot using serial communication. I am accepting 3 bits in my controller. An LED lights up when my controller recieves the 3rd bit.
> This works fine when i use it with X-CTU or FlashMagic to communicate serially. But when i do the following in an m-file, the LED glows after recievng the 2nd bit. Thus, its not waiting for the 3rd bit.
>
> o=serial('COM11','BaudRate',9600);
> fopen(o);
> fprintf(o,'[');
> fprintf(o,'F');
> fprintf(o,'2');
>
> The LED glows after recieving 'F' in matlab. On the other hand, if i use some other software to communicate serially it glows after recievin '2'. Can anyone please tell me why this is happening??? Really need help and quickly. I have an image processing event coming up and need matlab to communicate serially as well.

Subject: MATLAB - serial communication problem..loss of data

From: Muhammad Saeed Anwer

Date: 4 Jan, 2010 05:13:04

Message: 3 of 5

i need some code about serial communication with Matlab but fwirte and fprintf not work why?
i want to transfer 'String' data to Microcontroller through Serial Port. i write Matlab code in following form:

s=serial('com1','baudrate',115200);
fopen(s)
fprinf(s,'typeAlpha/r') %'typeAlpha/r' data to transfer
fclose(s)

plz help...................
i need this within 2 days

Subject: MATLAB - serial communication problem..loss of data

From: Steven Lord

Date: 4 Jan, 2010 14:35:55

Message: 4 of 5


"Muhammad Saeed Anwer" <mr_saeedanwer@hotmail.com> wrote in message
news:hhrtcv$pj1$1@fred.mathworks.com...
>i need some code about serial communication with Matlab but fwirte and
>fprintf not work why?

There is no "fwirte" function -- I assume you simply made a typo in this
posting, but I figured I'd call it out in case it was a typo in your code as
well.

> i want to transfer 'String' data to Microcontroller through Serial Port. i
> write Matlab code in following form:
>
> s=serial('com1','baudrate',115200);
> fopen(s)
> fprinf(s,'typeAlpha/r') %'typeAlpha/r' data to transfer

As above, you have a typo here. The function's name is "fprintf" not
"fprinf".

> fclose(s)
>
> plz help...................
> i need this within 2 days

If those typos are not the cause of this problem, you will need to explain
exactly what you mean when you say "fwirte and fprintf not work". Do they
throw a warning? Do they throw an error? Do they crash MATLAB? Do they
cause your computer to spontaneously combust? Post to the newsgroup (please
DON'T email me directly) showing a small piece of code that shows EXACTLY
what you've tried and state SPECIFICALLY what happens when you run that code
and you should receive some help.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: MATLAB - serial communication problem..loss of data

From: Muhammad Saeed Anwer

Date: 5 Jan, 2010 14:23:04

Message: 5 of 5

previous message have some typing mistakes, sorry for that

let i try to explain what exactly happen

i write below code in Matlab

%%%%%%%%%%%%%%%%%%%%%
s=serial('com1','baudrate',115200);
while 1
   fopen(s)
   y=fscanf(s)
   if strcmp(y,'captureImage/r')==1
        fprintf(s,'typeAlpha/r');
   else
        fprintf(s,'Nothing/r');
   end
   fclosed(s);
end
%%%%%%%%%%%%%%%%%%%%%

project is to take image when Microcontroller sends 'captureImage/r' string to PC(Matlab) continuously but in this code fscanf(s) run perfectly but fprintf(s,'typeAlpha/r') is not working. keep in mind that after 'captureImage/r' received then 'typeAlphe/r' sends to the Microcontroller. if i use only

s=serial('com1','baudrate',115200);
 fopen(s);
fprintf(s,'typeAlpha/r');
fclosed(s);

then nothing occur on the screen and even not generating any error but my data 'typeAlpha/r' not send to the microcontroller.

note:
Microcontroller Baudrate =115200

in last i mention that all are right and project is working perfectly by HyperTerminal.


plz help i need this

there is also one problem that is:
when captureImage not received then message occur in following way:

warning: Timeout...............
y='' %blank

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
rs232 Walid Abdelfatah 23 Nov, 2009 13:24:11
serial port Walid Abdelfatah 23 Nov, 2009 13:24:11
serial communic... Shrey 23 Nov, 2009 09:49:24
rssFeed for this Thread

Contact us at files@mathworks.com