从串行端口Visual Basic读取字符串

时间:2014-11-24 09:25:52

标签: vb.net

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()这样的其他方法,但没有任何事情发生。

2 个答案:

答案 0 :(得分: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))

因此,您可以将Char添加到缓冲区

,而不是将String附加到Byte

答案 1 :(得分:0)

Dim str As String = MyCOMPort.ReadExisting()
If Me.InvokeRequired Then
    Me.Invoke(New dlUpdateText(AddressOf updatetext), str)
Else
   updatetext(str)
End If