Reading databurst by serial2

Henk van Beek
Posts: 1
Joined: Wed Feb 12, 2020 3:11 pm

Reading databurst by serial2

Postby Henk van Beek » Fri Feb 14, 2020 6:07 pm

I am using a ESP32 WROOM-32 with arduino IDE 1.8.10 with
I am trying to read a databurst from a MT382, an electrical energy meter.
It is real burst 325 chars of 9600 bps.
Basically it is very simple:
  1. #define RXD2 16
  2. #define TXD2 17
  3.  
  4. #define Led_Catch 5
  5.  
  6. {
  7.   pinMode(Led_Catch, OUTPUT);
  8.  
  9.   // Setup serial ports
  10.   Serial.begin(115200);
  11.   Serial.println("Uart0 started 115200 bps.");
  12.  
  13.   Serial2.begin(9600, SERIAL_8N1, 16, 17);
  14.   Serial.println("Uart2 started 9600 bps streight.");
  15. }
  16.  
  17. void loop()
  18. {
  19.   char c;
  20.   while (Serial2.available() > 0)
  21.   {
  22.     digitalWrite(Led_Catch,HIGH);
  23.     c = Serial2.read();
  24.     Serial.print(c);
  25.     digitalWrite(Led_Catch,LOW);
  26.   }
  27.  
  28.   delay(1000);  
  29. }
Data does not come consecutive.
Connecting my logic analyzer:
Image

one can see: Serial2.availble becomes active after about 100 chars.
trace 1: Incoming dat 9600 bps UART2
trace 2: Serial2.read activity
trace 3: Actual output 115200 bps UART0

Even when i send a few chars into UART2, Serial2.availble becomes active much later than the first incoming char.
Attachments
Problem1.jpg
Problem1.jpg (52.59 KiB) Viewed 4434 times

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

Re: Reading databurst by serial2

Postby idahowalker » Sun Feb 16, 2020 2:20 pm

Sometimes using the Arduino way to get at the ESP32 functions does not work so well, as in the case of A:D conversions. That's when I turn to the ESP32 API https://docs.espressif.com/projects/esp ... /uart.html.

I have found that (Serial2.available() > 0) does not work as well as (Serial2.available() >= 1 ), you may, also, want to figure out of there are data start and data stop characters. Without some sort of start stop thingies, you will end up receiving data streams where you read data out of sequence.

An example of my use of 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 fParseSerial( void * parameters  )

Who is online

Users browsing this forum: Majestic-12 [Bot] and 101 guests