Using HardwareSerial to send/receive strings over serial
-
- Posts: 1
- Joined: Fri Apr 26, 2019 6:48 pm
Using HardwareSerial to send/receive strings over serial
Hey guys, I am currently trying to setup a serial connection between two esp32 pico kit v4's. I've read up a bit on how the whole HardwareSerial library is supposed to function, but I'm having a tough time getting the communication to work. So far the code I have is just meant to have one esp32 send a character or a string while the other should receive it and display it on the Serial monitor. I'll attach the code I have so far for both sides in hopes that someone here can point me in the right direction! Thanks in advance for any help you guys can lend me!
-
- Posts: 166
- Joined: Wed Aug 01, 2018 12:06 pm
Re: Using HardwareSerial to send/receive strings over serial
Here is the ESP32 code I use to send serial:
Here is the code used to receive serial
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.
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 )
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 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.
Re: Using HardwareSerial to send/receive strings over serial
Thank you very much.
Who is online
Users browsing this forum: No registered users and 43 guests