Code: Select all
void loop() {
if(Serial2.available() >0) {
Serial.println("Serial2 is available.");
while (Serial2.available()) {
char c = char(Serial2.read());
Radar_String += c;
Serial.print(char(Serial2.read()));
}
Serial.println("Radar_String = " + Radar_String);
}
}
Code: Select all
void fReceiveSerial_LIDAR( void * parameters )
{
bool BeginSentence = false;
char OneChar;
char *str;
str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
// log_i("Free PSRAM before String: %d", ESP.getFreePsram());
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 * ) &str );
xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
//
}
BeginSentence = false;
break;
}
strncat( str, &OneChar, 1 );
}
else
{
if ( OneChar == '<' )
{
strcpy( str, ""); // clear string buffer
BeginSentence = true; // found beginning of sentence
}
}
} // while ( LIDARSerial.available() )
} //if ( LIDARSerial.available() >= 1 )
xSemaphoreGive( sema_ReceiveSerial_LIDAR );
// log_i( "fReceiveSerial_LIDAR " );
// log_i(uxTaskGetStackHighWaterMark( NULL ));
}
free(str);
vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters )
You have a string buffer Radar_String, I have a string buffer str = (char *)ps_calloc(300, sizeof(char) );
You check for a something in the serial buffer Serial2.available() >0, I check for a something in the string buffer if ( LIDARSerial.available() >= 1 ).
You read the serial buffer whiles it has something while (Serial2.available()), I read the string buffer whiles it has something while ( LIDARSerial.available() ).
You pop from the serial buffer with char c = char(Serial2.read());, I pop from the serial buffer with OneChar = LIDARSerial.read();.
You put the received character into a string buffer Radar_String += c;, I put a received character into a string buffer strncat( str, &OneChar, 1 );.
Differences:
I wait for an entire character if ( LIDARSerial.available() >= 1 )
I send data as a sentence, I have a marker for when the sentence begins if ( OneChar == '<' ).
If a partial sentence is received I discard the partial.
I hold data in the string buffer until the sentence end is received so that the serial receive code can do multiple passes. The serial receive code can receive data, there be no more data in the serial buffer, and exit, but the entire message may not have been received. I, preserve, hold the received message and when the next time there is a serial data in the string buffer, I can add the new data to the end of the already received data.
I put the received string into a queue and trigger a parsing task to decode the string buffer when the end of a sentence has been received, if ( OneChar == '>').
I clear the data in the string buffer when a new sentence marker is received, if ( OneChar == '<' ).
I put my string buffer into PSRAM, str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM.