Skip to main content

Serial Communications

Serial Communications (Serial Class)

This library allows communication between the GR-ROSE board and PCs or other microcontroller (MCU) devices. There is a total of 8 serial channels available, 1 channel is USB communication and the other 7 channels are UART communication. Serial 1 to Serial 4 can perform single-wire communication for serial servo. Serial 6 is for communication with the ESP8266. Serial 7 is dedicated to RS-485 communication. The corresponding channel and pin numbers are as follows, and can be checked on the pin map.

Serial: USB communication
Serial1: 0(RX), 1(TX/RX)
Serial2: 2(RX), 3(TX/RX)
Serial3: 4(RX), 5(TX/RX)
Serial4: 6(RX), 7(TX/RX)
Serial5: 8(RX), 9(TX)
Serial6: For communication with ESP8266 Wireless module
Serial7: 22(D-), 23(D+)

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 3,000,000bps)
config: Sets data, parity and stop bits. Valid values are:
SERIAL_5N1, SERIAL_6N1, SERIAL_7N1, SERIAL_8N1 (default)
SERIAL_5N2, SERIAL_6N2, SERIAL_7N2, SERIAL_8N2
SERIAL_5E1, SERIAL_6E1, SERIAL_7E1, SERIAL_8E1
SERIAL_5E2, SERIAL_6E2, SERIAL_7E2, SERIAL_8E2
SERIAL_5O1, SERIAL_6O1, SERIAL_7O1, SERIAL_8O1
SERIAL_5O2, SERIAL_6O2, 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

direction

Description
Switch half duplex communication and switch input/output of RS-485 communication.
Syntax
Serial.direction(dir)
Parameters
dir:
In the case of Serial 1 ~ Serial 4
HALFDUPULEX (1 wire, halfduplex)
DUPLEX (2 wire, duplex)
In the case of Serial 7 (RS-485)
OUTPUT
INPUT
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
}