B4R Question Remap hardware serial ports on Esp32

janderkan

Well-Known Member
Licensed User
Longtime User
Using this code I can use hardwareserial port on Esp32
B4X:
#if C
void SerialNative1(B4R::Object* unused) {
 ::Serial1.begin(9600); //<--You can change the baud rate
 b4r_main::_serialnative1->wrappedStream = &::Serial1;
}
#end if

I found this code on github
B4X:
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}

void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
{
    if(0 > _uart_nr || _uart_nr > 2) {
        log_e("Serial number is invalid, please use 0, 1 or 2");
        return;
    }
    if(_uart) {
        end();
    }
    if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
        rxPin = 3;
        txPin = 1;
    }
    if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
        rxPin = RX1;
        txPin = TX1;
    }
    if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
        rxPin = RX2;
        txPin = TX2;
    }

    _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
    _tx_pin = txPin;
    _rx_pin = rxPin;

I would like to remap the TX and RX pins to some other pins.

Can anyone help me to change the first C code to include the 2 pin variables?
 

janderkan

Well-Known Member
Licensed User
Longtime User
On the Esp32 the Hardware serial number 1 is default to pin numbers RX=9 / TX=10
My question is: Is the Serial1.begin , from the inline C , calling the HardwareSerial::begin ?
If yes, can I expand the number of parameters to include the rxpin and txpin parameters ?

Remap serial port to other pins.
 
Upvote 0

Michael1968

Active Member
Licensed User
Longtime User
make this change

B4X:
#if C

void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(9600, SERIAL_8N1, 32, 26);;//<--use pin 32/26 as RX/Tx
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…