startIndex不能大于字符串的长度。参数名称:startIndex vb.net

时间:2015-11-16 10:43:02

标签: vb.net

我正在使用COM端口的缩放软件。我找到了COM端口,但数据不是来自机器。帮我从机器上获取数据。注意:机器通过COM端口连接到计算机

  

startIndex不能大于字符串的长度

请阅读我认为可以帮助您的评论。 这是代码:

Private Sub FrmScalWeight_all_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.TxtPDate.Text = Date.Now.Date '
    'Me.TxtUserID.Text = FrmUser_LogIn.TxtUser_Id.Text
    'TxtVoucherNo_auto.Visible = False
    Call Pending_car()




    Try


        '---Scale
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.

        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        cmbBaud.Items.Add(9600)     'Populate the cmbBaud Combo box to common baud rates used   
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)

        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next

        cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected
        cmbBaud.Text = cmbBaud.Items.Item(0)    'Set cmbBaud text to the first Baud rate on the list

    Catch ex As Exception
        MsgBox("Comport not found.")
    End Try


    btnDisconnect.Visible = False
    btnConnect.Enabled = True


End Sub

Private Sub ReceivedText(ByVal [text] As String)
    'compares the ID of the creating Thread to the ID of the calling Thread
    If Me.TxtWeight.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf ReceivedText)
        Me.Invoke(x, New Object() {(text)})
    Else
        Me.TxtWeight.Text &= [text]

    End If
End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Me.TxtWeight.Text = ""


        Try


            SerialPort2.PortName = cmbPort.Text         'Set SerialPort1 to the selected COM port at startup
            SerialPort2.BaudRate = cmbBaud.Text         'Set Baud rate to the selected value on 

            'Other Serial Port Property
            SerialPort2.Parity = IO.Ports.Parity.None
            SerialPort2.StopBits = IO.Ports.StopBits.One
            SerialPort2.DataBits = 8            'Open our serial port
            SerialPort2.Open()

            btnConnect.Visible = False          'Disable Connect button
            btnDisconnect.Visible = True        'and Enable Disconnect button



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
     Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click

      Me.TxtWeight.Text = Me.TxtWeight.Text.Substring(2, 6) ' error occurs here
        SerialPort2.Close()             'Close our Serial Port
        btnConnect.Visible = True
        btnDisconnect.Visible = False


        '---
        Call Weight()

   End Sub

1 个答案:

答案 0 :(得分:-3)

TxtWeight""(所以,nil)因为它还没有存在 - 所有声明所做的就是保留内存中的空间但不是分配任何东西。

我不确定你在这里做了什么,我怀疑你还没有发布的代码还有更多内容(所以请发布它和我&我#39; ll更新我的回答)。

基本上你正在做的是让TxtWeight一无所有,然后期望分配Me.TxtWeight.Text.Substring(2, 6)(由于那里没有任何东西而失败)。

在执行此操作之前,您需要为其指定一些内容,或alternatively formulate a Try..Catch block (see my answer linked here) 以捕获错误,以便您的程序不会崩溃。