Decoding serial data

Hi,

I`m trying to send commands in the following format Axxxx where A is my command letter, eg A, B, C, D.... and xxxx is a float data value, e.g 1.02, 10.05, 12.6.

I can read the serial using the following;

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

and i can get the letter but i`m struggling to also return the float data value. Can someone please give me some pointers.

Many thanks

James

Can someone please give me some pointers.

Sure thing.
Don't use Strings.
Post ALL of your code.

char *pThisIsAPointer;

If you insist on pissing away resources using Strings, when you have the last character in the packet (How will you know that?), extract the String contents as a string (NULL terminated array of chars, which is what you SHOULD be using), determine the letter that starts the string (the command), then replace the first letter with a space, and call atof() with the string as input.

Could you use ParseFloat?

So this was the code i was trying to get working, not brilliant but i`m just learning at the moment.

If i get a command R then i need to read some analogue channels. If i get a V value then i need to figure out the data value and then send this value to a digital Pot. There will be more commands based on other letters that do other things.

void loop() {
    if (stringComplete) {
      
      int val = inputString.charAt(0);
      int len = inputString.length();
      switch (val){
        case 82: //R
        {
          Read(); //Reads some analogue channels
          break;
        }
        case 86: //V
        {
         float voltageVal =(inputString.substring(1,len-1)).parseFloat;
          digitalPotWrite(voltageVal); //write a voltage to a digital POT.
          break;
        }
        default:{}
      } 
      inputString = "";
      stringComplete=false;
      }
 }

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Hint: save yourself a comment

switch (val){
        case 'R':

Thanks for the tip. With the tip and a little bit of web surfing i managed to figure it out. Probably not the most elegant way of doing it but it at least works.

void loop() {
    if (stringComplete) {
      
      int val = inputString.charAt(0);
      int len = inputString.length();
      switch (val){
        case 'R':
        {
         //Serial.println(inputString.substring(1,len-1));
          Read();
          break;
        }
        case 'V':
        {
          String DataVal = inputString.substring(1,len-1); 
          char carray[DataVal.length() + 1]; //Array size
          DataVal.toCharArray(carray, sizeof(carray)); //put DataVal into an array
          float VoltageVal = atof(carray) ;   //Change to float value
          digitalPotWrite(VoltageVal);
          break;
        }
        default:{}
      } 
      inputString = "";
      stringComplete=false;
      }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}