Issue using serial communications.

D

davem

I have a simple test application that displays barcode data. The barcode
scanner works via serial port communications. I am able to scan about 15 -
20 barcodes successfully then I recieve an incomplete barcode scan (partial
data), then the next scan always return no data. After that I'm able to
scan another 15 - 20 times correctly, then incomplete scan data followed a
scan with no data (and on and on . .).
I've tried clearing the buffer after every DataRecieved event, but this
seems to have no effect. Can anyone suggest what type of issue I'm running
into? This is the code I'm running

Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived

Try

Me.Invoke(New EventHandler(AddressOf AddToList))

Catch ex As Exception

MsgBox("From DataReceived - " & ex.Message)

End Try

End Sub

Private Sub AddToList(ByVal s As Object, ByVal e As EventArgs)

Try

Dim strMsg As String = serialPort.ReadExisting.ToString.Trim

strMsg = Microsoft.VisualBasic.Right(strMsg, Len(strMsg) - 3)

ListBox1.Items.Add(strMsg)

serialPort.DiscardInBuffer()

serialPort.DiscardOutBuffer()

Catch ex As Exception

MsgBox("From DoUpdate - " & ex.Message)

End Try


End Sub



Any suggestions or ideas would be greatly appriciated.



Dave M.
 
G

Ginny Caughey [MVP]

Probably you need to buffer the data you receive on a separate thread. Each
time add the data you've got to what you've received previously. Once you
have an entire record, use it and shorten the buffer accordingly. If you
receive a partial record that is never completed, you'll need to discard
that too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top