Serial2 read ESP32 wroomer wrong characters

Hi everyone ,

I am new here but there is a problem thats mabey easy for you to solve.
I have a device that received data over the air and send it to the serial2 of the ESP32.

When i put my serial to usb to that device and start Putty i received the right data .
its only a small text like R1234 nothing special.

But when i put the serial to my ESP32 on Serial2 (16,17) and i open my serialmonitor on the right Baudrate offcourse i get the text i received : and then the wrong data like ⸮⸮⸮v strange characters.

It looks like the ESP must translate it to something , i did try a lott but always the same ouput.

This is a zoom in from my programming :

Serial.begin(115200);     // open seriele poort, stel data snelheid in op 115200 bps
        Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);

and in the loop :

String incoming = "";
    
    if (Serial2.available() > 0) {
    // read the incoming string:
    incoming  = Serial2.readString();
    
    // prints the received data
    Serial.print("I received: ");
    Serial.println(incoming);

its that simple but the characters are wrong ,

Could some one help me with this , i searched everywhere .

Thank you very much

What is the serial monitor baud rate set to?

We request that you post the whole code. Snippets leave out important information. Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

serial mon

/*  Repeater en Pocsag naar IO 2021(C)
  Koppeling met SLR5500 , Minor , Major alarm, Reset knop

  2x RS232 UART , RX0/TX0 en RX2/TX2
*/

const int ledrood = 2 ; //Status LED
const int resetrepeater = 5 ; // Stuurt een reset output naar SUB-D 6
const int knopreset = 4 ; //Drukknop mini onboard
const int DO1 = 25 ; //D12 Teltonika TRB141
const int DO2 = 26 ; //DI1 Teltonika TRB141
const int DO3 = 35 ; // I Teltonika TRB141
const int Major = 23; // Major alarm repeater
const int Minor = 18; // Minor alarm repeater
int knopresetread = 0; //Status knop reset
int DI1Val = 0 ; //status Repeater major
int DI2Val = 0 ; //status Repeater Minor
int t2 = 0; //Timer 2

#include <Arduino.h>
#include <U8x8lib.h>     //Gebruik de u8g2 Bibliotheek
#include "FastLED.h"
#include "Countimer.h"

Countimer tDown; // Countdown Timer

#define NUM_LEDS 1 // Hoeveel leds
#define DATA_PIN 15 // Pin data leds
#define RXD2 16
#define TXD2 17
CRGB leds[NUM_LEDS];


U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(22, 21, U8X8_PIN_NONE); //Adres OLED 0,96 Scherm

void setup() {

  u8x8.begin();
  u8x8.setPowerSave(0);
  u8x8.setFont(u8x8_font_pxplusibmcgathin_f);
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); // type leds Neopixel met controller WS2821B


  pinMode(ledrood, OUTPUT); //rode status led
  pinMode(knopreset, INPUT_PULLUP); //Reset knop onboard
  pinMode(Major, INPUT_PULLUP);
  pinMode(Minor, INPUT_PULLUP);
  pinMode(DO1, OUTPUT);
  pinMode(DO2, OUTPUT);


  Serial.begin(115200);     // open seriele poort, stel data snelheid in op 115200 bps
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  //        Serial.flush();
  //        Serial2.flush();
  Serial.println("Module Actief");

  u8x8.drawString(0, 0, "-------------------------------");
  u8x8.drawString(0, 1, "*********");
  u8x8.drawString(0, 2, "Monitor(C)");
  u8x8.drawString(0, 3, "V1-2021");
  u8x8.drawString(0, 4, "-------------------------------");
  delay (1500);
  u8x8.clearLine(1);
  u8x8.clearLine(3);
  u8x8.clearLine(4);
  u8x8.clearLine(2);
  u8x8.clearLine(0);
  u8x8.clearLine(5);
  u8x8.clearLine(6);
  u8x8.clearLine(7);
  Serial.println("Monitor Actief");

  tDown.setCounter(0, 1, 10, tDown.COUNT_DOWN, tDownComplete);  // Count-down timer ingesteld op : 15min 10sec 0,15,10
  tDown.setInterval(print_time2, 1000); // Stuur print_time2() method Elke 1s.

}
void loop() {


  u8x8.setCursor(0, 7);
  u8x8.print("T2:");
  u8x8.setCursor(3, 7);
  u8x8.print(t2);
  tDown.run();

  String incoming = "";

  if (Serial2.available() > 0) {
    // read the incoming string:
    incoming  = Serial2.readString();

    // prints the received data
    Serial.print("I received: ");
    Serial.println(incoming);
    u8x8.setCursor(0, 5);
    u8x8.println(incoming);
  }

  DI1Val = digitalRead(Major); //Input Repeater SUBD-4 Major
  DI2Val = digitalRead(Minor); // input Repeater SUBD-3 Minor
  //int DI3Val = digitalRead(19); // input Repeater SUBD-2 Reserve

  knopresetread = digitalRead(knopreset);
  //
  //   if  (DI2Val == HIGH){ //
  //   Serial.println("MINOR ALARM");
  //   u8x8.drawString(1,2,"DI1-MINOR ALARM");
  //   delay (500);
  //   }
  //   else if  (DI1Val == HIGH){ //
  //   Serial.println("MAJOR ALARM");
  //    u8x8.drawString(1,3,"DI2-MAJOR ALARM");
  //   delay (500);
  //   }

  if  (knopresetread == LOW) { //
    leds[0] = CRGB::Green;
    FastLED.show();
    u8x8.clearLine(1);
    u8x8.clearLine(3);
    u8x8.clearLine(4);
    u8x8.clearLine(2);
    u8x8.clearLine(0);
    u8x8.clearLine(5);
    u8x8.clearLine(6);
    u8x8.clearLine(7);
    u8x8.drawString(5, 4, "RESET");
    Serial.println("RESET");
    tDown.start();
    digitalWrite (ledrood, LOW);
    digitalWrite (resetrepeater, LOW);
    delay (1000);
    digitalWrite (resetrepeater, HIGH);
    t2 = 0;
    delay (500);
  }

  else {
    leds[0] = CRGB::Red;
    FastLED.show();

    u8x8.drawString(4, 0, "REPEATER");
    u8x8.drawString(0, 1, "-------------------------------");
    u8x8.drawString(1, 2, "DI1-NO ALARM   ");
    u8x8.drawString(1, 3, "DI2-NO ALARM   ");
    u8x8.drawString(0, 4, "-------------------------------");

    u8x8.drawString(0, 6, "-------------------------------");
  }
}


