Char[] to Int problem

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:

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

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

SurferTim:
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?*_

Post your entire sketch.

  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)

  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.

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.

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

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.

Did you read the link I posted earlier?

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

...
...
    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 '!'");
    }