2016-04-28 55 views
0

我正在嘗試將我的串行數據接收到從一個類的文本框寫入到我的窗體。無法讀取串行數據接收到文本框

我使用的代碼:

Public Shared Sub DataReceviedHandler(sender As Object, e As SerialDataReceivedEventArgs) 

Dim sp As SerialPort = CType(sender, SerialPort) 
Dim indata As String = sp.ReadExisting() 
frmRBATestMain.RichTextBox1.AppendText("Data Received:" + Environment.NewLine) 
frmRBATestMain.RichTextBox1.AppendText(indata + Environment.NewLine) 
End Sub 

出於某種原因,它不會寫入RichTextBox的,它總是保持空白?

但是,如果我在控制檯運行它,它的工作原理。爲什麼這不起作用?

我試圖複製此:

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.90).aspx

我有一個從一個類閱讀的形式。

+0

什麼是調試器告訴你,當你在代碼中設置一個斷點,並運行你的應用程序? –

+0

您是否試圖運行控制檯並將數據接收器接收到表單中的文本框中,或者只需按一下按鈕即可發送命令並收集收到的數據? – noidea

+0

@KenWhite它顯示寫入文本框的數據收到但從未去。 –

回答

0

嘗試是這樣的:

Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived 
    ReceivedText(SerialPort1.ReadExisting()) 
End Sub 

Private Sub ReceivedText(ByVal [text] As String) 
    If Me.TextBox1.InvokeRequired Then 
     Dim x As New SetTextCallBlack(AddressOf ReceivedText) 
     Me.Invoke(x, New Object() {(text)}) 
    Else 
     Me.TextBox1.Text &= [text] 
    End If 
End Sub 
+0

你把這從另一個線程...我試過這個,它不工作.. –

+0

是的,我沒有彌補代碼。你如何打開連接? – noidea