-2

I am working with a scale software using COM Port . i found COM port but the data is not coming from machine . help me to get data from machine. Note: Machine is connected to computer through COM port

startIndex cannot be larger than length of string

Please read comments which I think they can help you. This is the code:

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
Kristján
  • 18,165
  • 5
  • 50
  • 62
  • 1
    What else did you expect to happen? Please come back when you've read about variable assignment. – AStopher Nov 16 '15 at 22:20
  • 1
    Like @bob said, what *did* you expect that line to do? If you told us that, we might be able to answer your question properly. – Ilmari Karonen Nov 16 '15 at 22:58

1 Answers1

-2

TxtWeight is "" (so, nil) because it doesn't exist yet- all the declaration does is reserve the space in the memory but doesn't assign anything.

I'm unsure what you're trying to do here, and I suspect there's more to the code that you haven't posted (so please post it and I'll update my answer).

Basically what you're doing is making TxtWeight equal nothing, and then expecting to assign Me.TxtWeight.Text.Substring(2, 6) (which fails because there's nothing there).

You need to assign something to it before doing that, or alternatively formulate a Try..Catch block (see my answer linked here) to catch the error so your program doesn't crash.

Community
  • 1
  • 1
AStopher
  • 4,207
  • 11
  • 50
  • 75