Decoding serial data from module

I have some serial data coming in from a modem that I am trying to decode.

I have the code below. Which most of it is set-up but I am getting some characters and I have tried various ways to read like making g a char or an int but cannot seem to make sense of it. I am expecting just the words OK in the serial port:

#include <HardwareSerial.h>
int greenpower = 32;
int led = 33;
int pwr = 5;


void setup() {

  //pinMode(pwr, OUTPUT); //for turning on quectel module
  pinMode(led, OUTPUT); //for verifying power to the board
  pinMode(greenpower, OUTPUT); //for allowing power to auxillary processes
  delay(1000);
  
  Serial.begin(115200);  // Open main serial port
  delay(1000);
  digitalWrite(greenpower, HIGH); //allow power to aux devices
  Serial.println("powering aux devices");
  delay(1000);
  //digitalWrite(pwr, LOW);  //start power circuit at low voltage
  //Serial.println("initializing power circuit");
  //delay(3000);
  digitalWrite(led, HIGH);  //apply power to verification circuit
  Serial.println("LED ON");
  delay(1000);
  //digitalWrite(pwr, HIGH);  //high voltage pulse to power circuit gpio
  //Serial.println("powering on Quectel BG95");
  //delay(750);
  //digitalWrite(pwr, LOW);  //return power circuit to low voltage
  //Serial.println("exiting powering up mode");
  delay(5000);     //allow quectel to turn on
  
  Serial2.begin(115200,SERIAL_8N1,16,17); //open modem serial port
  Serial.println("serial ports are open");
}

void loop() {
  Serial.println("Started");
  Serial2.write("AT\r\n");
  while (!Serial2.available()) continue;
  while (Serial2.available()) {
    char g = Serial2.read();
    Serial.println(g);
  }
  delay(1000);
}

But I am getting something which looks like:

Does anyone know what I should do?

Have you taken the the time to read serial input basics?

Are you certain that 115200 is the correct line speed for the modem?

My guess is the modem is not set to 115200.


The 115200 is the default baud... @Idahowalker I am reading through the post you sent trying to understand.

Uncompiled, untested

#include <HardwareSerial.h>
const byte greenpower = 32;
const byte led = 33;
const byte pwr = 5;


void setup() 
{
  pinMode(led, OUTPUT); //for verifying power to the board
  pinMode(greenpower, OUTPUT); //for allowing power to auxillary processes
  delay(1000);
  
  Serial.begin(115200);  // Open main serial port
  delay(1000);
  digitalWrite(greenpower, HIGH); //allow power to aux devices
  Serial.println("powering aux devices");
  delay(1000);

  digitalWrite(led, HIGH);  //apply power to verification circuit
  Serial.println("LED ON");
  delay(1000);

  delay(5000);     //allow quectel to turn on
  
  Serial2.begin(115200,SERIAL_8N1,16,17); //open modem serial port
  Serial.println("serial ports are open");
  Serial.println("Started");
  Serial2.write("AT\r\n");
}

void loop() 
{
  if (Serial2.available()) {
    char g = Serial2.read();
    Serial.println(g);
  }
}

serial receiver

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  )

Serial parser

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 );


@TheMemberFormerlyKnownAsAWOL ran the code you had suggested looks like I am getting a bunch of one line symbols. Also I think it is bizarre that the port continues to receive values because I am writing the AT command only in the set-up.

@Idahowalker I am trying to understand what you are suggesting here.

The serial receiver code does not parse the received serial, that I posted.

I use sentences to send and receive serial, if you look at the code to receive serial. See how all the serial received data is ignored until a "<" is received. Once a Serial "<" is received all the received info is stored. Stored until an ">" is received. A ">" means the sentence has ended. At the end of the sentence the thing can be printed as one word.

When you read receive serial did you notice there is a Serial.print("booggyiesrdjgh"; and a Serial.println('gofor thecandysofnsldfknsodghedforgid"): and the difference between the Serial.print() and Serial.println?

The AT manual says something similar to this that all responses begin and end with a carriage return and line feed character. So I assume you are suggesting that I add code that looks for these characters and prints everything in-between?

Also I saw this post which was unresolved but appears to be similar post

image

Do you really need to provide DPin (16, 17) numbers for UART2 Port in the above code when the method requires only two arguments (Fig-1)?

serialbegin; //two arguments
Figure-1:

Interesting point but where else would I specify which pins I am using?

1. From which document have you got the 4-argument begin() method?
2. When Serial2.begin() is executed, the TX2 and RX2 signals of UART2 Port are automatically routed to DPin-16 and DPin-17 respectively. These signals are TTL level; you might need
TTL<--->RS232 Converter (Fig-1) to connect your Modem?

TTLRS232ConverterModule
Figure-1;

3. You are executing: *Serial2.write("AT\r\n"); which pumps thses codes: 41590D0A (ASCII codes for A T CR NL) to your Modem. Are you sure that the Modem requires these codes first before it speaks with Arduino? If yes, then they should be given in the User Manual of the Modem. Can you post a picture of that page of the Manual?

1 Like

Assuming you're using an AVR, HardwareSerial / Serialx knows which pins to use.

Unless it's time to fess up and tell us which processor family you're using. ESP32?

Maybe you also need to use the invert option.

1 Like

Using this flavor ESP32 esp datasheet

Using this modem: Quectel_BG95_Series_Hardware_Design_V1.3 (1).pdf (1.8 MB)
Quectel_BG95BG77BG600L_Series_QCFGEXT_AT_Commands_Manual_V1.0.pdf (589.2 KB)

I am confused why would I need this TTL convertor the datasheet specifically says UART connection.. @TheMemberFormerlyKnownAsAWOL not sure what you mean by invert? As in TXD to RXD? This is done.. @GolamMostafa it seems like I should use Serial2.begin(115200,SERIAL_8N1) and eliminate the 16,17 because serial2 is dedicated to 16,17 hardware pins. I will try this code as soon as I am home.

No, that's crossover.

I meant invert the line condition - I think it is another .begin option.

It is very much likely that your Modem is equipped with UART Interface that needs RS232 Logic for TXD and RXD signals. You might need a RS323 <----> TTL converter (Fig-1). This converter inverts signal logic as per following Table.

Asynchronous TTL Asynchronous RS232
Logic High: 5V Logic High: -3V to -12V (loaded)
Logic Low: 0V Logic Low: +3V to +12V (loaded)


Figure-1:

@GolamMostafa so the ESP's UART connection uses TTL logic, but the modem might need RS232. I figure they would state this in the manual but I did a quick search for "RS232" and "TTL" in both manuals and no hits. I wanted to mention I do have a logic bridge taking this from 3.3V on esp side to 1.8V on modem side.

Also I put an Oscope on this and when one of the random characters hits the serial monitor my scope catches signals being sent and received by the ESP. Also I have determined something else very odd. Without the scope attached all of my readings will register as a blank space. But if I measure with the scope it will be a random character. I think there is a decode option on the scope as well but have never used this.

I don't own ESP32; but, I have ESP8266 NodeMCU whose IO pins are practically 5V tolerant. I am not sure about the IO pins of ESP32 if these are 5V tolerant or not. So, be sure about it before you connect your ESP32 with the RS232<--->TTL Converter Module.

If the IO pins of ESP32 are 5V tolerant, then you can try the connection of Post-18 subject to your satisfaction that your Modem is a RS232 device.