serialEvent()

Descripción

Se llama cuando se dispone de datos. Utilice Serial.read(), Serial.readBytes(), Serial.readBytesUntil(), Serial.readString(), or Serial.readStringUntil() para capturar estos datos.

Sintaxis

void serialEvent() { //instrucciones }

Solo Arduino Mega:

void serialEvent1() { //instrucciones } void serialEvent2() { //instrucciones } void serialEvent3() { //instrucciones }

Parámetros

  • instrucciones: cualquier instrucción válida.

Ejemplo

void setup() { Serial.begin(9600); } void loop() { } void serialEvent() { int incomingByte = Serial.read(); // prints the received data Serial.print("I received: "); Serial.println((char)incomingByte); }
  • Compile and upload the above code to Arduino
  • Type "Arduino" on Serial Monitor and click Send button:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Ln 11, Col 1
Arduino Uno on COM15
2
  • The result on Serial Monitor:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
I received: A I received: r I received: d I received: u I received: i I received: n I received: o I received:
Ln 11, Col 1
Arduino Uno on COM15
2

※ Nota:

  • En la actualidad, serialEvent() no es compatible con el Esplora, Leonardo, o Micro

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES