ESP32 Serial.
3 Serial ports (0), (1), (2), that you can use. Leave Serial1 for the serial monitor. Serial (0) is a bit tricky to use. Which leaves (1) and (2); not to confused with Serial1 and Serial2.
You'll want to have a: #include <HardwareSerial.h> as part of the includes.
You'll want to define your serial devices:
HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialContoller( 2 );
////// serial(1) = pin27=RX green, pin26=TX white
////// serial(2) = pin16=RXgreen , pin17=TX white
#define SerialDataBits 115200
Serial (2) is at pins 16 for rx and 17 for tx. Serial (1) is defaulted to pins that you should not, at this time, use. So you'll want to redefine your (1) pins in the setup.
In setup you'll want to begin your serial ports:
Serial.begin( SerialDataBits );
SerialController.begin( SerialDataBits );
SerialTFMini.begin( SerialDataBits, SERIAL_8N1, 27, 26 );
Notice that for serial (1) I defined the rx and tx pins.
After that, use as you wish.
Receiving Serial with the ESP32:
void fReceiveSerial_LIDAR( void * parameters )
{
bool BeginSentence = false;
sSerial.reserve ( StringBufferSize300 );
char OneChar;
for ( ;; )
{
EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
if ( LIDARSerial.available() >= 1 )
{
while ( LIDARSerial.available() )
{
OneChar = LIDARSerial.read();
if ( BeginSentence )
{
if ( OneChar == '>')
{
if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
{
xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
//
}
BeginSentence = false;
break;
}
sSerial.concat ( OneChar );With the
}
else
{
if ( OneChar == '<' )
{
sSerial = ""; // clear string buffer
BeginSentence = true; // found beginning of sentence
}
}
} // while ( LIDARSerial.available() )
} //if ( LIDARSerial.available() >= 1 )
xSemaphoreGive( sema_ReceiveSerial_LIDAR );
}
vTaskDelete( NULL );
} //void fParseSerial( void * parameters )
Note my use of if ( LIDARSerial.available() >= 1 ) and not if ( LIDARSerial.available() >= 0 ). I found that with the Due, STM32, and ESP32 a "1" works more reliably then a "0".