Here is the ESP32 code I use to send serial:
Code: Select all
#include <HardwareSerial.h>
HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialBrain( 2 );
////// serial(1) = pin27=RX green, pin26=TX white
////// serial(2) = pin16=RXgreen , pin17=TX white
#define SerialDataBits 115200
void setup()
{
SerialBrain.begin( SerialDataBits );
SerialTFMini.begin( SerialDataBits, SERIAL_8N1, 27, 26 );
}
void fSendLIDAR_InfoSerialToBrain( void *pvParameters )
{
struct stu_LIDAR_INFO pxLIDAR_INFO;
for ( ;; )
{
xEventGroupWaitBits (eg, evtGetIMU, pdTRUE, pdTRUE, portMAX_DELAY);
xSemaphoreTake( sema_LIDAR_FOR_ALARM, xSemaphoreTicksToWait );
xQueueReceive ( xQ_LIDAR_FOR_ALARM, &pxLIDAR_INFO, QueueReceiveDelayTime );
xSemaphoreGive( sema_LIDAR_FOR_ALARM );
int CellCount = 1 ;
// send LIDAR info for alarm
String sSerial = "";
sSerial.reserve ( 300 );
sSerial.concat( "<#," ); //sentence begin
sSerial.concat( String(ScanPoints) + "," );
sSerial.concat( String(pxLIDAR_INFO.ServoSweepUp) + "," ); // get direction of scan
for ( CellCount; CellCount <= ScanPoints; CellCount++ )
{
sSerial.concat( String(pxLIDAR_INFO.Range[CellCount]) + "," );
}
sSerial.concat( ">" ); //sentence end
vTaskDelay( 10 );
SerialBrain.println ( sSerial );
}
vTaskDelete( NULL );
} // void fSendLIDAAR_InfoSerialToBrain( void *pvParameters )
Here is the code used to receive serial
Code: Select all
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);
// vTaskDelayUntil( &xLastWakeTime, xFrequency );
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 );
}
else
{
if ( OneChar == '<' )
{
sSerial = ""; // clear string buffer
BeginSentence = true; // found begining of sentence
}
}
} // while ( LIDARSerial.available() )
} //if ( LIDARSerial.available() >= 1 )
xSemaphoreGive( sema_ReceiveSerial_LIDAR );
// Serial.print( "fReceiveSerial_LIDAR " );
// Serial.print(uxTaskGetStackHighWaterMark( NULL ));
// Serial.println();
// Serial.flush();
}
vTaskDelete( NULL );
} //void fParseSerial( void * parameters )
The ESP32 has 3 serial ports, forget about Serial port (0), which leaves serial(1) and serial(2), not to be confused with Serial1 and Serial2.
The default pin location for serial(2) is fine, serial(1) needs to have its pins reassigned; as shown in the setup.
Note the line to receive: if ( LIDARSerial.available() >= 1 ), I found that the Arduino DUE, STEM32, and ESP32 work better with a 1 instead of a 0.
I send and receive sentences. Each sentence starts with a "<" and ends with a ">" so that if the serial starts receiving data in the middle of a sentence the sentence is ignored.
The receive serial task is being triggered once a millisecond by a hardware timer.