RUI3 (RAK Unified Interface 3) - RAK4631

Functions

void begin (uint32_t baud, RAK_SERIAL_MODE mode=RAK_DEFAULT_MODE)
 
void end (void)
 
void lock (bool locked)
 
bool password (char *new_passwd, size_t len)
 
virtual size_t write (const uint8_t *buf, size_t size)
 
virtual int available (void)
 
virtual int read (void)
 
virtual int peek (void)
 
virtual void flush (void)
 
virtual uint32_t getBaudrate (void)
 
size_t print (long, int=DEC)
 
size_t println (int, int=DEC)
 
size_t printf (const char *val,...)
 
void setTimeout (unsigned long timeout)
 
unsigned long getTimeout (void)
 
size_t readBytes (char *buffer, size_t length)
 
size_t readBytesUntil (char terminator, char *buffer, size_t length)
 
String readString ()
 
String readStringUntil (char terminator)
 

Detailed Description

Function Documentation

◆ begin()

void begin ( uint32_t  baud,
RAK_SERIAL_MODE  mode = RAK_DEFAULT_MODE 
)
Description
Sets the data rate in bits per second (baud) for serial data transmission
Note
For RAK3172/RAK3172-SiP/RAK3172LP-SiP, Baudrate is only allow maximum 115200
Syntax
Serial.begin(baud);
Serial.begin(baud, mode);
Parameters
baudThe baudrate to set for the Serial
mode(optinal)The mode that use UART in different way (if not assigned, RAK_DEFAULT_MODE is chosen)
List:
RAK_AT_MODE
RAK_API_MODE
RAK_CUSTOM_MODE
RAK_DEFAULT_MODE
Returns
void
Example
void setup() {
  Serial.begin(115200);
  Serial1.begin(9600, RAK_CUSTOM_MODE);
}

void loop() {
}

◆ end()

void end ( void  )
Description
End connection of the Serial with flushing all data.
Syntax
Serial.end();
Returns
void
Example
void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.end();
}

◆ lock()

void lock ( bool  locked)
Description
To lock the Serial port of the device.
Note
1.If you never set a password successfully, the default password will be 00000000
2.Serial.lock can only lock the Serial Which is in AT Command mode.
3.Serial.lock will lock all Serial that is on AT Command mode.
4.Due that Serial lock and unlock is system-wise operation for all Serial ports in AT Command mode, the Serial could be unlocked from either one or more Serial ports which are in AT Command mode.
Syntax
Serial.lock(locked);
Parameters
lockedgiving true to lock the device,false to unlock the device
Returns
void
Example
void setup() {
  Serial.begin(115200);

  //Lock Serial(USB) with password => 12345678
  string password = "12345678";
  Serial.password(password);
  Serial.lock(true);
}

void loop() {
}

◆ password()

bool password ( char *  new_passwd,
size_t  len 
)
Description
Set up the password to unlock the device when locked.
Syntax
Serial.password(str);
Serial.password(new_passwd, len);
Parameters
stra string to set for unlock the device
new_passwdan char array to set for unlock the device
lenthe length your password set
Returns
bool
Return values
TRUEfor success
FALSEfor failure
Example
void setup() {
  Serial.begin(115200);

  //Lock Serial(USB) with password => 12345678
  string password = "12345678";
  Serial.password(password);
  Serial.lock(true);
}

void loop() {
}

◆ write()

virtual size_t write ( const uint8_t *  buf,
size_t  size 
)
virtual
Description
This API is used to write a byte sequence to a specified serial port.
Note
This function is a virtual function that declared in Print.h
Syntax
Serial.write(val);
Serial.write(buf, size);
Parameters
vala value to send as a single byte
bufan array to send as a series of bytes
sizethe number of bytes to be sent from the array
Returns
number of bytes sent successfully(Type: size_t)
Example
void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.write(45);
  Serial.write("Hello");
}

Reimplemented from Print.

◆ available()

virtual int available ( void  )
virtual
Description
This API is used to get the number of bytes available for reading from the specified serial port.
Note
Serial.available() inherits from the Stream utility class
Syntax
Serial.available();
Return values
thenumber of bytes available for reading from the specified serial port(Type: int)
Example
void setup() {
  Serial.begin(115200, RAK_CUSTOM_MODE);
}

