2014-11-24 115 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添加到缓冲区