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!