When we know a little bit on how serial data communication takes place between PC/Serial Monitor and UNO, the program writing job becomes entertaining!1.
Assume that we have placed the cursor in the InputBox of the Serial Monitor. Type the character A
or any other character from the keyboard and then click on the Send button. 2,
The character has ultimately arrived at the Receiver Section of the UART Port of the MCU of the Arduino UNO.3.
The Receiver Section immediately interrupts the MCU; the MCU automatically goes to the
subroutine (the interrupt subroutine, ISR) named serialEvent()
; it reads the the character (the 8-bit ASCII code for A = 0x41) from the receiver and saves into a FIFO type buffer. The buffer is not visible to the programmer; but, Arduino has provided functions to access it. 5.
In the ISR, we can examine the buffer to be sure that the received data is there. After that, we can bring out (read) the data byte from the buffer and send it back to the PC/Serial Monitor. The character A
will appear on the OutputBox of the Serial Monitor.6.
Im presenting below a tested (demonstrative) program with which you can play around. Considering this one as a building block and taking further help from the Forum/Net (Serial Input Basics
) we can develop much more elegant and complex programs on serial communication.
//chracter echo programBTW:
//in the Serail Monitor, keep NL & CR enabled.
//wait here to be interrupted by the Recever Section of UART Port
//whenevr a character arrives
//check that charctaer is saved in the buffer using Serial.avaiable() function
//bring out character from FIFO type buffer into user variable using Serial.read() function
byte x = Serial.read(); //character is read from buffer; the FIFO buffer is empty
//send the charctaer back to PC/Serial Monitor
} //the control goes back to loop(0 function
Can you tell how many characters have actually come from PC to the UNO? If there are 3, then we see only one -- A; where are the other two?