在VB.net 2008中从串口读取信息

发布于 2024-08-09 14:45:51 字数 3111 浏览 3 评论 0原文

我在砖墙上!

我有一个温度 PCB,可以通过串行端口报告温度。

我可以打开超级终端并接收我想要的所有数据 - 所以我知道该装置正在工作...但我想创建一个 VB 应用程序,以便我可以使用收到的数据。

当我运行该程序时,出现此错误:

System.TimeoutException: The operation has timed out.
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Ports.SerialPort.InternalRead(Char[] buffer, Int32 offset, Int32 count, Int32 timeout, Boolean countMultiByteCharsAsOne)
   at System.IO.Ports.SerialPort.ReadTo(String value)
   at System.IO.Ports.SerialPort.ReadLine()
   at Temperature.Form1.ReadFromCom() in C:\Documents and Settings\asamuel\Desktop\VB Project Sollutions\Temperature2\Temperature\Form1.vb:line 43

有人可以帮助我吗?我要疯了!

在超级终端中,数据如下所示:

R V1.0 2002-01-06 20:37:37 C
1 0027.00
2 0027.00
3 0027.06
4 0027.18
1 0027.00
2 0027.00
3 0027.06
4 0027.18
1 0027.00
2 0027.06

我的 VB 应用程序代码如下所示:

Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Dim SerialPort1 As New SerialPort
    Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
    Dim abortThread As Boolean

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text Is "Start Capture" Then
            Try
                abortThread = False
                SerialPort1.Open()
                readThread.Start()
                Button1.Text = "Stop Capture"
            Catch ex As Exception
                MsgBox("Another program is already using COM1." & vbCrLf & vbCrLf & _
                       "Please try again later", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "COM1 Not Available")
            End Try

        ElseIf Button1.Text Is "Stop Capture" Then
            abortThread = True
            Button1.Text = "Start Capture"
        End If
    End Sub

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With SerialPort1
            .PortName = "COM1"
            .BaudRate = 2400
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = 1
            .ReadTimeout = 500
        End With
    End Sub

    Public Sub ReadFromCom()
        While abortThread = False
            Try
                Dim message As String = SerialPort1.ReadLine
                updateStatus("Received: " & message)
            Catch ex As TimeoutException
                updateStatus(ex.ToString)
            End Try
        End While            
    End Sub

    Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    Public Sub updateStatus(ByVal newStatus As String)
        If Me.InvokeRequired Then
            Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
            Me.Invoke(upbd, New Object() {newStatus})
        Else
            TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
        End If
    End Sub
End Class

I'm at a brick wall!

I have a teperature PCB that reports the temp back via serial port.

I can open Hyper Terminal and receive all the data I want - so I know the unit is working... but I want to create a VB app so I can use the data received.

When I run the program I get this error:

System.TimeoutException: The operation has timed out.
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Ports.SerialPort.InternalRead(Char[] buffer, Int32 offset, Int32 count, Int32 timeout, Boolean countMultiByteCharsAsOne)
   at System.IO.Ports.SerialPort.ReadTo(String value)
   at System.IO.Ports.SerialPort.ReadLine()
   at Temperature.Form1.ReadFromCom() in C:\Documents and Settings\asamuel\Desktop\VB Project Sollutions\Temperature2\Temperature\Form1.vb:line 43

Can someone PLEASE help me! I'm going mad!

In hyper terminal the data comes through like this:

R V1.0 2002-01-06 20:37:37 C
1 0027.00
2 0027.00
3 0027.06
4 0027.18
1 0027.00
2 0027.00
3 0027.06
4 0027.18
1 0027.00
2 0027.06

My VB app code looks like this:

Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Dim SerialPort1 As New SerialPort
    Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
    Dim abortThread As Boolean

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text Is "Start Capture" Then
            Try
                abortThread = False
                SerialPort1.Open()
                readThread.Start()
                Button1.Text = "Stop Capture"
            Catch ex As Exception
                MsgBox("Another program is already using COM1." & vbCrLf & vbCrLf & _
                       "Please try again later", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "COM1 Not Available")
            End Try

        ElseIf Button1.Text Is "Stop Capture" Then
            abortThread = True
            Button1.Text = "Start Capture"
        End If
    End Sub

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With SerialPort1
            .PortName = "COM1"
            .BaudRate = 2400
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = 1
            .ReadTimeout = 500
        End With
    End Sub

    Public Sub ReadFromCom()
        While abortThread = False
            Try
                Dim message As String = SerialPort1.ReadLine
                updateStatus("Received: " & message)
            Catch ex As TimeoutException
                updateStatus(ex.ToString)
            End Try
        End While            
    End Sub

    Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    Public Sub updateStatus(ByVal newStatus As String)
        If Me.InvokeRequired Then
            Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
            Me.Invoke(upbd, New Object() {newStatus})
        Else
            TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
        End If
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

避讳 2024-08-16 14:45:51

我需要将 searialport1.DTREnable 设置为 true!

现在可以了 - 两个月的头痛消失了!

I needed to set searialport1.DTREnable to true!

It now works - that's a 2 month headache gone!

血之狂魔 2024-08-16 14:45:51

您可能想放弃您的设计,转而使用 SerialPort-Class 的 DataReceived-Event()。

如果达到一定量的数据(我认为是 DataReceivedThreshold-Property),则将触发 DataReceived-Event。但该事件将在另一个线程上触发,因此如果您尝试更改任何控件,请确保使用 Invoke()。

You might wanna drop your design in favor of using the DataReceived-Event() of the SerialPort-Class.

The DataReceived-Event will be fired if a certain amount of data has arrived (DataReceivedThreshold-Property, I think). But the event will be fired on another thread, so make sure that you use Invoke() if you try to change any controls.

指尖微凉心微凉 2024-08-16 14:45:51

也许这篇文章可以帮助你。您需要将 open 语句更改为

...
open "COM1:" for Input as #1
Input #1, MyString
...

Maybe THIS post can help you. You need to change the open statement to

...
open "COM1:" for Input as #1
Input #1, MyString
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文