我正在尝试做的事情:(大墙的文字,所以我标记了每个部分以使其更易于阅读)
您好,我正在 VS 2010 中申请一个 RFID 控制的磁门锁电路,它通过串行端口接收 RFID 标签 ID,从数据库(SQL server 2005)对其进行身份验证,通过串口,并将当前时间和ID保存到数据库中。
我设计的微控制器电路的工作原理如下:从 RFID 模块读取标签 ID > 按下电路上的按钮后通过串口将标签 ID 发送到 PC > 接收一串数据,“接受”或“拒绝” > 打开红色 LED 或根据接收到的数据串停用磁力锁。
我做了什么:
我将上述任务划分为函数 - CheckValidate() 用于从 DB 验证 ID,SendData() 用于将数据字符串发送到我的电路,Intermediate() 用于从 DB 中获取 ID 的相应名称,SaveData() 用于保存 ID、名称和当前时间进入数据库。
问题:
当我单独执行它们时,它们每个都可以完美地工作,但是我需要它们在一个事件下一个接一个地执行,即 SerialPort_DataReceived 事件。我尝试在 DataReceived 事件的子项下一个接一个地调用所有函数。我尝试在 DataReceived 事件下调用第一个函数,然后将其他函数相互嵌套。两者都不起作用,第一个函数执行并且我的应用程序停止响应。
我的完整代码:
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.IO.Ports
Public Class Form1
Dim sqlcon As New SqlConnection
Dim Chars(11) As Char
Dim CharsCnt As Integer
Dim currtime As String
Dim savename As String
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
CharsCnt += SerialPort1.Read(Chars, CharsCnt, Chars.Length - CharsCnt)
If CharsCnt = Chars.Length Then
Me.Invoke(New SetReceivedText(AddressOf SetText))
CharsCnt = 0
End If
End Sub
Delegate Sub SetReceivedText()
Private Sub SetText()
CheckValidate()
End Sub
Private Sub CheckValidate()
Dim cmd As New SqlCommand
cmd.Connection = sqlcon
If (sqlcon.State = ConnectionState.Closed) Then
sqlcon.Open()
End If
cmd.CommandText = "select Name from masterdata where ID = '" & Chars & "'"
Dim da As New SqlDataAdapter
Dim dt As New DataTable
da.SelectCommand = cmd
da.Fill(dt)
If (dt.Rows.Count = 0) Then
TextBox2.Text = "REJECTED"
dt.Dispose()
Else
TextBox2.Text = "ACCEPTED"
End If
sqlcon.Close()
SendData()
End Sub
Private Sub SendData()
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Using com1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM2")
com1.WriteLine(TextBox2.Text)
End Using
If TextBox2.Text = "ACCEPTED" Then
Intermediate()
End If
End Sub
Private Sub Intermediate()
TextBox1.Text = Chars
Dim cmd As New SqlCommand
cmd.Connection = sqlcon
cmd.CommandText = "select Name from masterdata where ID = '" & Chars & "'"
If (sqlcon.State = ConnectionState.Closed) Then
sqlcon.Open()
End If
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim dt As New DataTable
da.SelectCommand = cmd
da.Fill(ds)
da.Fill(dt)
DataGridView1.DataSource = ds.Tables(0)
sqlcon.Close()
SaveData()
End Sub
Private Sub SaveData()
currtime = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt")
savename = DataGridView1.CurrentRow.Cells(0).Value.ToString
Dim cmd As New SqlCommand
If (sqlcon.State = ConnectionState.Closed) Then
sqlcon.Open()
End If
cmd.Connection = sqlcon
cmd.CommandText = "insert into logdata values('" & Chars & "','" & savename & "','" & currtime & "')"
Dim ds As New DataSet
Dim da As New SqlDataAdapter
da.SelectCommand = cmd
da.Fill(ds)
sqlcon.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each s In System.IO.Ports.SerialPort.GetPortNames()
ListBox1.Items.Add(s)
Next s
Label1.Text = "All COM ports are closed."
sqlcon.ConnectionString = "Data Source=localhost;User ID=TAS; password = TAS123; database=secure"
sqlcon.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
If ListBox1.SelectedIndex = -1 Then
MessageBox.Show("Please Select a Port.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
Else
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = ListBox1.SelectedItem.ToString
SerialPort1.Open()
Label1.Text = "'" & ListBox1.SelectedItem.ToString & "' COM port is Open"
End If
Catch
MessageBox.Show("Could not open Port, please try again!", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
当我想做的工作时:(使用按钮单独执行每个功能)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SendData()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox2.Text = "ACCEPTED" Then
Intermediate()
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
SaveData()
End Sub
任何有关如何在一个 DataRecieved 事件下执行所有功能的帮助将不胜感激。
谢谢。