Arduino Serial Ports
UART0 is the default serial port which is used for serial programming.
Some arduino’s have more than one serial port and these are called Serial1, Serial2, etc
Setup Serial Port
//----- SETUP SERIAL PORT -----
Serial.begin(9600);
while(!Serial)
; //Leonardo/Micro should wait for serial init
//or
Serial2.begin(9600);
//or
Serial.begin(9600, SERIAL_8N2); //SERIAL_8E1, SERIAL_8O1, etc
Sending ASCII Text
Serial.print("Hello"); //Write string no new line
Serial.println(my_variable); //Write a value with line break at end
Sending Variables As ASCII
Serial.print(MyVariable, DEC); //int value
Serial.print(MyVariable, HEX); //hex value
Serial.println(1.23456, 2) //Limit decimal places (gives "1.23")
Others see here.
Write Binary Data
Serial.write(0x01);
Receive Binary Data
int DataRx;
if (Serial.available() > 0) //Gets the number of bytes available for reading from the serial port (buffer holds up to 64 bytes)
{
DataRx = Serial.read(); //Read next byte (returns -1 if no data available)
}
