B4R Tutorial Additional Hardware Serial Ports

Discussion in 'B4R Tutorials' started by Erel, May 22, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Now you can pass SerialNative1 to AsyncStream and work with it exactly like you work with any other stream.
    Last edited: Jan 15, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice