Serial Communications

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

Serial: USB communication
Serial2: ESP32 communication
Serial4: 0(RX), 1(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
}

Sample to communicate with ESP32. You can try AT commands, but not use them for ESP32 programming.


#include <Arduino.h>
void setup() {
    Serial.begin(115200); // for PC
    Serial2.begin(115200); // for ESP
    
    pinMode(PIN_ESP_EN, OUTPUT);
    pinMode(PIN_ESP_IO0, OUTPUT);
    pinMode(PIN_SW0, INPUT);
    pinMode(PIN_SW1, INPUT);
    digitalWrite(PIN_ESP_IO0, HIGH);
    digitalWrite(PIN_ESP_EN, HIGH);
}
    
void loop(){
    if(digitalRead(PIN_SW0)){
        digitalWrite(PIN_ESP_EN, HIGH);
    } else {
        digitalWrite(PIN_ESP_EN, LOW);
    }
    if(digitalRead(PIN_SW1)){
        digitalWrite(PIN_ESP_IO0, HIGH);
    } else {
        digitalWrite(PIN_ESP_IO0, LOW);
    }
    
    if(Serial.available()){
        Serial2.write(Serial.read());
    }
    if(Serial2.available()){
        Serial.write(Serial2.read());
    }
    
}