Serial.readBytes()

Description

  • Cette fonction lit les caractères d'un port série et les place dans une variable de stockage (buffer). La fonction s'arrête si la longueur de chaîne voulue (c'est à dire le nombre de caractères voulu) a été lu, ou si le temps limite est écoulé. Voir la fonction Serial.setTimeout().
  • Cette fonction renvoie le nombre de caractères qui ont été stockés dans la variable buffer. 0 signifie qu'aucune donnée valide n'a été trouvée.

Syntaxe

Serial.readBytes(buffer, length)

Paramètres

  • Serial: tout objet d'une classe utilisant la classe Serial
  • buffer: la variable de stockage des octets entrants (tableau de char[] ou de byte[])
  • length: le nombre d'octets (ou caractères) à lire (int)

Valeurs Renvoyées

  • Byte : Cette fonction renvoie le nombre de caractères qui ont été stockés dans la variable buffer. 0 signifie qu'aucune donnée valide n'a été trouvée.

Exemple

const int BUFFER_SIZE = 50; char buf[BUFFER_SIZE]; void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available if (Serial.available() > 0) { // read the incoming bytes: int rlen = Serial.readBytes(buf, BUFFER_SIZE); // prints the received data Serial.print("I received: "); for(int i = 0; i < rlen; i++) Serial.print(buf[i]); } }
  • Type "HELLO" on Serial Monitor and click Send button:
  • COM6
    Send
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • The result on Serial Monitor:
  • COM6
    Send
    I received: HELLO
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

※ Remarque:

  • Serial.readBytes() may read a stream of bytes in multiple times (resulting in multiple fragments). To read a stream of bytes at one time, there are two ways:
    • Based on the delimiter: Use Serial.readBytesUntil() with delimiter.
    • Based on the fixed length: Wait until the fixed number of bytes is available. for example:
const int BUFFER_SIZE = 5; char buf[BUFFER_SIZE]; void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available if (Serial.available() >= BUFFER_SIZE) { // read the incoming bytes: Serial.readBytes(buf, BUFFER_SIZE); // TODO: PROCESS THE INCOMING DATA HERE } }
  • Make sure that the read length does NOT exceed the buffer size. The below is the best practice:
const int BUFFER_SIZE = 5; char buf[BUFFER_SIZE]; // application buffer void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available int rxlen = Serial.available(); // number of bytes available in Serial buffer if (rxlen > 0) { int rlen; // number of bytes to read if (rxlen > BUFFER_SIZE) // check if the data exceeds the buffer size rlen = BUFFER_SIZE; // if yes, read BUFFER_SIZE bytes. The remaining will be read in the next time else rlen = rxlen; // read the incoming bytes: rlen = Serial.readBytes(buf, rlen); // TODO: PROCESS THE INCOMING DATA HERE } }
  • Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:
    • An Arduino board and other Arduino board
    • An Arduino board and other sensors/devices
    • An Arduino board and computer (any Serial software on computer)

Voir Également

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • Any suggestion, correction, and translation? please email us at ArduinoGetStarted@gmail.com, We appreciate it
  • We mainly keep improving the references in English. See English version of this page for the latest update.