void loop() {
  //print if you receive data
  if (Serial.available() > 0) {
    Serial.print("Return Byte = ");
    Serial.println(Serial.read());   
  }
}

Implements Stream.

◆ read()

virtual int read ( void  )
virtual
Description
Reads incoming serial data
Note
Serial.read() inherits from the Stream utility class
Syntax
Serial.read()
Returns
The first byte of incoming serial data available(Type: int32_t)
Return values
-1Read fail,get nothing from the specified serial port
Example
void setup() {
  Serial.begin(115200, RAK_CUSTOM_MODE);
}

void loop() {
  //print if you receive data
  if (Serial.available() > 0) {
    Serial.print("Return Byte = ");
    Serial.println(Serial.read());
  }
}

Implements Stream.

◆ peek()

virtual int peek ( void  )
virtual
Description
Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer. That is, successive calls to peek() will return the same character, as will the next call to read().
Syntax
Serial.peek();
Returns
The first byte of incoming serial data available (or -1 if no data is available)(Type: int)
Example
void setup() {
  Serial.begin(115200, RAK_CUSTOM_MODE);
}

void loop() {
  //print if you receive data
  if (Serial.available() > 0) {

    //Peek the data first
    Serial.print("Peek the Byte = ");
    Serial.println(Serial.peek());

    //Read the data you peeked
    Serial.print("Return Byte = ");
    Serial.println(Serial.read());
  }
}

Implements Stream.

◆ flush()

virtual void flush ( void  )
virtual
Description
Waits for the transmission of outgoing serial data to complete
Syntax
Serial.flush();
Returns
void
Example
void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.write(45);
  Serial.write("Hello");
  Serial.flush();
}

Implements Stream.

◆ getBaudrate()

virtual uint32_t getBaudrate ( void  )
virtual
Description
Get the baud rate from the system configure
Syntax
Serial.getBaudrate();
Returns
void
Example
void setup() {
  uint32_t baudrate = Serial.getBaudrate();
  Serial.begin(baudrate);
}

void loop() {
}

◆ print()

size_t print ( long  ,
int  = DEC 
)
Description
Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is
Syntax
Serial.print(val);
Serial.print(val,format);
Parameters
valthe value to print,Allow any data type
Returns
returns the number of bytes written, though reading that number is optional(Type: size_t)
Example
void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop() {
  // print labels
  Serial.print("NO FORMAT");  // 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.println();        // carriage return after the last label

  for (int 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\t");  // prints two tabs to accomodate the label lenght

    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
} 

◆ println()

size_t println ( int  ,
int  = DEC 
)
Description
Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '') and a newline character (ASCII 10, or '
'). This command takes the same forms as Serial.print().
Syntax
Serial.println(val);
Serial.println(val,format);
Parameters
valthe value to print,Allow any data type
Returns
returns the number of bytes written, though reading that number is optional(Type: size_t)
Example
int analogValue = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog input on pin 0:
  analogValue = analogRead(0);

  // print it out in many formats:
  Serial.println(analogValue);       // print as an ASCII-encoded decimal
  Serial.println(analogValue, DEC);  // print as an ASCII-encoded decimal
  Serial.println(analogValue, HEX);  // print as an ASCII-encoded hexadecimal
  Serial.println(analogValue, OCT);  // print as an ASCII-encoded octal
  Serial.println(analogValue, BIN);  // print as an ASCII-encoded binary

  // delay 10 milliseconds before the next reading:
  delay(10);
}

◆ printf()

size_t printf ( const char *  val,
  ... 
)
Description
to output formated text over Serial without the need to create a char array first, fill it with snprintf() and then send it with Serial.println
Syntax
Serial.printf(val)
Parameters
val
Returns
returns the number of bytes written, though reading that number is optional(Type: size_t)
Example
int exampleNum = 123;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.printf("The example number = %d\r\n", exampleNum);
}
Examples:
BLE_Beacon/src/app.cpp, BLE_Beacon_Custom_Payload/src/app.cpp, BLE_Configuration/src/app.cpp, and System_FS/src/app.cpp.

◆ setTimeout()

