伊莉討論區

標題: 分離奇數項與偶數項字串 [打印本頁]

作者: vx96825k    時間: 2013-2-19 02:31 AM     標題: 分離奇數項與偶數項字串

大家好,我想以","作為分離字元,把奇數項與偶數項字串分離並放置於兩個box
字串如  [ ,0000,000A,0000,000A,0000,000A,0000,000A ]

使用以下程式會"出現索引在陣列的界限之外"的錯誤訊息

For j = dataSep.GetLowerBound(0) To dataSep.GetUpperBound(0)
rxdmix.Text &= dataSep(j)
rxdcounter.Text &= dataSep(2 * j)
rxdvolt.Text &= dataSep((2 * j) + 1)
Next               

想請問各位高手要如何成功分離奇數項與偶數項字串,希望有高手可以幫忙解惑
謝謝!


以下是VB2010的程式碼
'**************************************************************
    '計時器控制項的Timer事件
    '將接收的動作放於其中,只要有資料就會被接收進來
'**************************************************************
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim instring As String,dataStr As String,Sep(0) As Char
        Dim dataSep() As String, i As Integer, j As Integer

        instring = ""
        Try
            RS232.ReadTimeout = 1000
            instring = RS232.ReadExisting()
            If instring.Length = 0 Then
                Exit Sub
            
         Else
                rxdmix.Text += instring
                i = rxdmix.TextLength
                Textlength.Text = i
                dataStr = rxdmix.Text
                Sep(0) = "," '訂定分離字元陣列
                dataSep = dataStr.Split(Sep) '將字串依分離字元分離
                rxdmix.Text = ""

                For j = dataSep.GetLowerBound(0) To dataSep.GetUpperBound(0)
                    rxdmix.Text &= dataSep(j)
                    rxdcounter.Text &= dataSep(2 * j)
                    rxdvolt.Text &= dataSep((2 * j) + 1)
                Next

            End If
        Catch ex As Exception
            MessageBox.Show("讀取錯誤:" + ex.ToString, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
End Sub



作者: gpgpi    時間: 2013-2-20 12:07 AM

我有個疑問~你的字串是含逗號的字串,還是已經是陣列?

還有你開頭是一個 " , " ...在這逗號前面是一個空白還是沒東西?

以上兩點還滿重要的...可以的話請回答一下

因為我不知道以上兩點你實際情況是怎樣,所以我就照我理解的方式做

簡單做給你看~我就不放程式給你抓了
表單上放一個Button、一個TextBox、兩個ListBox

以下為程式碼

  1. Public Class Form1
  2.     Dim txt As String = ",0000,000A,0000,000A,0000,000A,0000,000A"
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  4.         TextBox1.Text = txt

  5.         Dim splittxt() = txt.Split(",")

  6.         For i = splittxt.GetLowerBound(0) To splittxt.GetUpperBound(0) Step 2
  7.             ListBox1.Items.Add(splittxt(i))
  8.         Next

  9.         For j = splittxt.GetLowerBound(0) + 1 To splittxt.GetUpperBound(0) Step 2

  10.             ListBox2.Items.Add(splittxt(j))
  11.         Next

  12.     End Sub
  13. End Class
複製代碼


執行結果

i為奇數項(顯示在左邊),j為偶數項(顯示在右邊)

[attach]87632420[/attach]


以上應該不難懂,理解完再改進去自己的程式吧




作者: vx96825k    時間: 2013-2-20 01:20 AM

是含逗號的字串,然後逗號是第一個字元前面沒有資料
我的資料是透過單晶片然後用RS232收進PC端,所以我列的字串只是其中的極小段
因為資料量很大所以我是用Timer收資料進 rxdmix.Text這個textbox
之後再用 Split 把 string 變成陣列
再來取得陣列的上下限後用For迴圈將字串分離
  1. For j = dataSep.GetLowerBound(0) To dataSep.GetUpperBound(0)
  2. rxdmix.Text &= dataSep(j)
  3. rxdcounter.Text &= dataSep(2 * j)
  4. rxdvolt.Text &= dataSep((2 * j) + 1)
  5. Next               
複製代碼
在索引的數值不超過 j 值前都可以成功分離,但是超過後就會亂抓資料
然後就會顯示"出現索引在陣列的界限之外"的錯誤訊息
以上就是我的實際狀況與問題了

謝謝gpgpi的回答
雖然不知道我改不改的成功

anyway 謝謝你!
作者: vx96825k    時間: 2013-2-20 01:42 AM

以下是完整的程式碼
  1. Imports System.IO.Ports
  2. Imports System.Text

  3. Public Class Form1
  4.     '**************************************************************
  5.     '表單的Load事件中先將所有的通訊埠先列出來
  6.     '將通訊埠排序,並將第一個通訊埠設為預設值
  7.     '**************************************************************

  8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.         For Each sp As String In SerialPort.GetPortNames()
  10.             cmb1.Items.Add(sp)
  11.         Next
  12.         cmb1.Sorted = True
  13.         cmb1.SelectedIndex = 0
  14.     End Sub

  15.     '**************************************************************
  16.     '『開啟通訊埠』按鈕的Click事件
  17.     '此事件將設定通訊埠參數,並開啟通訊埠
  18.     '**************************************************************
  19.     Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
  20.         Dim EncodeType As Encoding
  21.         RS232.PortName = cmb1.SelectedItem.ToString
  22.         RS232.BaudRate = 2400
  23.         RS232.Parity = Parity.None
  24.         RS232.DataBits = 8
  25.         RS232.StopBits = StopBits.One

  26.         '檢查編碼方式
  27.         If rbtnCode1.Checked Then EncodeType = Encoding.UTF8
  28.         If rbtnCode2.Checked Then EncodeType = Encoding.UTF7
  29.         If rbtnCode3.Checked Then EncodeType = Encoding.ASCII
  30.         If rbtnCode4.Checked Then EncodeType = Encoding.Unicode

  31.         If Not RS232.IsOpen Then
  32.             RS232.Open()
  33.             Timer1.Interval = 100
  34.             Timer1.Enabled = True
  35.         Else
  36.             MsgBox("通訊埠開啟錯誤(通訊埠已被開啟)", MsgBoxStyle.Critical Or MsgBoxStyle.OkCancel)
  37.             End
  38.         End If
  39.     End Sub

  40.     '**************************************************************
  41.     '『關閉通訊埠』按鈕的Click事件
  42.     '以Close方法關閉通訊埠,並釋放物件所佔用的資源
  43.     '**************************************************************
  44.     Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
  45.         If RS232 Is Nothing OrElse Not RS232.IsOpen Then
  46.             MsgBox("通訊埠尚未開啟", MsgBoxStyle.Critical Or MsgBoxStyle.OkCancel)
  47.         Else
  48.             Timer1.Enabled = False
  49.             RS232.Close()
  50.         End If
  51.     End Sub

  52.     '**************************************************************
  53.     '『結束程式』按鈕 的Click事件
  54.     '此事件將設定通訊埠參數,並開啟通訊埠
  55.     '**************************************************************
  56.     Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
  57.         If Not RS232 Is Nothing Then
  58.             If RS232.IsOpen Then RS232.Close()
  59.         End If
  60.         End
  61.     End Sub

  62.     '**************************************************************
  63.     '計時器控制項的Timer事件
  64.     '將接收的動作放於其中,只要有資料就會被接收進來
  65.     '**************************************************************
  66.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  67.         Dim instring As String
  68.         Dim dataStr As String
  69.         Dim Sep(0) As Char
  70.         Dim dataSep() As String, i As Integer, j As Integer
  71.         'Dim x, x1

  72.         instring = ""
  73.         Try
  74.             RS232.ReadTimeout = 1000
  75.             instring = RS232.ReadExisting()
  76.             If instring.Length = 0 Then
  77.                 Exit Sub
  78.             Else
  79.                 rxdmix_16.Text += instring
  80.                 i = rxdmix_16.TextLength
  81.                 Textlength.Text = i
  82.                 dataStr = rxdmix_16.Text
  83.                 Sep(0) = "," '訂定分離字元陣列
  84.                 dataSep = dataStr.Split(Sep) '將字串依分離字元分離
  85.                 rxdmix_16.Text = ""

  86.                 For j = dataSep.GetLowerBound(0) To dataSep.GetUpperBound(0)

  87.                     rxdmix_16.Text &= dataSep(j)
  88.                     rxdcounter.Text &= dataSep(2 * j)
  89.                     rxdvolt.Text &= dataSep((2 * j) + 1)

  90.                     'Dim x, x1
  91.                     'x = "&H" + dataSep(j)
  92.                     'x1 = CLng(x)
  93.                     'rxdmix_10.Text = x1

  94.                 Next

  95.             End If
  96.         Catch ex As Exception
  97.             MessageBox.Show("讀取錯誤:" + ex.ToString, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  98.         End Try
  99.     End Sub


  100.     '**************************************************************
  101.     '通訊埠的KeyPress事件,在此不讓使用者輸入資料
  102.     '**************************************************************
  103.     Private Sub txd1_keypress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txd1.KeyPress
  104.         If e.KeyChar = vbCr Then
  105.             If Not RS232 Is Nothing Then
  106.                 RS232.Write(txd1.Text)
  107.             End If
  108.         End If
  109.     End Sub

  110.     '**************************************************************
  111.     '傳送文字框的KeyPress事件,當檢測到Enter按鍵時
  112.     '將所有的資料送出
  113.     '**************************************************************
  114.     Private Sub cmb1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmb1.KeyPress
  115.         e.KeyChar = ChrW(0)
  116.     End Sub

  117. End Class
複製代碼
以下是我的介面及錯誤通知
[attach]87635112[/attach]
[attach]87635132[/attach]
紅色框框裡的就是亂抓的資料




歡迎光臨 伊莉討論區 (http://a401.file-static.com/) Powered by Discuz!