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