如何保持来自 Arduino 串行的数据写入 VB.NET TextBox
how to keep data comming from Arduino serial written in VB.NET TextBox
我正在将数据从 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
问题出在这段代码中
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
我错过了 &
放在 =
之前
我正在将数据从 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
问题出在这段代码中
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
我错过了 &
放在 =