[SOLVED]Receiving no data from Serial2

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

[SOLVED]Receiving no data from Serial2

Postby GeorgeFlorian1 » Thu Oct 24, 2019 2:20 pm

Board: ESP32-EVB

I am trying to read data from a Radar's RS232 Serial Interface through the UEXT connector on the ESP.

Image

As per the schematics, I am using GPIO 36 as the rxPin and GPIO 4 as the txPin.

Code: Select all

#define RXD2 36
#define TXD2 4

void setup() {
  delay(100);
  Serial.begin(115200);
  delay(1000);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

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);
}
I have also tried replacing Serial2 with USE_SERIAL1 and defined it like this : HardwareSerial USE_SERIAL1(1); but to no avail.

The radar is set up to send data on 9600.

I'm receiving no data. Not even Serial.println("Serial2 is available.");.

Any thoughts ?
Last edited by GeorgeFlorian1 on Mon Oct 28, 2019 10:56 am, edited 1 time in total.

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: Receiving no data from Serial2

Postby mikemoy » Thu Oct 24, 2019 3:51 pm

I am trying to read data from a Radar's RS232 Serial Interface through the UEXT connector on the ESP.
Not knowing what a Radar's RS232 is because you gave no information about it. If the serial output of this device is "RS232" then you wont get anything because the voltage levels are all wrong, and you may have in fact fried your serial pins.

Edje11
Posts: 18
Joined: Thu May 17, 2018 10:33 am
Contact:

Re: Receiving no data from Serial2

Postby Edje11 » Thu Oct 24, 2019 7:42 pm

Isn't it that RXD2 gpio 16 and TXD2 gpio17?

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Receiving no data from Serial2

Postby idahowalker » Thu Oct 24, 2019 10:23 pm

The ESP32 does not have Serial1 or Serial2 or Serialxxx unless you initalize the ports with those names:

Code: Select all

#include <HardwareSerial.h>


HardwareSerial LIDARSerial ( 2 );


const int SerialDataBits = 115200;

setup()
{
LIDARSerial.begin(  SerialDataBits );
// LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 ); to change pin numbers
}
A task to receive serial

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  )
I typically trigger the fReceiveSerial_LIDAR() task once a milliSec.

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: Receiving no data from Serial2

Postby GeorgeFlorian1 » Fri Oct 25, 2019 9:39 am

idahowalker wrote:
Thu Oct 24, 2019 10:23 pm
The ESP32 does not have Serial1 or Serial2 or Serialxxx unless you initalize the ports with those names:

Code: Select all

#include <HardwareSerial.h>


HardwareSerial LIDARSerial ( 2 );


const int SerialDataBits = 115200;

setup()
{
LIDARSerial.begin(  SerialDataBits );
// LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 ); to change pin numbers
}
A task to receive serial

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  )
I typically trigger the fReceiveSerial_LIDAR() task once a milliSec.
I believe that the ESP32's Arduino Core has the Serials initialized:

This is inside HardwareSerial.cpp

Code: Select all

#ifndef RX1
#define RX1 9
#endif

#ifndef TX1
#define TX1 10
#endif

#ifndef RX2
#define RX2 16
#endif

#ifndef TX2
#define TX2 17
#endif

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
HardwareSerial Serial(0);
HardwareSerial Serial1(1);
HardwareSerial Serial2(2);
#endif
Also, why is your task of receiving Serial so much more different than what I have found:

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);
  }
}
Beside that, I have tried actually replacing Serial2 with HardwareSerial USE_SERIAL2(2); and it still doesn't work.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Receiving no data from Serial2

Postby idahowalker » Sat Oct 26, 2019 3:16 pm

Have you tried to get serial working with ESP32 made by someone else? If the code works with a board made by someone else and does not work for the board trying to get working...

Have you tried working with the manufacturer as to why their board is not doing the thing?

Have you tried using the natural pins for (2), RxD GPIO16 and TxD17?

I only use ESP32's from HiLetgo, so far no problems with them.

The 2 receive serial codes do the same thing, mine is just more ESP32-esq.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: Receiving no data from Serial2

Postby idahowalker » Sat Oct 26, 2019 3:39 pm

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.

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: Receiving no data from Serial2

Postby GeorgeFlorian1 » Mon Oct 28, 2019 10:56 am

idahowalker wrote:
Sat Oct 26, 2019 3:39 pm

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.
Wow ! Thank you for the explanation. I will try and implement the same thinking in my code.

I have also fixed the issue. It was a hardware problem with the RS232 connector. The wires were soldered for a FEMALE connector, instead of a MALE connector. Re-soldering them accordingly fixed my issue and now I receive data through serial.

Who is online

Users browsing this forum: No registered users and 130 guests