read data from arduino and react when data is larger than a zero
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
add a comment |
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
And what is your question?
– svtag
Nov 21 '18 at 11:31
1
why do you even think that you can compare astring
with the>
operator?? you can't. You would first have to convert it to int
– sLw
Nov 21 '18 at 11:39
add a comment |
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
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
c# serial-port
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 astring
with the>
operator?? you can't. You would first have to convert it to int
– sLw
Nov 21 '18 at 11:39
add a comment |
And what is your question?
– svtag
Nov 21 '18 at 11:31
1
why do you even think that you can compare astring
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 11:40
Tobias Tengler
1,1491215
1,1491215
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 21 '18 at 12:15
matteo cracco
1019
1019
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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