Serial Monitor Not Printing What it Should - Nano

By the time your code gets to the if statement, there will more than likely not be 16 characters received. So you will never read the GPS.

SIM7000E.print("AT+CGNSPWR=1\r\n");
while(1)
{
  if (SIM7000E.available())
  {
    char c = Serial.read();
    if (c < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(c, HEX);
    Serial.print(" ");
  }
}

You will get an output like

2B 43 47 ...

Those are the HEX representations of the data that you have received. You can look them up on e.g. https://www.asciitable.com/. The last one or two are the important ones for you to continue. They are probably 0A and 0D or a combination of that; 0A is a line feed and 0D is a carriage return.

Once you know that, you can read your SIM data into an array; you need to make sure that your array is big enough to hold the full message plus a terminating '\0'; looking at the output of your opening post, 16 will not be enough !

E.g. to read up to the 0A

char data[128];
SIM7000E.print("AT+CGNSPWR=1\r\n");
SIM7000E.readBytesUntil('\n', data, sizeof(data));

E.g. to read up to the 0D

char data[128];
SIM7000E.print("AT+CGNSPWR=1\r\n");
SIM7000E.readBytesUntil('\r', data, sizeof(data));

Those are not perfect but should get you started. Problem with readBytesUntil is that it's blocking for a short while. If that poses a problem, you can read Serial Input Basics - updated to get ideas for non-blocking receive code and you will need a simple mechanism to only send again once you have received the full message.

1 Like