Skip to main content

Serial Communications

Serial Communications (Serial Class)

This library allows communication between the GR-PEACH board and PCs or other microcontroller (MCU) devices. With a total of 8 serial channels available, 1 channel is for USB communication and the other 7 channels are for UART communication. The following describe channels corresponding to pins, you can confirm these using the pin map.

Serial: USB communication
Serial0: 0(RX), 1(TX)
Serial1: 41(RX), 42(TX)
Serial3: 25(RX), 26(TX)
Serial4: 8(RX), 9(TX)
Serial5: 7(RX), 6(TX)
Serial6: 22(RX), 23(TX)
Serial7: 63(RX), 64(TX)

available

Description
Gets the number of bytes (characters) available for reading from the serial port.
Syntax
int Serial.available()
Parameters
None
Returns
The number of bytes available in the serial buffer. Returns 0 when there is no data.

begin

Description
Sets the data rate in bits per second (baud) for serial data transmission. An optional second argument configures the data, parity and stop bits. The default is 8 data bits, no parity, one stop bit.
Syntax
Serial.begin(unsigned long speed, config)
Parameters
speed: In bits per second (baud approx. 1,200 to 2,083,333bps)
config: Sets data, parity and stop bits. Valid values are:
  SERIAL_7N1, SERIAL_8N1(default)
  SERIAL_7N2, SERIAL_8N2
  SERIAL_7E1, SERIAL_8E1
  SERIAL_7E2, SERIAL_8E2
  SERIAL_7O1, SERIAL_8O1
  SERIAL_7O2, SERIAL_8O2
Returns
None

end

Description
Disables the serial communication port.
Syntax
Serial.end()
Parameters
None
Returns
None

read

Description
Reads 1 byte of data from the receive buffer in the the serial communications port.
Syntax
int Serial.read()
Parameters
None
Returns
Data. If no data available, returns -1.

peek

Description
Returns the next byte (character) of incoming serial data in the serial communications port without removing it from the receive buffer. CRLF conversion is not carried out.
Syntax
int Serial.peek()
Parameters
None
Returns
The first byte of incoming serial data available (or -1 if no data is available)

flush

Description
Waits until the send buffer of the serial communications port is empty.
Syntax
Serial.flush()
Parameters
None
Returns
None

write

Description
Writes a character string or data to the serial communications port.
Syntax
Serial.write(const char* str)Serial.write(const unsigned char* buf, int len)
Parameters
str: A string to send as a series of bytes
buf: An array (pointer) to send data
len: Length of the output (written) data
Returns
The number of bytes of output (written) data

print

Description
Prints a character string to the serial communications port.
Syntax
Serial.print(val)Serial.print(val, format)
Parameters
val: The value or character string to print
format: Specifies the number base for values (BIN: binary or base 2, OCT: octal or base 8, DEC: decimal or base 10, HEX: hexadecimal or base 16)
Returns
The number of bytes printed

println

Description
Prints a character string to the serial communications port followed by a carriage return.
Syntax
Serial.println(val)Serial.println(val, format)
Parameters
val: The value or character string to print
format: Specifies the number base for values (BIN: binary or base 2, OCT: octal or base 8, DEC: decimal or base 10, HEX: hexadecimal or base 16)
Returns
The number of bytes printed

Sample Program

Sample to print each format.


#include <Arduino.h>
/*
Uses a FOR loop for data and prints a number in various formats.
*/
int x = 0;    // variable
 
void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:
}
 
void loop() {  
  // print labels 
  Serial.print("RAW");       // prints a label
  Serial.print("\t");              // prints a tab
 
  Serial.print("DEC");  
  Serial.print("\t");      
 
  Serial.print("HEX"); 
  Serial.print("\t");   
 
  Serial.print("OCT");
  Serial.print("\t");
 
  Serial.print("BIN");
  Serial.print("\t"); 
 
  for(x=0; x< 64; x++){    // only part of the ASCII chart, change to suit
 
    // print it out in many formats:
    Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"
    Serial.print("\t");    // prints a tab
 
    Serial.print(x, DEC);  // print as an ASCII-encoded decimal
    Serial.print("\t");    // prints a tab
 
    Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal
    Serial.print("\t");    // prints a tab
 
    Serial.print(x, OCT);  // print as an ASCII-encoded octal
    Serial.print("\t");    // prints a tab
 
    Serial.println(x, BIN);  // print as an ASCII-encoded binary
    //                             then adds the carriage return with "println"
    delay(200);            // delay 200 milliseconds
  }
  Serial.println("");      // prints another carriage return
}