void setTimeout ( unsigned long  timeout)
Description
This API is used to set the timeout value for read/write/flush API.
Syntax
Serial.setTimeout(timeout);
Parameters
timeoutthe timeout value
Returns
void
Example
void setup() {
  Serial.begin(115200);

  //Set Timeout to 5000
  Serial.setTimeout(5000);
  Serial.print("Time out = ");
  Serial.println(Serial.getTimeout());
}

void loop() {
}

◆ getTimeout()

unsigned long getTimeout ( void  )
Description
This API is used to get the timeout value for read/write/flush API.
Syntax
Serial.getTimeout();
Returns
The value of timeout(Type: unsigned long)
Example
void setup() {
  Serial.begin(115200);

  //Set Timeout to 5000
  Serial.setTimeout(5000);
  Serial.print("Time out = ");
  Serial.println(Serial.getTimeout());
}

void loop() {
}

◆ readBytes()

size_t readBytes ( char *  buffer,
size_t  length 
)
Description
Read characters from Stream into buffer terminates if length characters have been read, or timeout (see setTimeout)
Note
Serial.readBytes() inherits from the Stream utility class
Syntax
Serial.readBytes(buffer, length);
Parameters
bufferThe buffer to store the bytes in
lengthThe number of bytes to read
Returns
The number of bytes placed in the buffer(Type: size_t)
Example
void setup() {
  Serial.begin(11500, RAK_CUSTOM_MODE);
}

void loop() {
  int returnBytes = 0;
  char readBuf[256];

  if (Serial.available > 0) {

    //Read 5 characters in 1 second
    returnBytes = Serial.readBytes(readBuf, 5);

    if (returnBytes == 0)
      Serial.print("read nothing");
    else {
      Serial.print("read: ");
      for (int i=0; i<returnBytes; i++) {
        Serial.print(readBuf[i]);
      }
    }

    Serial.println("");
  }
}

◆ readBytesUntil()

size_t readBytesUntil ( char  terminator,
char *  buffer,
size_t  length 
)
Description
As readBytes with terminator character Terminates if length characters have been read, timeout, or if the terminator character detected
Note
Serial.readBytesUntil() inherits from the Stream utility class
Syntax
Serial.readBytesUntil(terminator, buffer, length);
Parameters
terminatorThe character to search for
bufferThe buffer to store the bytes in
lengthThe number of bytes to read
Returns
size_t
Example
void setup() {
  Serial.begin(11500, RAK_CUSTOM_MODE);
}

void loop() {
  int returnBytes = 0;
  char readBuf[256];

  if (Serial.available > 0) {

    //Read 5 characters in 1 second,or Press ENTER to end reading
    returnBytes = Serial.readBytesUntil('\r', readBuf, 5);

    if (returnBytes == 0)
      Serial.print("read nothing");
    else {
      Serial.print("read: ");
      for (int i=0; i<returnBytes; i++) {
        Serial.print(readBuf[i]);
      }
    }

    Serial.println("");
  }
}

◆ readString()

String readString ( )
Description
Reads characters from a Stream into a String The function terminates if it times out (see setTimeout()).
Note
Serial.readString() inherits from the Stream utility class\
Syntax
Serial.readString()
Returns
A String read from a Stream(Type: String)
Example
void setup() {
  Serial.begin(115200, RAK_CUSTOM_MODE);
}

void loop() {
  String returnString = "";

  if (Serial.available() > 0) {

    //Read 5 characters in 1 second
    returnString = Serial.readString();

    if (returnString == "")
      Serial.print("read nothing");
    else {
      Serial.print("read: ");
      Serial.println(returnString);
    }

    Serial.println("");
  }
}

◆ readStringUntil()

String readStringUntil ( char  terminator)
Description
readStringUntil() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout())
Note
Serial.readStringUntil() inherits from the Stream utility class
Syntax
Serial.readStringUntil(terminator);
Returns
The entire String read from the serial buffer, up to the terminator character(Type: String)
Example
void setup() {
  Serial.begin(115200, RAK_CUSTOM_MODE);
}

void loop() {
  String returnString = "";

  if (Serial.available > 0) {

    //Read 5 characters in 1 second,or Press ENTER to end reading
    returnString = Serial.readStringUntil('\r');

    if (returnString == "")
      Serial.print("read nothing");
    else {
      Serial.print("read: ");
      Serial.println(returnString);
    }

    Serial.println("");
  }
}