Serial Communications (Serial Class)

This library allows communication between the GR-COTTON board and PCs or other microcontroller (MCU) devices. With a total of three serial channels available, the user can specify Serial, Serial1 and Serial2.

If these serial ports are missing, try SoftwareSerial.

Serial2 can not be used with SPI at the same time.

begin

Description

Sets the data rate in bits per second (baud).

Syntax

Serial.begin(baud)
Serial.begin(baud, config)
Serial.begin(baud, config, rx_buf, tx_buf)

Parameters

baud: Baudrate
config: Data bit width, parity, stop bit. Select from the following.
  SERIAL_7N1
  SERIAL_8N1(default)
  SERIAL_7N2
  SERIAL_8N2
  SERIAL_7E1
  SERIAL_8E1
  SERIAL_7E2
  SERIAL_8E2
rx_buf: Buffer size for receive (default 256)
tx_buf: Buffer size for transmit (default 256)

Returns

None

end

Description

Disables serial communication port.

Syntax

Serial.end()

Parameters

None

Returns

None

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.

read

Description

Reads one 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

availableForWrite

Description

Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation.

Syntax

int Serial.availableForWrite()

Parameters

None

Returns

Number of bytes

availableForRead

Description

Get the number of bytes (characters) available for receiving.

Syntax

int Serial.availableForRead()

Parameters

None

Returns

Number of bytes


Sample Program

The data from Serial is output to Serial2. The data from Serial2 is output to Serial.


        #include <Arduino.h>
            
        void setup()
        {
            Serial.begin(9600);
            Serial2.begin(9600);
        }
            
        void loop()
        {
            if(Serial.available())
            {
                char c = Serial.read();
                Serial2.write(c);
            }
            
            if(Serial2.available())
            {
                char c = Serial2.read();
                Serial.write(c);
            }
        }