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
2011 年 6 月 22 日
What does happen for "fprintf(s,'boy')"? Did you setup the serial connection with the correct parameters?
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!