2009-11-02 95 views
0

我在一堵磚牆上!從VB.net 2008的串口讀取信息

我有一個高溫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 
+0

什麼問題?就你的問題而言,一切都進展順利。 – 2009-11-02 13:28:05

+0

同意Konrad,告訴我們什麼是或不正在發生與VB – 2009-11-02 13:49:10

+0

啊 - 對不起! 基本上,我運行程序時出現超時錯誤。 似乎沒有任何信息通過COM1傳出......但我知道它是! – 2009-11-02 14:02:14

回答

2

我需要將searialport1.DTREnable設置爲true!

它現在的作品 - 這是一個2個月的頭痛了!

0

你可能想贊成使用DataReceived-事件的減少您的設計()的SerialPort類。

如果有一定數量的數據到達(DataReceivedThreshold-Property,我認爲),DataReceived-Event將被觸發。但事件將在另一個線程上觸發,所以如果您嘗試更改任何控件,請確保使用Invoke()。

+1

+1 - 我還建議查看PCB發送的行尾字符,以確保它與串行端口類在ReadLine()函數中所期望的相符,儘管我同意@Bobby基於事件的模型是要走的路。 – 2009-11-02 14:18:03

+0

@Miky D:提及ReadLine()/ NewLine-Character事物的+1 ...我總是忘記這一點。 :d – Bobby 2009-11-02 14:22:09

+0

我改成 私人小組DataReceived1(BYVAL發件人爲對象,BYVALË作爲System.IO.Ports.SerialDataReceivedEventArgs)處理SerialPort1.DataReceived MSGBOX( 「W」) ListBox1.Invoke(新myDelegate1(AddressOf新的對象(){}) 但沒有任何反應 我添加了msgbox,看看子有沒有觸發 - 它不? – 2009-11-02 14:39:20

-1

也許THIS後可以幫助你。您需要將開放聲明更改爲

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

Ahm ...我不想被粗魯,噢,上帝沒有......但是這就像使用木製日誌作爲螺絲刀一樣......至少在DotNET中(你鏈接的帖子是VBA,這是更有限的)。 – Bobby 2009-11-02 14:17:27

+0

此代碼甚至不能在.NET中工作。 – 2009-11-02 14:53:39

+0

true Konrad ...抱歉 - 我的VBA內容太深 – MikeD 2009-11-02 15:12:25