Can not send string via Serial monitor

Hey there, noob here
I am trying to send a string " $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75"
via serial monitor in code for further execution of code. But when enter the string and click on send button it gives only '$'.....need help
follwing is part of my code

void setup(){
  Serial.begin(9600);
}                                                                         

void loop(){

 if(Serial.available()>0){
 
   
  char message=(char)Serial.read(); //"$GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 "; 
  // NM.toCharArray(message,i);
   Serial.println(message);

That code won't even compile. Try again.

appreciated for your concern, as i mention above this is part of my code
following is my full code

void setup(){
  Serial.begin(9600);
}                                                                         

void loop(){

 if(Serial.available()>0){
 
   
  char message=(char)Serial.read();//"$GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 "; 
 
   Serial.println(message);
  char*next;// pointer to substring (token)
  float value;
  next=strtok(message,",");//splitting string by "," and get 1st substring token
  Serial.println(next);// print $GPGSV

  Serial.print("NO.of Satellites: ");// NO. of satellites for full data
  (next=strtok(NULL,","));// print next string  and remove previous string
  
  Serial.println(next);

  Serial.print("Sentence No: ");
  (next=strtok(NULL,","));
 
  Serial.println(next);

   Serial.print("satellites in View: ");
  (next=strtok(NULL,","));
  //value=atof(next); - cut this to get exact value from sentence 
  Serial.println(next);// get exact "08" of sentence


  
  Serial.print(" satellite PRN number: ");
  (next=strtok(NULL,","));
  //value=atof(next);
  Serial.println(next );

  
  Serial.print("Elevation: ");// print elevation in degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  
   Serial.print("Azimuth: ");// print Azimuth in Degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  Serial.print("SNR: ");// print SNR higher is better
  (next=strtok(NULL,","));
  Serial.println(next);

   


    Serial.print(" satellite PRN number: ");
  (next=strtok(NULL,","));
  //value=atof(next);
  Serial.println(next );

  
  Serial.print("Elevation: ");// print elevation in degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  
   Serial.print("Azimuth: ");// print Azimuth in Degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  Serial.print("SNR: ");// print SNR higher is better
  (next=strtok(NULL,","));
  Serial.println(next); 


  Serial.print(" satellite PRN number: ");
  (next=strtok(NULL,","));
  //value=atof(next);
  Serial.println(next );

  
  Serial.print("Elevation: ");// print elevation in degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  
   Serial.print("Azimuth: ");// print Azimuth in Degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  Serial.print("SNR: ");// print SNR higher is better
  (next=strtok(NULL,","));
  Serial.println(next);  

Serial.print(" satellite PRN number: ");
  (next=strtok(NULL,","));
  //value=atof(next);
  Serial.println(next );

  
  Serial.print("Elevation: ");// print elevation in degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  
   Serial.print("Azimuth: ");// print Azimuth in Degree
  (next=strtok(NULL,","));
  Serial.print(next);
  Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
  Serial.println(" ");

  Serial.print("SNR: ");// print SNR higher is better
  (next=strtok(NULL,"*"));
  Serial.println(next);  

  Serial.print("check sum : ");//print check sum data
  (next=strtok(NULL,"*"));
  Serial.print("*");
  Serial.println(next);  
}


}

Serial.read() only gets one character. You have to keep calling it over and over and adding the characters to your string one at a time. They don’t all come in at once and they don’t all read with just one call to read().

The serial input basics tutorial may be helpful. In the IDE set line endings to New Line. Then you could use the receive with start and end markers example with start marker of $ and the end marker of '/n'. The $ will not show in the receivedCharacters, though.

Delta_G:
Serial.read() only gets one character. You have to keep calling it over and over and adding the characters to your string one at a time. They don’t all come in at once and they don’t all read with just one call to read().

@OP
I am converting the above text code into executable code for you; hope, you will understand why you are not seeing your all characters (that are coming from the InputBox of Serial Monitor) back in the OutputBox of the Serial Monitor.

void loop()
{
   byte n = Serial.available(); //to check that a charterer has come from Serial Monitor
   if(n !=0)
   {
      char x = Serial.read();     //a character has come; read it and save in variable x
      Serial.print(x);                //send the received character back at the OutputBox of Serial Monitor
   }
}

If you want to insist that your style of coding should work, then you have to execute he following codes (Line ending tab of your Serial Monitor must have the option Newline):

char myMessage[100] = "";
void loop()
{
   byte n = Serial.available(); //to check that a charterer has come from Serial Monitor
   if(n !=0)
   {
      Serial.readBytesUntl('\n', myMessage, 100);
      Serial.print(myMessage);   //send all received characters back at the OutputBox of Serial Monitor
   }
}

@GolamMostafa thank you for your consern i had tried the code you mention in newline (line ending tab) … still it’s only displaying one character

void loop(){

 if(Serial.available()>0){
 // int i;
   //String NM= Serial.readString();
{
   byte n = Serial.available(); //to check that a charterer has come from Serial Monitor
   if(n !=0)
   {
      char message = Serial.read();     //a character has come; read it and save in variable x
      Serial.print(message);      //send the received character back at the OutputBox of Serial 
   }
}
[code/]

i had tried the code you mention in newline (line ending tab) .... still it's only displaying one character

Please post the code you used?

Is your monitor set to terminate the line with a new line character?

Below is the mention code

char message;
void setup() {
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {
    // int i;
    //String NM= Serial.readString();
    {
      byte n = Serial.available(); //to check that a charterer has come from Serial Monitor
      if (n != 0)
      {
        char message = Serial.read();     //a character has come; read it and save in variable x
        Serial.print(message);                //send the received character back at the OutputBox of Serial Monitor
      }
    }

    //char message=(char)Serial.read();//" $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 ";
    // NM.toCharArray(message,i);

    char*next;// pointer to substring (token)
    float value;
    next = strtok(message, ","); //splitting string by "," and get 1st substring token
    Serial.println(next);// print $GPGSV

    Serial.print("NO.of Satellites: ");// NO. of satellites for full data
    (next = strtok(NULL, ",")); // print next string  and remove previous string
    //value=atoi(next);
    Serial.println(next);

    Serial.print("Sentence No: ");
    (next = strtok(NULL, ","));
    // value=atof(next);
    Serial.println(next);

    Serial.print("satellites in View: ");
    (next = strtok(NULL, ","));
    //value=atof(next); - cut this to get exact value from sentence
    Serial.println(next);// get exact "08" of sentence



    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, ","));
    Serial.println(next);

    //  add some logics here to reduc code


    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, ","));
    Serial.println(next);


    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, ","));
    Serial.println(next);

    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, "*"));
    Serial.println(next);

    Serial.print("check sum : ");//print check sum data
    (next = strtok(NULL, "*"));
    Serial.print("*");
    Serial.println(next);
  }
  //else{
  //  Serial.end();
  //}
}

