2014-11-24 113 views
0
Imports System.IO.Ports 
Imports System.Text 

Public Class Form4 
    Dim myStringBuilder As String 
    Dim insert As New OleDb.OleDbCommand 
    Dim cnn As New OleDb.OleDbConnection 
    Public user As String 

Private Sub Serialport2_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived 
    myStringBuilder = SerialPort2.ReadExisting() 
    Me.Invoke(New EventHandler(AddressOf UpdateControls)) 
End Sub 

Private Sub UpdateControls(ByVal sender As Object, ByVal e As EventArgs) 
    Dim A As String = myStringBuilder 
    Dim Sqql As String 

    If Not cnn.State = ConnectionState.Open Then 
     cnn.Open() 
    End If 

    insert.Connection = cnn 


    Dim dt As New DataTable 
    Sqql = "SELECT * FROM `FileInfo` WHERE `File ID`='" & A & "'" 
    Dim cmd As New OleDb.OleDbDataAdapter(Sqql, cnn) 
    cmd.Fill(dt) 
    Dim i As Integer = dt.Rows.Count 
    Dim todaysdate As String = String.Format("{0:dd/MM/yyyy}", DateTime.Now) 
    If i = 1 Then 
      insert.CommandText = "INSERT INTO `File Log`(File ID,Name,Information,Time,Date) " & _ 
       " VALUES('" & A & "','" & dt.Rows(0).Item("Name") & "','" & user & " telah" & dt.Rows(0).Item("Status") & "File" & "','" & 
_ 
       TimeOfDay & "','" & todaysdate & "')" 
      textBox1.Text += dt.Rows(0).Item("Name") & " " & TimeOfDay & " " & todaysdate & 

Environment.NewLine 
      textBox1.Select(textBox1.TextLength, 0) 
      textBox1.ScrollToCaret() 
      insert.ExecuteNonQuery() 
      myStringBuilder = "" 
     Else 
      myStringBuilder = "" 
      textBox1.Text += A & Environment.NewLine 
     End If 


End Sub 


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    cnn = New OleDb.OleDbConnection 
    cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\data.mdb" 

    If SerialPort2 IsNot Nothing Then 
     SerialPort2.Close() 
    End If 

    SerialPort2 = My.Computer.Ports.OpenSerialPort("COM27", 9600, Parity.None, 8, StopBits.One) 
    textBox1.Text = "-- Door Have Open -- " & Environment.NewLine & Environment.NewLine 


End Sub 

Private Sub textBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textBox1.TextChanged 

End Sub End Class 

在我的串口監視器視圖中它會正確顯示,但在Visual Basic中它會自動斷行並且不在一行中顯示字符串。 我試過其他方法像serialport.readline()但沒有發生。從串口讀取字符串Visual Basic

+0

ReadExisting()是這個問題的。你通常只會得到一個或兩個字符。您需要先獲得完整的回覆。就像使用ReadLine()一樣,只要設備幫助併發送特定的字符來指示行尾即可。換行是標準的。 – 2014-11-24 11:48:08

+0

感謝您的回覆。所以,我應該改變什麼? 。即時通訊這種新程序。 – ZackRich 2014-11-24 14:03:49

+0

在調用Me.Invoke()之前,您必須閱讀完整的響應。這個問題無法從問題中猜出,你沒有描述設備發送的內容。如果你不知道,那麼ReadLine()值得一試。 – 2014-11-24 14:44:22

回答

0

如果您有您的串口提取數據的問題,你可以試試這個:

Sub SerialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) 

    Dim currentSP As SerialPort = Convert.ChangeType(sender, GetType(SerialPort)) 
    Dim strBuilder As System.Text.StringBuilder = New System.Text.StringBuilder() 

    For index = 1 To currentSP.BytesToRead   
     strBuilder.Append(Convert.ChangeType(currentSP.ReadByte(), GetType(Char))) 
    Next 

    ' Have a global string to allow the threads to have a shared resource 
    myString = strBuilder.ToString() 

    Me.Invoke(New EventHandler(AddressOf UpdateControls)) 

End Sub 

它應該以同樣的方式作爲SerialPort.ReadLine(),這種方法也可以讓你操縱不過你想要的數據。相反,用字符串的工作,你總是可以工作的數據是這樣的:

Dim buffer As Byte() = New Byte(currentSP.BytesToRead) {} 
buffer(index) = Convert.ChangeType(currentSP.ReadByte(), GetType(Byte)) 

因此,而不是追加CharString可以將Byte添加到緩衝區