Basic Library
Digital I/O
Analog I/O
Advanced I/O
Time
Math
Trigonometry
Random Numbers
Bits and Bytes
Interrupts
Serial Comm.
Standard Library
Ethernet
Ethernet (Server)
Ethernet (Client)
Servo Motor
Stepping Motor
Character LCD
EEPROM
SPI
I2C (Wire)
SD Card
SD (File Operations)
Periodic Operation
Clock (RTC)
Utility
Serial Communications
This library allows communication between the GR-SAKURA 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
Serial1: 0(RX), 1(TX)
Serial2: 7(RX), 6(TX)
Serial3: 26(RX), 24(TX)
Serial4: 5(RX), 3(TX)
Serial5: 8(RX), 9(TX)
Serial6: 11(RX), 12(TX)
Serial7: 60(RX), 58(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,200bps 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
- Notes
- Its margin of error depends on setting baud rate.
About general baud rates, 4800, 9600, 19200, 38400, 57600, 115200, 230400 (bps), the serial communication may not occur (error) within a byte.
But in the case of setting a baud rate larger than what is listed above, it is better to set the following baud rate, 500000, 1000000, 1500000, 3000000 (bps).
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
- 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:
while(!Serial.available());// wait to press key
Serial.read(); //dummy read
}
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
}