VB.NET If语句基于Arduino串口输出
VB.NET If statement based on Arduino Serial Output
我正在构建一个 VB.NET Win Forms 应用程序,它正在读取 Arduino 的串行输出,我希望 VB.Net 应用程序根据从 Arduino 读取的内容执行某些操作。我遇到的问题是,即使我可以从 Arduino 读取数据,我似乎也无法将其写入 if 语句。
Arduino 是一个简单的 3 按钮原理图。连接到数字引脚 2、3 和 4 的红色、黄色和绿色按钮。按下一个按钮时,Arduino 输出文本“Red Button On”、“Yellow Button On”或“Green Button On”,具体取决于哪个按下按钮。
在 VB.Net 应用程序中,我有一个名为 lblHintRequest 的标签。我可以将 arduino 串行输出加载到这个标签上。然而,一旦我将 arduino 串行输出加载到这个标签上,我似乎无法对它做任何事情。我需要的是一个 if 语句,它将读取按下的按钮,然后根据按下的按钮执行某些操作。我还尝试添加一个计时器,该计时器每 500 毫秒滴答一次,它会读取标签,但这仍然无效。 vb.net 应用程序一直跳过 if 语句。
我能想到的最后一件事是使用文本框而不是标签,也许这会有帮助?但我真的很想把它当成标签。
VB.NET代码
Imports System.IO.Ports
Public Class frmRoom
Private Sub frmRoom_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SerialPort1.Open()
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Searching for data input
Dim ReadCode As String
ReadCode = SerialPort1.ReadLine
'Print the button press
lblHintRequest.Invoke(New MethodInvoker(Sub() lblHintRequest.Text = ReadCode), Nothing)
'This code will not run even though when I invoke the lblhintrequest it shows re button on
If lblHintRequest.Text = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
'This if statement does not work either
If lblHintRequest.Invoke(New MethodInvoker(Sub() lblHintRequest.Text = ReadCode), Nothing) = "Red Button On" Then
MsgBox("hit")
End If
End Sub
'----------Code below added for solution response on stack overflow ---
Private Sub UpdateHintRequest(code As String)
If lblHintRequest.InvokeRequired Then
lblHintRequest.Invoke(Sub() UpdateHintRequest(code))
--Getting stuff to run here was the solution.
If lblHintRequest.Text = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
Else
End If
End Sub
End Class
Arduino 代码
const int RedButton = 2;
const int YellowButton = 3;
const int GreenButton = 4;
const int ledpin = 13;
int buttonstateRed =0;
int buttonstateYellow =0;
int buttonstateGreen =0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(RedButton, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstateRed = digitalRead(RedButton);
if (buttonstateRed == HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("Red Button On");
}
buttonstateYellow = digitalRead(YellowButton);
if (buttonstateYellow == HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("Yellow Button On");
}
buttonstateGreen = digitalRead(GreenButton);
if (buttonstateGreen == HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("Green Button On");
}
if (buttonstateRed == LOW && buttonstateYellow == LOW && buttonstateGreen == LOW){
digitalWrite(ledpin, LOW);
//Serial.println("No Button Pressed");
}
Serial.flush();
delay(500);
//Serial.println("Hello");
//delay(250);
}
由于您在变量中有 Arduino 返回的值,您可以在 If
语句中使用它:
If ReadCode = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
否则,为了处理您的代码是从另一个线程(SerialPort
的 DataReceived
事件)调用的事实,您可以将整个逻辑包装在它自己的 Sub
:
Private Sub UpdateHintRequest(code As String)
If lblHintRequest.InvokeRequired Then
lblHintRequest.Invoke(Sub() UpdateHintRequest(code))
Else
' Print the button press
lblHintRequest.Text = code
If lblHintRequest.Text = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
If lblHintRequest.Text = "Red Button On" Then
MsgBox("hit")
End If
End If
End Sub
回到你原来的代码:
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Searching for data input
Dim ReadCode As String
ReadCode = SerialPort1.ReadLine
UpdateHintRequest(ReadCode)
End Sub
我正在构建一个 VB.NET Win Forms 应用程序,它正在读取 Arduino 的串行输出,我希望 VB.Net 应用程序根据从 Arduino 读取的内容执行某些操作。我遇到的问题是,即使我可以从 Arduino 读取数据,我似乎也无法将其写入 if 语句。
Arduino 是一个简单的 3 按钮原理图。连接到数字引脚 2、3 和 4 的红色、黄色和绿色按钮。按下一个按钮时,Arduino 输出文本“Red Button On”、“Yellow Button On”或“Green Button On”,具体取决于哪个按下按钮。
在 VB.Net 应用程序中,我有一个名为 lblHintRequest 的标签。我可以将 arduino 串行输出加载到这个标签上。然而,一旦我将 arduino 串行输出加载到这个标签上,我似乎无法对它做任何事情。我需要的是一个 if 语句,它将读取按下的按钮,然后根据按下的按钮执行某些操作。我还尝试添加一个计时器,该计时器每 500 毫秒滴答一次,它会读取标签,但这仍然无效。 vb.net 应用程序一直跳过 if 语句。
我能想到的最后一件事是使用文本框而不是标签,也许这会有帮助?但我真的很想把它当成标签。
VB.NET代码
Imports System.IO.Ports
Public Class frmRoom
Private Sub frmRoom_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SerialPort1.Open()
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Searching for data input
Dim ReadCode As String
ReadCode = SerialPort1.ReadLine
'Print the button press
lblHintRequest.Invoke(New MethodInvoker(Sub() lblHintRequest.Text = ReadCode), Nothing)
'This code will not run even though when I invoke the lblhintrequest it shows re button on
If lblHintRequest.Text = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
'This if statement does not work either
If lblHintRequest.Invoke(New MethodInvoker(Sub() lblHintRequest.Text = ReadCode), Nothing) = "Red Button On" Then
MsgBox("hit")
End If
End Sub
'----------Code below added for solution response on stack overflow ---
Private Sub UpdateHintRequest(code As String)
If lblHintRequest.InvokeRequired Then
lblHintRequest.Invoke(Sub() UpdateHintRequest(code))
--Getting stuff to run here was the solution.
If lblHintRequest.Text = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
Else
End If
End Sub
End Class
Arduino 代码
const int RedButton = 2;
const int YellowButton = 3;
const int GreenButton = 4;
const int ledpin = 13;
int buttonstateRed =0;
int buttonstateYellow =0;
int buttonstateGreen =0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(RedButton, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstateRed = digitalRead(RedButton);
if (buttonstateRed == HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("Red Button On");
}
buttonstateYellow = digitalRead(YellowButton);
if (buttonstateYellow == HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("Yellow Button On");
}
buttonstateGreen = digitalRead(GreenButton);
if (buttonstateGreen == HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("Green Button On");
}
if (buttonstateRed == LOW && buttonstateYellow == LOW && buttonstateGreen == LOW){
digitalWrite(ledpin, LOW);
//Serial.println("No Button Pressed");
}
Serial.flush();
delay(500);
//Serial.println("Hello");
//delay(250);
}
由于您在变量中有 Arduino 返回的值,您可以在 If
语句中使用它:
If ReadCode = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
否则,为了处理您的代码是从另一个线程(SerialPort
的 DataReceived
事件)调用的事实,您可以将整个逻辑包装在它自己的 Sub
:
Private Sub UpdateHintRequest(code As String)
If lblHintRequest.InvokeRequired Then
lblHintRequest.Invoke(Sub() UpdateHintRequest(code))
Else
' Print the button press
lblHintRequest.Text = code
If lblHintRequest.Text = "Red Button On" Then
lblHintRequest.ForeColor = Color.Red
End If
If lblHintRequest.Text = "Red Button On" Then
MsgBox("hit")
End If
End If
End Sub
回到你原来的代码:
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Searching for data input
Dim ReadCode As String
ReadCode = SerialPort1.ReadLine
UpdateHintRequest(ReadCode)
End Sub