read data from arduino and react when data is larger than a zero












0














in c#
i'm trying to read data from arduino so when the data is not "0" i will get a reaction.



The signal from arduino is ok and displays on the label but i cannot use the "<" condition.



        private void ArduinoTMR_Tick(object sender, EventArgs e)
{
string ard1 = Arduino.ReadLine();

label2.Text = ard1;
if (ard1 > "0")
{
label2.BackColor = Color.Red;
}
else
label2.BackColor = Color.Blue;

}









share|improve this question
























  • And what is your question?
    – svtag
    Nov 21 '18 at 11:31






  • 1




    why do you even think that you can compare a string with the > operator?? you can't. You would first have to convert it to int
    – sLw
    Nov 21 '18 at 11:39


















0














in c#
i'm trying to read data from arduino so when the data is not "0" i will get a reaction.



The signal from arduino is ok and displays on the label but i cannot use the "<" condition.



        private void ArduinoTMR_Tick(object sender, EventArgs e)
{
string ard1 = Arduino.ReadLine();

label2.Text = ard1;
if (ard1 > "0")
{
label2.BackColor = Color.Red;
}
else
label2.BackColor = Color.Blue;

}









share|improve this question
























  • And what is your question?
    – svtag
    Nov 21 '18 at 11:31






  • 1




    why do you even think that you can compare a string with the > operator?? you can't. You would first have to convert it to int
    – sLw
    Nov 21 '18 at 11:39
















0












0








0







in c#
i'm trying to read data from arduino so when the data is not "0" i will get a reaction.



The signal from arduino is ok and displays on the label but i cannot use the "<" condition.



        private void ArduinoTMR_Tick(object sender, EventArgs e)
{
string ard1 = Arduino.ReadLine();

label2.Text = ard1;
if (ard1 > "0")
{
label2.BackColor = Color.Red;
}
else
label2.BackColor = Color.Blue;

}









share|improve this question















in c#
i'm trying to read data from arduino so when the data is not "0" i will get a reaction.



The signal from arduino is ok and displays on the label but i cannot use the "<" condition.



        private void ArduinoTMR_Tick(object sender, EventArgs e)
{
string ard1 = Arduino.ReadLine();

label2.Text = ard1;
if (ard1 > "0")
{
label2.BackColor = Color.Red;
}
else
label2.BackColor = Color.Blue;

}






c# serial-port






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:27









gre_gor

4,06592631




4,06592631










asked Nov 21 '18 at 11:17









Jozee Media

185




185












  • And what is your question?
    – svtag
    Nov 21 '18 at 11:31






  • 1




    why do you even think that you can compare a string with the > operator?? you can't. You would first have to convert it to int
    – sLw
    Nov 21 '18 at 11:39




















  • And what is your question?
    – svtag
    Nov 21 '18 at 11:31






  • 1




    why do you even think that you can compare a string with the > operator?? you can't. You would first have to convert it to int
    – sLw
    Nov 21 '18 at 11:39


















And what is your question?
– svtag
Nov 21 '18 at 11:31




And what is your question?
– svtag
Nov 21 '18 at 11:31




1




1




why do you even think that you can compare a string with the > operator?? you can't. You would first have to convert it to int
– sLw
Nov 21 '18 at 11:39






why do you even think that you can compare a string with the > operator?? you can't. You would first have to convert it to int
– sLw
Nov 21 '18 at 11:39














2 Answers
2






active

oldest

votes


















2














It doesn't work, because you're comparing two strings and not numbers. Mathematical operators like > can't be applied to strings.



This is what you're looking for:



private void ArduinoTMR_Tick(object sender, EventArgs e)
{
string ard1 = Arduino.ReadLine();

label2.Text = ard1;

if(!int.TryParse(ard1, out var state)) return; // try to convert text to a number

label2.BackColor = state > 0 ? Color.Red : Color.Blue; // now compare the parsed number to the number 0
}


If what you get from your Arduino is only 1 or 0, you could think about parsing it to a byte as well.






