Notice how the code, below, is in code tags and well formatted?
In declarations
#include <HardwareSerial.h>
HardwareSerial GPSSerial ( 1 );
HardwareSerial LIDARSerial ( 2 );
In setup()
LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial
One function using serial one serial port.
void fGPS_Parse( void *pvParameters )
{
// int iBit = 1;
for (;;)
{
xEventGroupWaitBits (eg, evtGPS_Parse, pdTRUE, pdTRUE, portMAX_DELAY) ;
if ( xSemaphoreTake( sema_GPS_Gate, xTicksToWait0 ) == pdTRUE )
{
//query GPS: has a new complete chunk of data been received?
if ( GPSSerial.available() > 1 )
{
if ( GPS.encode(GPSSerial.read()) )
{
if ( GPS.location.isValid())
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.Lat = GPS.location.lat();// store data into structure
xPosit.Lon = GPS.location.lng();
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.location.isValid())
if (GPS.speed.isValid())
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.MPH = GPS.speed.mph();
xPosit.KPH = GPS.speed.kmph();
xSemaphoreGive( sema_Posit );
}
} // if (GPS.speed.isValid())
if (GPS.time.isValid())
{
if ( xSemaphoreTake( sema_Time, xSemaphoreTicksToWait ) == pdTRUE )
{
xTime.iSeconds = GPS.time.second();
xTime.iMinutes = GPS.time.minute();
xTime.iHours = GPS.time.hour();
xSemaphoreGive( sema_Time );
}
} // if (GPS.time.isValid())
if (GPS.date.isValid())
{
if ( xSemaphoreTake( sema_Date, xSemaphoreTicksToWait ) == pdTRUE )
{
xDate.iMonth = GPS.date. month();
xDate.iDay = GPS.date.day();
xDate.iYear = GPS.date.year();
xSemaphoreGive( sema_Date );
}
} // if (GPS.date.isValid())
if ( GPS.altitude.isValid() )
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.Alti = GPS.altitude.meters();
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.altitude.isValid() )
if ( GPS.course.isUpdated() )
{
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xPosit.Hdg = GPS.course.deg();
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.course.isUpdated() )
if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
{
xQueueOverwrite( xQ_Posit, (void *) &xPosit );
xSemaphoreGive( sema_Posit );
}
} // if ( GPS.encode(GPSSerial.read()))
} // if ( GPSSerial.available() > 0 )
xSemaphoreGive( sema_GPS_Gate );
}
} // for (;;)
vTaskDelete( NULL );
} // void fGPS_Parse( void *pvParameters )
Another function using the other serial port
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 fParseSerial( void * parameters )
Using 2 of the 3 serial ports on a ESP32