Arduino command parsing












0














I am writing an ESP32 project which receives an UDP packet and based on it some action should be carried out. There are those commands:



FON
FOFF
MSC <INT> <INT>
TC <INT>


I receive the message like this:



void receiveUdpMessages(){
int udpMsgLength = Udp.parsePacket();
if(udpMsgLength != 0){
byte udpPacket[udpMsgLength+1];
IPAddress senderIp = Udp.remoteIP();
Udp.read(udpPacket, udpMsgLength);
udpPacket[udpMsgLength] = 0;
Udp.flush();

Serial.println("Received UDP Message from : " + String(senderIp[0]) + "." + String(senderIp[1]) + "." + String(senderIp[2])+ "."+ String(senderIp[3]));

processReceivedMessage((char *)udpPacket);
}
}


and this is the processReceivedMessage method:



void processReceivedMessage(char *message){
if(strncmp("FON",message,3)==0){
setParameters(ct, 100);
}else if(strncmp("FOFF",message,4)==0){
setParameters(ct, 0);
}else if(strncmp("MSC",message,3)==0){

}else if(strncmp("TC",message,2)==0){

}
}


My question is what is the best way to split both of the ints for the parameterized commands? Also if you notice any sort of issue with above code please tell me I did not have an opportunity to test it yet.