share|improve this answer





























    2














    You can't use > or < operators with the strings, you have to do this before:



    int ard1converted=Convert.ToInt32(ard1);


    and then



    if (ard1converted> 0)
    //do something





    share|improve this answer





















      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%2f53410969%2fread-data-from-arduino-and-react-when-data-is-larger-than-a-zero%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      It doesn't work, because you're comparing two strings and not numbers. Mathematical operators like > can't be applied to strings.



      This is what you're looking for:



      private void ArduinoTMR_Tick(object sender, EventArgs e)
      {
      string ard1 = Arduino.ReadLine();

      label2.Text = ard1;

      if(!int.TryParse(ard1, out var state)) return; // try to convert text to a number

      label2.BackColor = state > 0 ? Color.Red : Color.Blue; // now compare the parsed number to the number 0
      }


      If what you get from your Arduino is only 1 or 0, you could think about parsing it to a byte as well.






      share|improve this answer


























        2














        It doesn't work, because you're comparing two strings and not numbers. Mathematical operators like > can't be applied to strings.



        This is what you're looking for:



        private void ArduinoTMR_Tick(object sender, EventArgs e)
        {
        string ard1 = Arduino.ReadLine();

        label2.Text = ard1;

        if(!int.TryParse(ard1, out var state)) return; // try to convert text to a number

        label2.BackColor = state > 0 ? Color.Red : Color.Blue; // now compare the parsed number to the number 0
        }


        If what you get from your Arduino is only 1 or 0, you could think about parsing it to a byte as well.






        share|improve this answer
























          2












          2








          2






          It doesn't work, because you're comparing two strings and not numbers. Mathematical operators like > can't be applied to strings.



          This is what you're looking for:



          private void ArduinoTMR_Tick(object sender, EventArgs e)
          {
          string ard1 = Arduino.ReadLine();

          label2.Text = ard1;

          if(!int.TryParse(ard1, out var state)) return; // try to convert text to a number

          label2.BackColor = state > 0 ? Color.Red : Color.Blue; // now compare the parsed number to the number 0
          }


          If what you get from your Arduino is only 1 or 0, you could think about parsing it to a byte as well.






          share|improve this answer












          It doesn't work, because you're comparing two strings and not numbers. Mathematical operators like > can't be applied to strings.



          This is what you're looking for:



          private void ArduinoTMR_Tick(object sender, EventArgs e)
          {
          string ard1 = Arduino.ReadLine();

          label2.Text = ard1;

          if(!int.TryParse(ard1, out var state)) return; // try to convert text to a number

          label2.BackColor = state > 0 ? Color.Red : Color.Blue; // now compare the parsed number to the number 0
          }


          If what you get from your Arduino is only 1 or 0, you could think about parsing it to a byte as well.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 11:40









          Tobias Tengler

          1,1491215




          1,1491215

























              2














              You can't use > or < operators with the strings, you have to do this before:



              int ard1converted=Convert.ToInt32(ard1);


              and then



              if (ard1converted> 0)
              //do something





              share|improve this answer


























                2














                You can't use > or < operators with the strings, you have to do this before:



                int ard1converted=Convert.ToInt32(ard1);


                and then



                if (ard1converted> 0)
                //do something





                share|improve this answer
























                  2












                  2








                  2






                  You can't use > or < operators with the strings, you have to do this before:



                  int ard1converted=Convert.ToInt32(ard1);


                  and then



                  if (ard1converted> 0)
                  //do something





                  share|improve this answer












                  You can't use > or < operators with the strings, you have to do this before:



                  int ard1converted=Convert.ToInt32(ard1);


                  and then



                  if (ard1converted> 0)
                  //do something






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 12:15









                  matteo cracco

                  1019




                  1019






























                      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%2f53410969%2fread-data-from-arduino-and-react-when-data-is-larger-than-a-zero%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

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Marschland

                      Redirect URL with Chrome Remote Debugging Android Devices