Can send multiple characters through serial?

2 次查看(过去 30 天)
Hi,
I wish to know can I send something like below through serial RS232 from my MATLAB GUI to microcontroller?
fprintf(s,'boy');
In normal case, we send single character such as:
fprintf(s,'b');
then microcontroller detect a 'b', it will do something. But I have to send multiple characters instead of single character. I wish to know my microcontroller will recognize as what input data, 'b'? 'boy'? Because I try all, it seems nothing related to 'boy' or 'b'.
Thank you.
  1 个评论
Jan
Jan 2011-6-22
What does happen for "fprintf(s,'boy')"? Did you setup the serial connection with the correct parameters?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2011-6-22
That can happen if your port speed is too high for the receiver to be able to handle.
The receiver might be rated to handle that speed, but handling more than one character at that speed might require that some kind of FIFO queue be enabled or might require that the receiver be in DMA mode instead of in interrupt mode.
Using hardware flow control can help.
Note that when you use
fprintf(s,'b')
and s is a serial port, then that is defined to be the same as
fprintf(s,'%s\n','b')
with the line terminator sent after the 'b'. Likewise, fprintf(s,'boy') would send the line terminator after the 'boy'. If you do not want the line terminator sent, use
fprintf(s,'%s','boy')
or
fwrite(s,'boy')
  2 个评论
Yeoh
Yeoh 2011-6-23
Actually the whole serial coding for my matlab GUI is as below:
>>delete(instrfind);
>>pause(0.1);
>>s = serial('COM1');
>>set(s,'BaudRate',19200,'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none','Terminator','CR/LF');
>>fopen(s);
>>fprintf(s,'boy');
>>pause(0.1);
>>fclose(s);
>>delete(s);
>>clear s;
So I already send the line terminator. So did you mean that with the line terminator, serial data out from MATLAB will be 'boy' instead of 'b'? thank you.
Walter Roberson
Walter Roberson 2011-6-23
In that configuration,
fprintf(s,'boy')
would be the same as sending 'boy' followed by carriage return and line feed.
http://www.mathworks.com/help/techdoc/ref/serial.fprintf.html
... "fprintf(obj,'cmd') writes the string cmd to the device connected to the serial port object, obj. The default format is %s\n. The write operation is synchronous and blocks the command-line until execution completes."
Most systems can handle 19200, but especially if you have longer cables or an electromagnetically harsh environment, you can end up losing characters at 19200. Hardware flow control would be better.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by