share|improve this question



























    0














    I am writing an ESP32 project which receives an UDP packet and based on it some action should be carried out. There are those commands:



    FON
    FOFF
    MSC <INT> <INT>
    TC <INT>


    I receive the message like this:



    void receiveUdpMessages(){
    int udpMsgLength = Udp.parsePacket();
    if(udpMsgLength != 0){
    byte udpPacket[udpMsgLength+1];
    IPAddress senderIp = Udp.remoteIP();
    Udp.read(udpPacket, udpMsgLength);
    udpPacket[udpMsgLength] = 0;
    Udp.flush();

    Serial.println("Received UDP Message from : " + String(senderIp[0]) + "." + String(senderIp[1]) + "." + String(senderIp[2])+ "."+ String(senderIp[3]));

    processReceivedMessage((char *)udpPacket);
    }
    }


    and this is the processReceivedMessage method:



    void processReceivedMessage(char *message){
    if(strncmp("FON",message,3)==0){
    setParameters(ct, 100);
    }else if(strncmp("FOFF",message,4)==0){
    setParameters(ct, 0);
    }else if(strncmp("MSC",message,3)==0){

    }else if(strncmp("TC",message,2)==0){

    }
    }


    My question is what is the best way to split both of the ints for the parameterized commands? Also if you notice any sort of issue with above code please tell me I did not have an opportunity to test it yet.










    share|improve this question

























      0












      0








      0







      I am writing an ESP32 project which receives an UDP packet and based on it some action should be carried out. There are those commands:



      FON
      FOFF
      MSC <INT> <INT>
      TC <INT>


      I receive the message like this:



      void receiveUdpMessages(){
      int udpMsgLength = Udp.parsePacket();
      if(udpMsgLength != 0){
      byte udpPacket[udpMsgLength+1];
      IPAddress senderIp = Udp.remoteIP();
      Udp.read(udpPacket, udpMsgLength);
      udpPacket[udpMsgLength] = 0;
      Udp.flush();

      Serial.println("Received UDP Message from : " + String(senderIp[0]) + "." + String(senderIp[1]) + "." + String(senderIp[2])+ "."+ String(senderIp[3]));

      processReceivedMessage((char *)udpPacket);
      }
      }


      and this is the processReceivedMessage method:



      void processReceivedMessage(char *message){
      if(strncmp("FON",message,3)==0){
      setParameters(ct, 100);
      }else if(strncmp("FOFF",message,4)==0){
      setParameters(ct, 0);
      }else if(strncmp("MSC",message,3)==0){

      }else if(strncmp("TC",message,2)==0){

      }
      }


      My question is what is the best way to split both of the ints for the parameterized commands? Also if you notice any sort of issue with above code please tell me I did not have an opportunity to test it yet.










      share|improve this question













      I am writing an ESP32 project which receives an UDP packet and based on it some action should be carried out. There are those commands:



      FON
      FOFF
      MSC <INT> <INT>
      TC <INT>


      I receive the message like this:



      void receiveUdpMessages(){
      int udpMsgLength = Udp.parsePacket();
      if(udpMsgLength != 0){
      byte udpPacket[udpMsgLength+1];
      IPAddress senderIp = Udp.remoteIP();
      Udp.read(udpPacket, udpMsgLength);
      udpPacket[udpMsgLength] = 0;
      Udp.flush();

      Serial.println("Received UDP Message from : " + String(senderIp[0]) + "." + String(senderIp[1]) + "." + String(senderIp[2])+ "."+ String(senderIp[3]));

      processReceivedMessage((char *)udpPacket);
      }
      }


      and this is the processReceivedMessage method:



      void processReceivedMessage(char *message){
      if(strncmp("FON",message,3)==0){
      setParameters(ct, 100);
      }else if(strncmp("FOFF",message,4)==0){
      setParameters(ct, 0);
      }else if(strncmp("MSC",message,3)==0){

      }else if(strncmp("TC",message,2)==0){

      }
      }


      My question is what is the best way to split both of the ints for the parameterized commands? Also if you notice any sort of issue with above code please tell me I did not have an opportunity to test it yet.







      arduino int udp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 7:52









      jonjon1jonjon1

      237




      237
























          1 Answer
          1






          active

          oldest

          votes


















          1














          So there's actually two seperate steps here:




          1. Find where the integers substrings start in your message

          2. Convert those substrings to actual ints


          For step 1, there are many ways to do this, but using strchr is probably the easiest for your purposes.



          For step 2, either use atoi or the safer-but-harder-to-use strtol



          Here's an example for the MSC message, the one for processing the TC message will be very similar.



          I've kept it as one function for clarity, but there's scope for refactoring it.



          void process_msc_message(char * message)
          {
          int integers[2];
          // strchr returns a pointer to the space character
          char* substring = strchr(message, ' ');
          if (substring)
          {
          // atoi will convert the first number it finds in the given string
          integers[0] = atoi(substring);
          }

          // Jump forward to the next number
          substring = strchr(substring+1, ' ');
          if (substring)
          {
          integers[1] = atoi(substring);
          }

          // Do something with the integers...

          }





          share|improve this answer





















          • Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
            – jonjon1
            Nov 23 '18 at 12:16












          • cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
            – jfowkes
            Nov 23 '18 at 12:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442612%2farduino-command-parsing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          So there's actually two seperate steps here:




          1. Find where the integers substrings start in your message

          2. Convert those substrings to actual ints


          For step 1, there are many ways to do this, but using strchr is probably the easiest for your purposes.



          For step 2, either use atoi or the safer-but-harder-to-use strtol



          Here's an example for the MSC message, the one for processing the TC message will be very similar.



          I've kept it as one function for clarity, but there's scope for refactoring it.



          void process_msc_message(char * message)
          {
          int integers[2];
          // strchr returns a pointer to the space character
          char* substring = strchr(message, ' ');
          if (substring)
          {
          // atoi will convert the first number it finds in the given string
          integers[0] = atoi(substring);
          }

          // Jump forward to the next number
          substring = strchr(substring+1, ' ');
          if (substring)
          {
          integers[1] = atoi(substring);
          }

          // Do something with the integers...

          }





          share|improve this answer





















          • Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
            – jonjon1
            Nov 23 '18 at 12:16












          • cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
            – jfowkes
            Nov 23 '18 at 12:17
















          1














          So there's actually two seperate steps here:




          1. Find where the integers substrings start in your message

          2. Convert those substrings to actual ints


          For step 1, there are many ways to do this, but using strchr is probably the easiest for your purposes.



          For step 2, either use atoi or the safer-but-harder-to-use strtol



          Here's an example for the MSC message, the one for processing the TC message will be very similar.



          I've kept it as one function for clarity, but there's scope for refactoring it.



          void process_msc_message(char * message)
          {
          int integers[2];
          // strchr returns a pointer to the space character
          char* substring = strchr(message, ' ');
          if (substring)
          {
          // atoi will convert the first number it finds in the given string
          integers[0] = atoi(substring);
          }

          // Jump forward to the next number
          substring = strchr(substring+1, ' ');
          if (substring)
          {
          integers[1] = atoi(substring);
          }

          // Do something with the integers...

          }





          share|improve this answer





















          • Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
            – jonjon1
            Nov 23 '18 at 12:16












          • cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
            – jfowkes
            Nov 23 '18 at 12:17














          1












          1








          1






          So there's actually two seperate steps here:




          1. Find where the integers substrings start in your message

          2. Convert those substrings to actual ints


          For step 1, there are many ways to do this, but using strchr is probably the easiest for your purposes.



          For step 2, either use atoi or the safer-but-harder-to-use strtol



          Here's an example for the MSC message, the one for processing the TC message will be very similar.



          I've kept it as one function for clarity, but there's scope for refactoring it.



          void process_msc_message(char * message)
          {
          int integers[2];
          // strchr returns a pointer to the space character
          char* substring = strchr(message, ' ');
          if (substring)
          {
          // atoi will convert the first number it finds in the given string
          integers[0] = atoi(substring);
          }

          // Jump forward to the next number
          substring = strchr(substring+1, ' ');
          if (substring)
          {
          integers[1] = atoi(substring);
          }

          // Do something with the integers...

          }





          share|improve this answer












          So there's actually two seperate steps here:




          1. Find where the integers substrings start in your message

          2. Convert those substrings to actual ints


          For step 1, there are many ways to do this, but using strchr is probably the easiest for your purposes.



          For step 2, either use atoi or the safer-but-harder-to-use strtol



          Here's an example for the MSC message, the one for processing the TC message will be very similar.



          I've kept it as one function for clarity, but there's scope for refactoring it.



          void process_msc_message(char * message)
          {
          int integers[2];
          // strchr returns a pointer to the space character
          char* substring = strchr(message, ' ');
          if (substring)
          {
          // atoi will convert the first number it finds in the given string
          integers[0] = atoi(substring);
          }

          // Jump forward to the next number
          substring = strchr(substring+1, ' ');
          if (substring)
          {
          integers[1] = atoi(substring);
          }

          // Do something with the integers...

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 11:51









          jfowkesjfowkes

          764312




          764312












          • Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
            – jonjon1
            Nov 23 '18 at 12:16












          • cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
            – jfowkes
            Nov 23 '18 at 12:17


















          • Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
            – jonjon1
            Nov 23 '18 at 12:16












          • cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
            – jfowkes
            Nov 23 '18 at 12:17
















          Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
          – jonjon1
          Nov 23 '18 at 12:16






          Wow I did not know that there was function returning pointer to next space. That will come in handy for sure. Thanks a lot for your help!
          – jonjon1
          Nov 23 '18 at 12:16














          cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
          – jfowkes
          Nov 23 '18 at 12:17




          cplusplus.com/reference/cstring/strchr You can give it any character to search for. It will returns NULL if that character isn't in the string.
          – jfowkes
          Nov 23 '18 at 12:17


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442612%2farduino-command-parsing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...