Go Down

Topic: Char[] to Int problem (Read 125 times) previous topic - next topic

rfsingh81

Apr 10, 2016, 03:22 pm Last Edit: Apr 10, 2016, 03:28 pm by rfsingh81
Can someone please help me solve this problem. I am receiving data as characters and want to convert them to an integer (part of it). My code is as follows:

Code: [Select]

bool halt=false;
while (Serial2.available()>0){
                      data1[counter] = Serial2.read(); //data1[] is char array
                      ++counter;
                      }
                  char buff[counter-1];
                  Serial.print("buff[i]=");
                  for (byte i=0;i<counter;i++){
                      buff[i]='\0';
                      if ((data1[i]!='!') and (!halt)){
                          buff[i]=data1[i];
                          } else{
                              halt=true;
                              }
                       Serial.print(buff[i]);
                      }
                  rxStatus3=atoi(buff);
                  Serial.println("");
                  Serial.print("rxStatus3=");
                  Serial.println(rxStatus3);

I get on my terminal window something like below:
buff=1034
rxStatus3=0

I am sending from the other end something like this:

1034!1112@

I only want to convert to int before '!' currently. Thanks

SurferTim

Looks like you already are. rxStatus3 holds the converted int. Serial.println(rxStatus3) converts the int to a character string again.

rfsingh81

Looks like you already are. rxStatus3 holds the converted int. Serial.println(rxStatus3) converts the int to a character string again.
So, yes  you are right! careful observation revealed that it does get printed once. but why ONCE?

This code is visited every 100mS so I am expecting a result every 100mS. I changed the code to the following:

........
Serial.print("buff=");
                  for (byte i=0;i<counter;i++){
                      buff='\0';
                      if ((data1!='!') and (!halt)){
                          buff=data1;
                          } else{
                              halt=true;
                              }
                       Serial.print(buff);
                      }
                  int rxStatus3=atoi(buff);
                  Serial.println("");
                  if (rxStatus3==1034) Serial.println("rxStatus3=1034"); // ONLY GETS PRINTED ONCE! WHY?



SurferTim


sterretje

#4
Apr 10, 2016, 05:35 pm Last Edit: Apr 10, 2016, 06:02 pm by sterretje
Code: [Select]

  bool halt = false;
  while (Serial2.available() > 0) {
    data1[counter] = Serial2.read(); //data1[] is char array
    ++counter;
  }

  char buff[counter - 1];

I wonder if the last line above is actually valid C. As far as I know, the compiler likes to know the size of an array at compile time.

A different approach can be the below (without using an additional buffer)

Code: [Select]

  bool halt = false;
  int counter = 0;
  char data1[20];

  while (Serial.available() > 0 && counter != 10) {
    data1[counter] = Serial.read(); //data1[] is char array
    ++counter;
  }
  data1[counter] = '\0';

  // find exclamation mark
  char *ptr = strchr(data1, '!');
  // if found, replace by string terminator
  if(ptr!=NULL)
  {
    *ptr = '\0';
  }

  // convert
  int rxStatus3 = atoi(data1);
  // restore to original string
  if(ptr!=NULL)
  {
    *ptr = '!';
  }

  Serial.println("");
  Serial.print("rxStatus3=");
  Serial.println(rxStatus3);



I further suggest that you read Serial Input Basics

Without seeing your complete sketch (or at least the function that contains the code that you posted)
You assume that you receive all your bytes in one go; that might not be the case (see above link to solve that). Your code (as it stands now), might be executed in an endless loop; so even if there is no data, it will do a conversion.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

rfsingh81

#5
Apr 10, 2016, 10:18 pm Last Edit: Apr 11, 2016, 12:42 am by rfsingh81
Here is the complete function (amended with fresh approach!). I don't have anything out of this function which could influence the behaviour here. This has all the relevant variables contained in itself but the result is the same.

Code: [Select]

void recvData(){
          txTimes++;
          int counter=0;
          int rxState3=0;
          char data1[256];
          bool halt=false;
          if (txTimes>20) txTimes=10;
          if (Serial2.available()>0){
                rxTimes=txTimes;
                while (Serial2.available()>0){
                    data1[counter] = Serial2.read();
                    ++counter;
                    }
                data1[counter]='\0';
                char *ptr = strchr(data1, '!'); // find exclamation mark
                if(ptr!=NULL){  // if found, replace by string terminator
                    *ptr = '\0';
                    rxState3 = atoi(data1);
                    *ptr = '!';
                    }
                if (rxState3==1034) Serial.println("Valid");
                rxState3=0;
                control=true;
                warning=0;
                }
}

aarg

See the funny italics? Please edit your post and include the code inside code tags. Get in the habit of using them to post code.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

sterretje

Did you read the link I posted earlier?

Further add some debug statements using serial.print/ln so you can see what you received.

Code: [Select]

...
...
    data1[counter] = '\0';
    Serial.print("data1: "); Serial.println(data1);

    char *ptr = strchr(data1, '!'); // find exclamation mark
    if (ptr != NULL) { // if found, replace by string terminator
      *ptr = '\0';
      Serial.print("data1 truncated: "); Serial.println(data1);
      rxState3 = atoi(data1);
      Serial.print("rxState3: "); Serial.println(rxState3);
      *ptr = '!';
    }
    else
    {
      Serial.println("no '!'");
    }


If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy