我的vb.net应用程序出现问题,该应用程序使用RFID MFRC522和Arduino到Visual Basic。来自Arduino串口的数据已成功发送到VB.net文本框。如果文本框仅从Arduino(卡片标签的UUID)接收到特定数据,那么如果有匹配的卡片标签,则会检索SQL中的数据。
扫描时检索数据并自动保存没有问题。唯一的问题是我扫描RFID标签。 UUID被转移到文本框,没有数据出现在标签上。但是,当我单击文本框内的UUID卡标记的末尾并单击退格键时,将检索数据。我只想自动检索扫描的标签并显示数据......
这是代码:
Private Sub Students_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadtable()
Timer2.Start()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim StringIn As String = SerialPort1.ReadLine()
Me.Invoke(Sub() studtag.Text = StringIn) 'we need to invoke since we're on another thread
End Sub
Private Sub studtag_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles studtag.TextChanged
If studtag.TextLength = 8 Then
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
Dim query As String
query = "select * from dat.students"
cmd = New MySqlCommand(query, con)
Dim table As New DataTable
Try
con.Open()
'Gets or sets an SQL statement or stored procedure used to select records in the database.
With cmd
.Connection = con
.CommandText = "SELECT * from students where `studtags`='" & studtag.Text & "';"
End With
da.SelectCommand = cmd
da.Fill(table)
'it gets the data from specific column and fill it into Label
studtag.Text = table.Rows(0).Item(0)
idno.Text = table.Rows(0).Item(1)
lastxt.Text = table.Rows(0).Item(2)
firstxt.Text = table.Rows(0).Item(3)
middletxt.Text = table.Rows(0).Item(4)
dob.Text = table.Rows(0).Item(6)
agetxt.Text = table.Rows(0).Item(7)
crsetxt.Text = table.Rows(0).Item(9)
tagtxt.Text = studtag.Text
timein.Text = times.Text
dr = cmd.ExecuteReader()
dr.Read()
If dob.Text = datenow.Text Then
greet.Text = "Happy Birthday To You"
End If
Dim img() As Byte = CType(dr("studpic"), Byte())
Using ms As New IO.MemoryStream(img)
PictureBox1.Image = Image.FromStream(ms)
End Using
insert()
loadtable()
Catch ex As Exception
Notenrolled.Show()
Finally
con.Dispose()
con.Close()
End Try
End If
End Sub
Public Sub insert()
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
Dim reader As MySqlDataReader
Dim mstream As New System.IO.MemoryStream()
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()
Try
con.Open()
Dim query3 As String
query3 = "insert into dat.studlogs (studtags,idno,lastxt,firstxt,middletxt,dob,log,age,timein,crse,studpic) values ('" & tagtxt.Text & "','" & idno.Text & "','" & lastxt.Text & "','" & firstxt.Text & "','" & middletxt.Text & "','" & dob.Text & "','" & log.Text & "','" & agetxt.Text & "','" & timein.Text & "','" & crsetxt.Text & "',@studpic)"
cmd = New MySqlCommand(query3, con)
cmd.Parameters.AddWithValue("@studpic", arrImage)
reader = cmd.ExecuteReader
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
Public Sub loadtable()
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
Dim SDA As New MySqlDataAdapter
Dim dbDataset As New DataTable
Dim bSource As New BindingSource
Try
con.Open()
Dim query3 As String
query3 = "select studtags,idno,lastxt,firstxt,middletxt,dob,log,age,timein,crse from dat.studlogs"
cmd = New MySqlCommand(query3, con)
SDA.SelectCommand = cmd
SDA.Fill(dbDataset)
bSource.DataSource = dbDataset
DataGridView1.DataSource = bSource
SDA.Update(dbDataset)
DataGridView1.Sort(DataGridView1.Columns(8), System.ComponentModel.ListSortDirection.Descending)
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
Arduino代码:
#include<SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
void loop() {
// Look for new cards
if (! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been read
if (! rfid.PICC_ReadCardSerial())
return;
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? "0":"");
Serial.print(buffer[i], HEX);
}
}
如您所见,从Arduino收到的数据必须是8位数或字符。例如,我在Arduino的串行监视器中的数据是7B6BA321。它匹配,所以过程开始。但是它无法检索数据......我需要在文本框中单击文本的末尾并单击退格键以进行检索。此外,当我单击退格键时,文本框中的8个字符的数据不会更改或删除最后一个字母或数字并进行检索。
我必须单击退格键两次以删除一个字母,如果我手动输入8个字符,它将从数据库中检索。我已经尝试过其他事件而不是textchanged而且我失败了。请帮我。如果您有任何问题,请随时发表评论。
我将提供截屏。