สอบถามครับ คือผมต้องการเขียนโปรแกรมให้รับค่าจากSerial Port ครับ และเมื่อรับค่านั้นมาก็ให้ทำการตรวจสอบว่าค่าที่รับมานั้นเป็นค่าที่อนุญาตให้บันทึกหรือไม่ ถ้าเป็นค่าที่อนุญาตให้ส่ง a กลับไปยังอุปกรณ์ผ่าน Serial Port และถ้าเป็นค่าที่ไม่อนุญาตให้บันทึกให้ส่ง b กลับไปยังอุปกรณ์ผ่าน Serial Port เป็นต้นครับ ตอนนี้ผมสามารถให้โปรแกรมรับค่าจาก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
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
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 Me.txtSN.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
If intNumRows > 0 Then
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("b")
End If
Else
Me.txtSN.Text &= [text]
End If
sqlcon.Close()
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
End Class
If Me.txtSN.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
If intNumRows > 0 Then
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("b")
End If
Else
Me.txtSN.Text &= [text]
End If