Can send multiple characters through serial?

4 vues (au cours des 30 derniers jours)
Yeoh
Yeoh le 22 Juin 2011
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 commentaire
Jan
Jan le 22 Juin 2011
What does happen for "fprintf(s,'boy')"? Did you setup the serial connection with the correct parameters?

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 22 Juin 2011
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 commentaires
Yeoh
Yeoh le 23 Juin 2011
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 le 23 Juin 2011
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Messages dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by