Serial Communications (Serial Class)

This library allows communication between the GR-SAKURA board and PCs or other microcontroller devices. With a total of 8 serial channels available, 1 channel is USB communication other 7 channels are UART communication. The followings describe channels corresponding to pins, also you can confirm that in 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

Describe Gets the number of bytes (characters) available for reading from the serial port.
Syntax int Serial.available()
Parameter 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,000 bps);
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 It has a 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 case of setting baud rate larger than above, it is better to set following baud rate, 500000, 1000000, 1500000, 3000000(bps).

end

Description Disables serial communication port.
Syntax Serial.end()
Parameter 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 communcations port without removing it from the receive buffer. CRLF conversion is not carried out.
Syntax int Serial.peek()
Parameter 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 th number base for values (BIN: binary or base 2, OCT: octal or base 8, DEC: decimal or base 10, HEX: hexadecimal or based 16)
Returns The number of bytes printed

println

Desscription 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 based 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
}

Made by Gadget Renesas Project
Contents are CC BY-SA 3.0