如何保持从VB.NET TextBox中编写的Arduino串行数据来

时间:2018-11-19 10:28:21

标签: vb.net arduino-uno

我正在将数据从arduino发送到VB.NET应用程序,如rfid号,将其显示在TextBox3.Text中,传输的数据没有问题,但在TextBox3.text中显示之后,然后将其从文本框中删除,并且我希望它位于文本框中删除它,直到我删除它。我该如何实现?

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

1 个答案:

答案 0 :(得分:0)

问题出在此代码中

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

我错过了&放在=之前