none
[VS2005]RS232通訊問題 RRS feed

  • 問題

  • 編譯器或直譯器: VB2005 Express SP1
    作業系統:Windows XP Professional SP2
    問題描述:
    我使用RS232接收多行資料進來到TextBox1裡面,然後用ZedGraph來畫出折線圖的曲線,可是會遇到一些執行緒上的問題,一直沒有辦法解決,想請教各位大大. (以下是我的相關資訊)
    ZedGraph.dll
    出處 http://zedgraph.org/wiki/index.php?title=Tutorial:Initial_Chart_Demo

    問題1:我要在RS232每次接收到資料之前將TextBox的內容清除掉之後再將收到的資料擺進去,但是在sp.DataReceived裡面卻不能做TextBox清除的動作.
    問題2:如何將TextBox的內容一行一行的擺進ZedGraph的PointPairList內(X從0開始遞增,Y就是要擺進去的值)

    接收到的資料範例:
    <210,152,151,149,149,149,149,148,150,149,149,149,45,46,45,44,45,43,44,43,43,43,144, ... ,142,141,140,140>

    Source Code:
        Dim WithEvents sp As System.IO.Ports.SerialPort
        Delegate Sub SetTextCallback(ByVal [text] As String)

        Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
            Try
                ReDim buff(sp.BytesToRead - 1)
                sp.Read(buff, 0, buff.Length)
                Dim rxStr As String = ""
                Dim i As Integer = 0
                For i = 0 To buff.Length - 1
                    Select Case buff(i)
                        Case 44 ','
                            rxStr += vbCrLf
                        Case 60 '<'
                        Case 62 '>'
                        Case Else
                            rxStr += HexToStr(Hex(buff(i)))
                    End Select
                Next
                SetText(rxStr)
            Catch ex As Exception
                MessageBox.Show("讀取錯誤:" + ex.Message, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                If sp.IsOpen Then
                    sp.Close()
                End If
            End Try
        End Sub

        Private Sub SetText(ByVal [text] As String)
            Try
                If TextBox1.InvokeRequired Then
                    Dim d As New SetTextCallback(AddressOf SetText)
                    Me.Invoke(d, New Object() {[text]})
                Else
                    TextBox1.Text += [text]
                End If
            Catch ex As Exception
                MessageBox.Show("讀取錯誤:" + ex.Message, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
        End Sub

        Private Sub GetAD(ByVal zgc As ZedGraphControl)
            Dim x As Double, y As Double
            Dim list = New PointPairList()
            For x = 0 To 100
                y = CDbl(strArray(CInt(x)))
                list.Add(x, y)
            Next x
            If myPane.CurveList.Count > 0 Then
                myPane.CurveList.Clear()
            End If
            myCurve = myPane.AddCurve("", list, Color.Blue, SymbolType.None)
            myCurve.Symbol.Fill = New Fill(Color.White)
            zgc.AxisChange()
        End Sub
    2009年6月22日 上午 10:06

解答

所有回覆