Can send multiple characters through serial?

조회 수: 3 (최근 30일)
Yeoh
Yeoh 2011년 6월 22일
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에서 Messages에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by