next = strtok(message, ",");strtok works on strings, not single characters.

Does that even compile?

You did not follow the readBytesUntil() method. You need to declare a character array to hold the received message. Serial.readBytesUntil() returns the number of characters read into the buffer. It's best to null terminate what you receive without relying on the large array with zeros at the end.

////$GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 
char message[100];//declare an array to hold the complete message
void setup() {
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {
    // int i;
    //String NM= Serial.readString();
    {
      byte n = Serial.available(); //to check that a charterer has come from Serial Monitor
      if (n != 0)
      {
        // char message = Serial.read();     //a character has come; read it and save in variable x
        byte numBytesReceived = Serial.readBytesUntil('\n', message, 100);
        message[numBytesReceived] = '\0'; //null terminate message
        Serial.print(message);                //send the received character back at the OutputBox of Serial Monitor
      }
    }

    //char message=(char)Serial.read();//" $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 ";
    // NM.toCharArray(message,i);

    char*next;// pointer to substring (token)
    float value;
    next = strtok(message, ","); //splitting string by "," and get 1st substring token
    Serial.println(next);// print $GPGSV

    Serial.print("NO.of Satellites: ");// NO. of satellites for full data
    (next = strtok(NULL, ",")); // print next string  and remove previous string
    //value=atoi(next);
    Serial.println(next);

    Serial.print("Sentence No: ");
    (next = strtok(NULL, ","));
    // value=atof(next);
    Serial.println(next);

    Serial.print("satellites in View: ");
    (next = strtok(NULL, ","));
    //value=atof(next); - cut this to get exact value from sentence
    Serial.println(next);// get exact "08" of sentence



    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, ","));
    Serial.println(next);

    //  add some logics here to reduc code


    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, ","));
    Serial.println(next);


    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, ","));
    Serial.println(next);

    Serial.print(" satellite PRN number: ");
    (next = strtok(NULL, ","));
    //value=atof(next);
    Serial.println(next );


    Serial.print("Elevation: ");// print elevation in degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");


    Serial.print("Azimuth: ");// print Azimuth in Degree
    (next = strtok(NULL, ","));
    Serial.print(next);
    Serial.print("°");// ASCII VALUE FOR °= (ALT +428)
    Serial.println(" ");

    Serial.print("SNR: ");// print SNR higher is better
    (next = strtok(NULL, "*"));
    Serial.println(next);

    Serial.print("check sum : ");//print check sum data
    (next = strtok(NULL, "*"));
    Serial.print("*");
    Serial.println(next);
  }
  //else{
  //  Serial.end();
  //}
}

Monitor shows

11:14:34.183 -> $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 $GPGSV
11:14:34.250 -> NO.of Satellites: 2
11:14:34.284 -> Sentence No: 1
11:14:34.317 -> satellites in View: 08
11:14:34.317 ->  satellite PRN number: 01
11:14:34.350 -> Elevation: 40° 
11:14:34.385 -> Azimuth: 083° 
11:14:34.385 -> SNR: 46
11:14:34.385 ->  satellite PRN number: 02
11:14:34.418 -> Elevation: 17° 
11:14:34.452 -> Azimuth: 308° 
11:14:34.452 -> SNR: 41
11:14:34.487 ->  satellite PRN number: 12
11:14:34.487 -> Elevation: 07° 
11:14:34.521 -> Azimuth: 344° 
11:14:34.554 -> SNR: 39
11:14:34.554 ->  satellite PRN number: 14
11:14:34.587 -> Elevation: 22° 
11:14:34.587 -> Azimuth: 228° 
11:14:34.621 -> SNR: 45
11:14:34.621 -> check sum : *75

cattledog:

 byte numBytesReceived = Serial.readBytesUntil('\n', message, 100);

message[numBytesReceived] = '\0'; //null terminate message

This is a very good way of null terminating a string; but, I forget it very often.

9 posts were split to a new topic: Bluetooth + Serial Monitor = Trouble?

A post was split to a new topic: I cant send/receive any thing i type any thing to the serial moniter

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.