Reputation: 15
My problem is then I read first time BAR Code with BarCode Scanner ds457 I get full text for e.g. 02405529, but if i read code again i get only last character 9. If i read other code for e.g. 02405530 ill get this code last char 0. I need to close my program, to read full text again. DC Button from my program dosent help as Clear Listbox too. Any idea?
Public Class frmMain
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 100
'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
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Handshake = IO.Ports.Handshake.XOnXOff
SerialPort1.RtsEnable = True
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
btnConnect.Enabled = False 'Disable Connect button
btnDisconnect.Enabled = True 'and Enable Disconnect button
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'Close our Serial Port
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtTransmit.Text & vbCr)
'The text contained in the txtText will be sent to the serial port as ascii
'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
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.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
' If [text] <> Me.rtbReceived.Text Then
Me.rtbReceived.Text = [text]
' Me.rtbReceived.Text &= [text]
'End If
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
rtbReceived.Text = " "
End Sub
Private Sub txtTransmit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTransmit.TextChanged
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub
Private Sub Isvalyti_Click(sender As Object, e As EventArgs) Handles Isvalyti.Click
rtbReceived.Clear()
End Sub
End Class
Upvotes: 1
Views: 1023
Reputation: 15
Thank you Hans Passant and DMAN for your help. Both answers helped me a lot. Also i found a bug with scanner and my code. Scanner cant scan to quickly so i added threading.Thread.Sleep(100), now scanner take full part of code, not one piece.
Upvotes: 0
Reputation: 941545
ReceivedText(SerialPort1.ReadExisting())
Serial ports are very slow, your ReadExisting() call normally only ever retrieves one or two characters. Your ReceivedText() method does not append the text so you only see those few characters.
You need to configure your barcode scanner to send a special character that indicates the end of the string. If it doesn't already do that, most do. Set the SerialPort.NewLine property to that special character, now you can use SerialPort.ReadLine() instead and be sure you get the entire scanner response.
Upvotes: 1