首页 > 解决方案 > 如何保持来自用 VB.NET TextBox 编写的 Arduino 串行的数据

问题描述

我正在将数据从 arduino 发送到 VB.NET 应用程序,例如要在 TextBox3.Text 中显示的 rfid 号我删除它。我怎样才能做到这一点?

Arduino代码:

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

void setup() 
{
Serial.begin(9600);   // Initiate a serial communication
SPI.begin();      // Initiate  SPI bus
mfrc522.PCD_Init();   // Initiate MFRC522

}
void loop() 
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) 
{
 return;
}
 // Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) 
 {
   return;
 }
 //Show UID on serial monitor
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
 {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);

  }
   delay(5000);

 }

VB.NET 中的代码是:

  Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
    TextBox2.Text = TextBox3.Text

    Dim str As String = "Server=localhost;Port=3306;Database=testdb;Uid=root;Pwd=password"


    Using con As New MySqlConnection(str)


        Dim query As String = "select * from testdatawhere rfid_tag='" & TextBox3.Text & "' 
                               and Date_Operation<= '" & Date.Now.ToString("yyyy-MM-dd ") & "'
                               and Start_Time<= '" & Date.Now.ToString("HH:mm:ss ") & "' 
                               and End_Time>= '" & Date.Now.ToString("HH:mm:ss ") & "' 
                               or spring_size='' " 'Note:TextBox3 is the RFID number come from RFID arduino
        Dim cm As New MySqlCommand(query, con)


        con.Open()


        Dim rd As MySqlDataReader = cm.ExecuteReader()

        ' Check if any rows exist
        If rd.Read() Then
            If rd.GetString(3) = "small" Then
                SerialPort1.Write("1")

                MessageBox.Show("small")


            ElseIf rd.GetString(3) = "Big" Then
                SerialPort1.Write("2")

                MessageBox.Show("big")

            ElseIf rd.GetString(3) = "Midium" Then
                SerialPort1.Write("3")

                MessageBox.Show("Mid")


            End If

        End If
    End Using
End Sub

以及 VB.NET 中的串行连接代码:

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles   Timer1.Tick
    receivedData = ReceiveSerialData()
    TextBox3.Text = receivedData

End Sub

标签: vb.netarduino-uno

解决方案


问题出在这段代码中

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles   Timer1.Tick
   receivedData = ReceiveSerialData()
   TextBox3.Text = receivedData

End Sub

正确的是这样的:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles   Timer1.Tick
  receivedData &= ReceiveSerialData()
  TextBox3.Text &= receivedData

End Sub

&我之前错过了=


推荐阅读