Controller Code that Demonstrates Integer Parsing



  • Hi Everyone,

    I know I was looking at some C++ code on this site awhile back that parsed out the node and payload information from a serial data string. Would someone be so kind as to direct me to a similar example - code that grabs incoming serial data using the semicolon as a parsing guide?

    Thanks for any and all suggestions - Baran


  • Hero Member

    Try sscanf for easy string parsing.



  • I appreciate your suggestion but I know litte to nothing about coding. I was hoping to view a working example of some sort of parsing that might serve as a template for this module.


  • Contest Winner

    @Baran

    can you give an example of the message you are trying to parse.



  • Hi Bulldog,

    I am trying to parse typical node output data. When queried, my nodes send the analog data from each of the eight analog inputs. A typical output might look like:

    1;0;1;0;17;354
    1;0;1;0;17;422
    1;0;1;0;17;423
    1;0;1;0;17;519
    1;0;1;0;17;527
    1;0;1;0;17;439
    1;0;1;0;17;484
    1;0;1;0;17;406

    The first byte is the node ID, and I do not care about the second, third, fourth, or fifth bytes. The sixth byte is the payload or data from an analog input. I would like to, ideally, parse the 1st and 6th bytes from each line and store the data in an array, possibly in flash memory on a Mega board. The problem is, I am so new to this, I am not sure how to begin to even present something to work on. My hope is that someone who recognizes this as being similar to a question from an earlier time can direct me to a sample that might serve to illustrate how this process can be made to happen. In the best of all possible worlds, someone may say "oh yeah, here's how you do it". Hey . . . a newbie can dream.


  • Contest Winner

    @Baran said:

    1;0;1;0;17;354

    something like this:

    char myString[23] = "1;0;1;0;17;354";  // this is a C string, not String class...
    /*or like this:  char* myString = "1;0;1;0;17;354";  for example */
    int myValue[6]; //array to store all 6 values...
    
    
    void setup() 
    {
      Serial.begin(9600);
      char* token = strtok(myString, ";");  //tokenize the string
      for (int i = 0; i < 6; i++)  // works for your 6 values tokenized by ';'
      {
        myValue[i] = atoi(token); // take the tokenized value and store to the array
        token = strtok(NULL, ";");    // move the pointer to the next value
        Serial.println(myValue[i]);  //just print the value stored to the array
      }
    }
    
    void loop() 
    {
      //
    }
    

    I hope that helps...

    PS, since your value in the 6th position was greater than a byte, I used int



  • Greetings,

    Way too cool! I appreciate the illustration. I need to get to sleep but I will play with it tomorrow evening after work. I just wanted to say thank you for giving me a guide.

    Respectfully - Baran


  • Admin

    You can look in the SerialGateway code. It parses the commands.



  • @BulldogLowell said:

    char myString[23] = "1;0;1;0;17;354"; // this is a C string, not String class...
    /or like this: char myString = "1;0;1;0;17;354"; for example */

    Hi Bulldog,

    Could you explain the sttement (above)? I understand everything else about the code you posted except this. I think the "23" means you are allowing 23 spaces for the information but I do not understand the right side of the "=" sign. This is something like what might come through but why am I defining the payload? It could be from 20 to 1020 nd I do not understand the code above. Would you explain what this line is about and how the information in quotes relates to the variables being read in real time?

    Respectfully - Baran


  • Contest Winner

    Just assigning a C string to the char array, of length 23.

    Simulating the message you receive.


Log in to reply
 

Suggested Topics

  • 2
  • 10
  • 75
  • 5
  • 2
  • 17

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts