รบกวนสอบถามครับ คือตอนนี้ผมทำโปรแกรมรับค่าจาก Serial Port เพื่อให้ข้อมูลมาแสดงบน TextBox โดยเมื่อมีข้อมูลมาแสดงบน TextBox แล้วให้โปรแกรมตรวจสอบกับฐานข้อมูลว่ามีข้อมูลอยู่หรือไม่ ถ้ามีให้ทำการบันทึกเวลาเข้าไป ทีนี้ผมสามารถเขียนโปรแกรมรับ ส่งข้อมูลกับ Serial Port ได้แล้วครับ ติดอยู่ที่การบันทึกข้อมูลลงฐานข้อมูล ไม่ว่ายังไง มันก็ตรวจสอบว่าไม่เป็นสมาชิกอ่ะครับ ทั้งๆข้อมูลที่รับมาก็เป็นสมาชิกและต้องบันทึกข้อมูลในฐานข้อมูล
Imports System.Data
Imports System.Data.SqlClient
Imports VB = Microsoft.VisualBasic
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel
Imports System
Public Class Form1
Dim sqlcon As SqlConnection 'ประกาศตัวแปรเพื่อเชื่อต่อกับฐานข้อมูล
Dim sqlcmd As SqlCommand
Dim ds As DataSet
Dim da As SqlDataAdapter
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
cmbBaud.Items.Add(9600)
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
Try
sqlcon = New SqlConnection("Server=TOSHIBA-PC\SQLEXPRESS;Database=parking;Trusted_Connection=True;") 'เชื่อมต่อกับฐานข้อมูล
sqlcon.Open()
'MsgBox("เชื่อมต่อฐานข้อมูลแล้ว")
Call showdata2()
Catch ex As Exception
MsgBox("Error")
End Try
End Sub
Sub showdata2()
ds = New DataSet
da = New SqlDataAdapter("SELECT * FROM inout;", sqlcon)
da.Fill(ds, "inout")
DataGridView1.DataSource = ds.Tables("inout")
End Sub
Sub cleardata()
txtSN.Clear()
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
btnConnect.Enabled = False 'Disable Connect button
btnDisconnect.Enabled = True 'and Enable Disconnect button
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'Close our Serial Port
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label3.Text = TimeOfDay
Label4.Text = Date.Today
Label9.Text = Date.Today
Label10.Text = TimeOfDay
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
'compares the ID of the creating Thread to the ID of the calling Thread
If Me.txtSN.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.txtSN.Text &= [text]
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("port is Closed", vbCritical)
End If
End Sub
Private Sub txtSN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSN.TextChanged
If txtSN.Text <> "" Then
Dim dt As String
dt = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")
Dim strSQL As String
Dim intNumRows As Integer
sqlcon = New SqlConnection("Server=TOSHIBA-PC\SQLEXPRESS;Database=parking;Trusted_Connection=True;")
sqlcon.Open()
strSQL = "SELECT COUNT(*) FROM member WHERE CardSN = '" & txtSN.Text & "'"
sqlcmd = New SqlCommand(strSQL, sqlcon)
intNumRows = sqlcmd.ExecuteScalar()
If intNumRows > 0 Then
Thread.Sleep(1000)
ds = New DataSet
da = New SqlDataAdapter("INSERT INTO inout (CardSN,dtin) VALUES ('" & txtSN.Text & "','" & dt & "');", sqlcon)
da.Fill(ds, "inout")
Call showdata2()
SerialPort1.Write("a")
Else
SerialPort1.Write("s")
End If
End If
sqlcon.Close()
End Sub
End Class
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = "COM3"
SerialPort1.BaudRate = "115200"
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Open()
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.txtSN.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.txtSN.Text &= [text]
End If
End Sub