void print_time2()
{
  u8x8.setCursor(5, 7);
  u8x8.print("T1:");
  u8x8.drawString(4, 5, "POCSAG");
  u8x8.setCursor(8, 7);
  u8x8.print(tDown.getCurrentTime());
}

void tDownComplete() //Wat moet er gebeuren als Timer 1 verloopt
{
  t2++ ;
  tDown.restart();

  if (t2 == 2) { //wat moet er gebeuren als Timer 2 verloopt
    u8x8.clearLine(5);
    u8x8.drawString(0, 5, "POCSAG Time OUT");
    digitalWrite (ledrood, HIGH);
    tDown.stop();

  }
}

Sorry for not posting all my code , i thought you did have enough about this alone.
I set the baudrate for the Serialmonitor to 115200.
The communication baudrate of the device is 9600, 8,1 none to serial2

I do the ESP32 Serial thing this way:

#include <HardwareSerial.h>

HardwareSerial GPSSerial ( 1 );
HardwareSerial LIDARSerial ( 2 );

void setup()
{
 LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
  GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial
}

void fParseLIDAR_ReceivedSerial ( void * parameters )
{
  // distribute received LIDAR info
  String sTmp = "";
  sTmp.reserve ( 20 );
  String sMessage = "";
  sMessage.reserve ( StringBufferSize300 );
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
    xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
    //        Serial.println ( sMessage );
    int commaIndex = sMessage.indexOf(',');
    sTmp.concat ( sMessage.substring(0, commaIndex) );
    sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
    //    Serial.println ( sTmp );
    if ( sTmp == "!" )
    {
      xSemaphoreGive ( sema_LIDAR_OK );
      //  Display info from LIDAR
      sLIDAR_Display_Info = sMessage;
    }
    if ( sTmp == "$" )
    {
      xEventGroupSetBits( eg1, evtResetWatchDogVariables );
    }
    if ( sTmp == "#")
    {
      // Serial.println ( "#" );
      xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
      sLIDAR_Alarm_info = sMessage;
      xSemaphoreGive( sema_LIDAR_Alarm );
      xEventGroupSetBits( eg, evtfLIDAR_Alarm );
    }
    //    Serial.println ( "parse serial ok" );
    sTmp = "";
    sMessage = "";
    xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
    //    Serial.print( "fParseReceivedSerial " );
    //    Serial.print(uxTaskGetStackHighWaterMark( NULL ));
    //    Serial.println();
    //    Serial.flush();
  }
  vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )
