簡體   English   中英

VB.NET If 語句基於 Arduino 串行 Output

[英]VB.NET If statement based on Arduino Serial Output

I am building a VB.NET Win Forms app that is reading the serial output of an Arduino and I want the VB.Net app to do something based on what it reads from the Arduino. 我遇到的問題是,即使我可以從 Arduino 中讀取,我似乎也無法將其寫入 if 語句。

Arduino 是一個簡單的 3 按鈕原理圖。 連接到數字引腳 2、3 和 4 的紅色、黃色和綠色按鈕。按下一個按鈕時,Arduino 輸出文本“紅色按鈕打開”、“黃色按鈕打開”或“綠色按鈕打開”,具體取決於哪個按鈕按鈕被按下。

在 VB.Net 應用程序中,我有一個名為 lblHintRequest 的 label。 我可以將 arduino 系列 output 加載到這個 label 上。 但是,一旦我將 arduino 串行 output 加載到此 label 上,我似乎無能為力。 我需要的是一個 if 語句,它將讀取按下了哪個按鈕,然后根據按下的按鈕執行某些操作。 我還嘗試添加一個每 500 毫秒計時的計時器,它會讀取 label,但這仍然不起作用。 vb.net 應用程序只是不斷跳過 if 語句。

我能想到的最后一件事是使用文本框而不是 label,也許這會有所幫助? 但我真的希望這是一個 label。

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

否則,要處理您的代碼是從不同線程( SerialPortDataReceived事件)調用的事實,您可以將整個邏輯包裝在它自己的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM