none
{"索引超出範圍。必須為非負數且小於集合的大小。" & vbCrLf & "參數名稱: startIndex"} 請問這是甚麼錯誤 RRS feed

  • 問題

  • 
    Private Sub GPOWWWW(ByVal DATA As String) 'As String 
    
            Dim ID As String = "3831"
            Dim Address As String = "30303036"
            'Dim Datain As String = "3030"
            Dim R As String = "30"
            Dim W As String = "31"
            Dim ASCIIO As String = "4F"
            Dim ASCIIK As String = "4B"
            Dim ASCIICR As String = "0D"
    
            Dim CID1 As Integer = Convert.ToInt32("38", 16)
            Dim CID2 As Integer = Convert.ToInt32("31", 16)
            Dim CAddress1 As Integer = Convert.ToInt32("30", 16)
            Dim CAddress2 As Integer = Convert.ToInt32("30", 16)
            Dim CAddress3 As Integer = Convert.ToInt32("30", 16)
            Dim CAddress4 As Integer = Convert.ToInt32("36", 16)
    
            Dim CData1 As Integer = Convert.ToInt32(DATA.Substring(0, 0), 16)
            Dim CData2 As Integer = Convert.ToInt32(DATA.Substring(1, 1), 16)
    
            Dim CR As Integer = Convert.ToInt32("30", 16)
            Dim CW As Integer = Convert.ToInt32("31", 16)
    
            Dim CRC1 As String = Hex(CID1 + CID2 + CAddress1 + CAddress2 + CAddress3 + CAddress4 + CData1 + CData2 + CW).ToString.Substring(1, 2)
            Dim CRC2 As String = AsciiStringToHexString(CRC1)
            Dim indata As String = ID & Address & DATA & W & CRC2 & ASCIIO & ASCIIK & ASCIICR
    
    
            Dim bDataOut() As Byte
            Dim iSentCount As Integer
            Dim Encode1 As Encoding = Encoding.Unicode '採用Unicode編碼
    
    
            Try
    
                Dim Buf1(Convert.ToInt32(indata.Length / 2 - 1)) As Byte
    
                For i As Integer = 0 To Buf1.Length - 1
                    Buf1(i) = Convert.ToByte(indata.Substring(i * 2, 2), 16)
                Next
    
                bDataOut = Buf1 '將字串轉換為位元組陣列
                iSentCount = bDataOut.GetLength(0) '傳送總位元組數
                '顯示出總位元組數
                'lblSentCount.Text = "總傳輸量:" & iSentCount.ToString & "位元組"
                RS232.Write(bDataOut, 0, iSentCount)
    
                TimeDelay(200)
    
                Dim InByte() As Byte, ReadCount As Integer
    
                If RS232.BytesToRead <= 0 Then Exit Sub
                ReDim InByte(RS232.BytesToRead - 1)
                ReadCount = RS232.Read(InByte, 0, RS232.BytesToRead)
                If ReadCount = 0 Then  '若無資料則跳出
                    Exit Sub
                Else
                    MsgBox(System.Text.Encoding.ASCII.GetString(InByte))
                    txtReceive.Text = AsciiStringToHexString(System.Text.Encoding.ASCII.GetString(InByte))
                End If
    
            Catch ex As Exception
                MessageBox.Show("輸入數值錯誤:" + ex.ToString, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
    
        End Sub

    請問各位大哥 小弟是新手 但是我不知道這是怎樣的錯誤

    2018年1月17日 上午 02:22

解答

  • Dim x As Integer = Convert.ToInt32("38", 16)

    問題應該不是出在這行吧, 這行看起來沒有錯啊.

    另外,

    Dim CData1 As Integer = Convert.ToInt32(DATA.Substring(0, 0), 16)
    Dim CData2 As Integer = Convert.ToInt32(DATA.Substring(1, 1), 16)
    
    

    (1) Substring 的第一個參數是起始位置, 這代表如果你傳進來的 DATA 沒有兩個字元以上  在 DATA.Substring.(1,1) 會出錯

    (2) Substring 的第二參數是要截斷的長度, 你的第一個 Data.Substring(0,0) 等於是沒有任何一個字元。



    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    2018年1月17日 上午 02:50
    版主