/////////////////////////////////////////////////////////////////////////////////////////////////////
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);
    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 );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

hope that gives you a few hints and gets you up and running.

Is that pin used for an output?

yes but all that stuff works fine :relaxed::relaxed:

thank for the reply but i want it very simple this looks very complicated for me :roll_eyes:. i only want the string looking into the right characters

I have a device that received data over the air and send it to the serial2 of the ESP32.

When i put my serial to usb to that device and start Putty i received the right data .
its only a small text like R1234 nothing special.

But when i put the serial to my ESP32 on Serial2 (16,17) and i open my serialmonitor on the right Baudrate offcourse i get the text i received : and then the wrong data like ⸮⸮⸮v strange characters.

Is this device 3.3v or 5v?

Was there some possibility that the cable connecting the device to the computer had an rx/tx crossover?

hi , the device has a 12volt , the rx and tx are ok because when i change them they cant communicate . the cable is ok because when i put it on my pc with putty it works . i think i didnt show just right because its alphanumeric

i think i didnt show just right because its alphanumeric

 if (Serial2.available() > 0) {
    // read the incoming string:
    incoming  = Serial2.readString();
    
    // prints the received data
    Serial.print("I received: ");
    Serial.println(incoming);}

Try and determine the length of the incoming String, and print out a hex value byte by byte. You can see what has been received.

the device has a 12volt

What voltage level is the serial output?

If you are not going to properly initialize Serial (2) then do not expect it to work all that well. The ESP32 is not a Uno.

Doesn't the ESP32's Arduino Core have the Serials initialized?

https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/HardwareSerial.cpp

I've not ever had much success doing the other serial port (1) and (2) on a ESP32 the Arduino way.

I would appear to have no trouble with Serial2 the "Arduino way".

When I put the OP's two snippets together I can print correct output received on Serial2 from another device with 3.3v Serial TX/RX output pins.


#define RXD2 16
#define TXD2 17

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

void loop() {
  String incoming = "";

  if (Serial2.available() > 0) {
    // read the incoming string:
    incoming  = Serial2.readString();

    // prints the received data
    Serial.print("I received: ");
    Serial.println(incoming);
  }
}

i transferred it to hex and that all seems to be that every byte is on the screen

cool.

Great. So what do you see as being received?

but the right data never display on the serial monitor. the data received but everytime in wrong characters mabey i must use something else then serial print

This makes no sense to me. If you are receiving printable characters you should see them on the monitor. Perhaps try a different baud rate to the Serial monitor. Is your received string terminated?

When I send "HelloWorld" to the loop code below, I see

48 65 6C 6C 6F 57 6F 72 6C 64 D A
I received: HelloWorld

void loop() {
  String incoming = "";

  if (Serial2.available() > 0) {
    // read the incoming string:
    incoming  = Serial2.readString();
    for(byte i=0; i<incoming.length(); i++)
    {
      Serial.print(incoming[i],HEX);
      Serial.write(' ');
    }
    Serial.println();
    // prints the received data
    Serial.print("I received: ");
    Serial.println(incoming);
  }
}