B4R Tutorial Additional Hardware Serial Ports

Some boards like Arduino Mega and Arduino Due have several hardware serial ports. These ports are very useful if you need to interact with multiple modules. The alternative for multiple hardware ports is software serial ports: https://www.b4x.com/android/forum/t...s-with-rsoftwareserial-library.65779/#content

Software serial ports are more limited, especially if more than one is needed.
The names of the serial ports are Serial1, Serial2 and Serial3 (on Mega and Due). Each one is tied to two specific pins.
Accessing the additional ports requires a short inline C code.

1. Add a global variable named SerialNative1.
Private SerialNative1 As Stream

2. Add this inline C code:
#if C
void SerialNative1(B4R::Object* unused) {
 ::Serial1.begin(9600); //<--You can change the baud rate
 b4r_main::_serialnative1->wrappedStream = &::Serial1;
#end if

3. Call the C code in AppStart:
RunNative("SerialNative1", Null)

Now you can pass SerialNative1 to AsyncStream and work with it exactly like you work with any other stream.
Last edited: