Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Controllers
  3. Controller Code that Demonstrates Integer Parsing

Controller Code that Demonstrates Integer Parsing

Scheduled Pinned Locked Moved Controllers
10 Posts 4 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Baran
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • AWIA Offline
      AWIA Offline
      AWI
      Hero Member
      wrote on last edited by
      #2

      Try sscanf for easy string parsing.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Baran
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • BulldogLowellB Offline
          BulldogLowellB Offline
          BulldogLowell
          Contest Winner
          wrote on last edited by BulldogLowell
          #4

          @Baran

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Baran
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • BulldogLowellB Offline
              BulldogLowellB Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by BulldogLowell
              #6

              @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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Baran
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                1
                • hekH Offline
                  hekH Offline
                  hek
                  Admin
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Baran
                    wrote on last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #10

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

                      Simulating the message you receive.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      14

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular