我想要做的事:(文本的大牆,所以我標記每個部件,使其更易於閱讀)試圖調用SerialPort_DataReceived事件下的多個功能
你好,我正在做的應用於VS 2010中,用於RFID控制的磁性門鎖電路,該電路通過串口接收RFID標籤ID,從數據庫(SQL Server 2005)對其進行身份驗證,通過串口向電路發送信號,並保存當前時間和ID到數據庫中。
我設計的微控制器電路工作原理如下:從RFID模塊讀取標籤ID>按下電路上的按鈕,通過串口將標籤ID發送到PC>接收一串數據,即「接受」或「拒絕」 >根據接收到的數據串打開紅色LED或取消磁性鎖定。
我做了什麼:
我分了上述任務到功能 - CheckValidate()來驗證從DB,送出數據()ID數據串發送到我的電路,中級()來獲取ID的相應從DB和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事件下執行的函數將受到讚賞。
謝謝。