Reading serial output

hello, noob here,

what is wrong with this program:

void loop() {

 uint8_t x=0,  answer=0;
   char response[100];

if(Serial2.available())  
 {
   
  while(Serial2.available()>0)  

{
response[x] = Serial2.read();
x++;

}
   Serial.print(response);  
 }


}

I am getting output like: e%^** *%^$# %^&

and also, if the serial output is like this:

hahaha: hohoho

how can I get hohoho?

One obvious thing - you are not null terminating the response before printing it.

P.S. Have you set the baud rate and other serial parameters in the sender to be the same as those setup in the Arduino?

"One obvious thing - you are not null terminating the response before printing it." how do I do this?

"P.S. Have you set the baud rate and other serial parameters in the sender to be the same as those setup in the Arduino?"

Yes, it is located in the setup, serial - 9600, serial2 - 9600

I use this in the serial monitor: 9600 and no line ending

if the serial output is several lines like this: name : haha address: usa lapaluza cell: 1234344-776

how can i get tthose values after ":"?

boi_goog: "One obvious thing - you are not null terminating the response before printing it." how do I do this?

Before printing -

response[x] = '\0';

boi_goog: if the serial output is several lines like this: name : haha address: usa lapaluza cell: 1234344-776

how can i get tthose values after ":"?

Read until you get an end of line. Search the line until you find the colon. The 'name' is then extracted from index 0 to the index of the colon. The 'value' is extracted from the index of the colon + 1 to the end of the line.

does this means that each Serial.read() is executed, the serial buffer is diminished by 1? all i have to do is keep reading the next byte?

boi_goog: does this means that each Serial.read() is executed, the serial buffer is diminished by 1? all i have to do is keep reading the next byte?

Nearly. Serial.read() removes one byte from the input buffer but 2 or more may have arrived by the time you read another byte.

You are right about reading the next byte until you have received the expected number of bytes and/or the expected terminating byte. Don't forget that the Arduino can read from the buffer much faster that the Serial interface can receive bytes and put them in the buffer so just because at least 1 byte is available it does not meant that several are available.

Take a look At these post's. http://hacking.majenko.co.uk/reading-serial-on-the-arduino

http://jhaskellsblog.blogspot.in/2011/05/serial-comm-fundamentals-on-arduino.html

I also try to write the same thing (more like combination of these two).. But I don't think that goes well anyway.. Take a look If you don't understand these .. https://anilarduino.wordpress.com/2014/11/17/serial-communication-on-arduino/

This data input demo might give you some ideas.

...R

stowite:
One obvious thing - you are not null terminating the response before printing it.

P.S. Have you set the baud rate and other serial parameters in the sender to be the same as those setup in the Arduino?

Do we have to End the char Array with Null terminator(making it String…right?) to print that array to Serial Monitor?

boi_goog: if the serial output is several lines like this: name : haha address: usa lapaluza cell: 1234344-776

how can i get tthose values after ":"?

You can probably capture the data on the fly by reading the characters as they arrive. when the : is encountered a capture sub loop is entered and the subsequent characters are captured until the cr/lf is encountered, then the main loop is reentered. I'd think the easiest thing to do would be to capture all the lines and parse for each part. Below is some serial capture/parse code, but it assumes there is some unique end of data packet delimiter to tell when all the data has been captured. Is there an end of transmission marker with the data being sent in your setup?

//zoomkat 11-12-13 String capture and parsing  
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data 

String readString; //main captured String 
String angle; //data String
String fuel;
String speed1;
String altidude;

int ind1; // , locations
int ind2;
int ind3;
int ind4;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 90,low,15.6,125*
  //or 130,hi,7.2,389*

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '*') {
      //do stuff
      
      Serial.println();
      Serial.print("captured String is : "); 
      Serial.println(readString); //prints string to serial port out
      
      ind1 = readString.indexOf(',');  //finds location of first ,
      angle = readString.substring(0, ind1);   //captures first data String
      ind2 = readString.indexOf(',', ind1+1 );   //finds location of second ,
      fuel = readString.substring(ind1+1, ind2+1);   //captures second data String
      ind3 = readString.indexOf(',', ind2+1 );
      speed1 = readString.substring(ind2+1, ind3+1);
      ind4 = readString.indexOf(',', ind3+1 );
      altidude = readString.substring(ind3+1); //captures remain part of data after last ,

      Serial.print("angle = ");
      Serial.println(angle); 
      Serial.print("fuel = ");
      Serial.println(fuel);
      Serial.print("speed = ");
      Serial.println(speed1);
      Serial.print("altidude = ");
      Serial.println(altidude);
      Serial.println();
      Serial.println();
      
      readString=""; //clears variable for new input
      angle="";
      fuel="";
      speed1="";
      altidude="";
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

hello again, i am using adafruit gps shield with cc3000, I need to send the gps loction reading over telnet and here is my code:

 char c = GPS.read();
 GPS.parse(GPS.lastNMEA());
       char latis = (GPS.latitude, 4);
      client.fastrprint("Location: ");
      client.write(latis); client.write(GPS.lat);
      client.fastrprint(", "); 
      char longi = (GPS.longitude, 4);
      client.write(longi); client.write(GPS.lon);

but I am only getting the N and E part, ho can I capture the actual value?

char c = GPS.read();

Read ONE character from the GPS.

 GPS.parse(GPS.lastNMEA());

Toss that character in the bit bucket, and parse the last sentence, whether there is one, or not.

       char latis = (GPS.latitude, 4);

Regardless of whether parsing was successful, or not, do something completely stupid with the comma operator, and store the result as a single character.

Then, wonder why things don't work. Well, I know.

so how can i get the nemeric part? can suggest sample codes?

can suggest sample codes?

The TinyGPS++ library handles parsing GPS data AND comes with examples that, strangely enough, actually work.

What are you trying to do, here:

char latis = (GPS.latitude, 4);

I can't even begin to guess.

I am trying to convert it to character, this is my newes code:

       Serial.println("Divice Location Requested");
 double z;
char c = GPS.read();
 GPS.parse(GPS.lastNMEA());
 z=GPS.latitude;
       uint8_t latis = int(z);   this is new
      client.fastrprint("Location: ");
      client.print(latis); 
      Serial.println(latis); //I am able see 157
      Serial.println(z);   //actual value is 1436.0909
      client.write((GPS.latitude - int(GPS.latitude)) * 10000);
      client.write(GPS.lat);
      client.fastrprint(", "); 
      char longi = GPS.longitude;
      client.write(longi); client.write(GPS.lon);
       Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);

why am I getting latis = 157 when the valuue of z = 1436.0909? how can I get to latis = 1436.0909?

Its working already! I juzt changed the uint8_t to uint32_t

Converting a double to an int, and then storing the int in a byte does not make sense. Nor does reading the GPS data and throwing it away. Nor does assuming that parsing returned good data.

Why are you stubbornly persisting in not using the TinyGPS++ library? What library are you using? Why have you butchered the example code so badly?