VB.net实时从串口接收数据 [英] Receive data from serialport in realtime VB.net

查看:153
本文介绍了VB.net实时从串口接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建此代码是为了从串行端口 (com) 接收数据

I created this code to receive data from serial port (com)

 Private port As New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)

Function ReceiveSerialData() As String

    Dim returnStr As String = ""

    Dim com1 As IO.Ports.SerialPort = Nothing
    Try

        port.Open()

        port.ReadTimeout = 1000

        Dim Incoming As String

            Incoming = port.ReadLine()

        returnStr = Incoming

    Catch erreur As IOException
        returnStr = "Check Port"
    Catch ex As TimeoutException
        returnStr = "Error: Serial Port read timed out."
    Finally
        If port IsNot Nothing Then port.Close()
    End Try

    Return returnStr
End Function



Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

    Label2.Content = ReceiveSerialData()

End Sub

我想创建一个事件,以便在数据实时更改时重新执行此代码.

I want to create an event to re execute this code when the data's changed in real time.

我的表格是这样的:

有什么建议吗?

推荐答案

好吧,您总是可以使用评论中提到的某种形式的 Do While True.不理想但是...

Well, you can always go with some form of the Do While True mentioned in the comments. Not ideal but...

这是您代码的修改版本,将阅读部分包含在不同的线程中.当它读取某些内容时,它会引发一个您可以订阅的事件(或者您可以在阅读后直接将文本放入您的控件中).该端口始终保持打开状态,因此在此示例中,它在表单关闭时也处于关闭状态.

This is a modified version of your code, to include the reading part in a different thread. When it reads something, it raises an event you can subscribe to (or you could simply put the text in your control directly after reading). The port stays open all the time, so in this example it is being closed when the form is closed.

试试看是否适合你.

Private port As New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
Private rdthread As System.Threading.Thread
Event PortData(ByVal Message as String)

Private Sub readPort()
    Do While True
        Try
            Dim message As String = port.ReadLine()
            RaiseEvent PortData(message)
            System.Threading.Thread.Sleep(0)
            My.Application.DoEvents()
        Catch ex As Exception
            Exit Do
        End Try
    Loop
End Sub    

Private Sub HandleData(ByVal message as string) Handles Me.PortData
    Label2.Content = message
End Sub

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    InitPort()
End Sub

Private Sub Window_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Try
        If port.IsOpen Then port.Close()
    Finally
        port.Dispose()
    End Try
End Sub 

Sub InitPort() 
    Dim com1 As IO.Ports.SerialPort = Nothing
    Try
        port.Open()
        port.ReadTimeout = 1000

        rdthread = New System.Threading.Thread(AddressOf readPort)
        rdthread.Start()

    Catch erreur As IOException
        returnStr = "Check Port"
    Catch ex As TimeoutException
        returnStr = "Error: Serial Port read timed out."
    Catch ex as Exception

    End Try
End Sub

这篇关于VB.net实时从串口接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