Serial Barcode Scanner

I am playing with the serial port example for bar code readers and have come across an odd behavior that I’m not sure how to debug further.

When I run the program on my Mac it runs as expected, but when I run it on a windows 10 machine with the same model of scanner the DataAvailable Event does not always get raised. It seems that it’s almost always when scanning the first barcode after connecting, although this behavior persists for 2 or 3 scans sometimes. Then, it seems to be one read behind. Meaning if I scan a barcode ending in 852, then a barcode ending in 569 it will print the barcode ending in 852 to the text area (assuming the event gets raised on 569). This behavior is not found on my Mac. I’ve tried setting the baud to the rated speed on the fly as well as exchanging lookahead for readall in the DataAvailable event to no avail.

I’m using two Datalogic QD2340 scanners.

Any ideas would be much appreciated.

Can you share the code you have in your dataAvailable event?

Sure! It is the only thing that I have made (slight) modifications to in the example.

Sub DataAvailable() Handles DataAvailable // For this example, we used the Motorola LS2208 bar code scanner. // It can be easily configured to add a carriage return and line feed // to the end of the barcode value. This example then looks in the serial // port buffer to see if those two characters are present and if so // reads the data into the TextArea1 control. //Dim data As String = Me.LookAhead(Encodings.ASCII) Dim data As String = Me.ReadAll(Encodings.ASCII) If InStr(data, Chr(13)) > 0 Or InStr( data, Chr(10)) > 0 Then OutputArea.Text = OutputArea.Text + data End If End Sub

I don’t have time to look at it but you have to loop until you have no more DataAvailable

Off the top of my head:

[code]
dim sData as string
while me.DataAvailable>0
sData = sData + me.ReadAll
me.poll
wend

//now deal with sData[/code]

If that’s not enough to help I’ll dig out what I did in my barcode class.

Had a chance before meeting to open a project that uses a barcode scanner. Almost had it right. I’m using AddHandler to do this all via code but it demonstrates what you should do with all serial comms.

[code]Private Sub Serial_DataAvailable(oInstance as Serial)

dim s as string

While oInstance.BytesAvailable > 0
s = s + oInstance.ReadAll

oInstance.Poll

Wend

SendBarcodeToListeners(s)
End Sub
[/code]

Use lookahead for the if statement part. That keeps the data in the buffer, so if it’s not all data yet, you can skip the event without loosing the buffer.
Then use ReadAll when the expected data is in there or even better use Read to get only the string part from the buffer that you need before the check Chr(13) + Chr(10).

It’s best to keep awat from loops. Just know that the datavailable event is not guaranteed to have all data or even has data. But it can also have more data than expected (for example 2 barcodes in one event)

[quote=416033:@Bob Keeney]Had a chance before meeting to open a project that uses a barcode scanner. Almost had it right. I’m using AddHandler to do this all via code but it demonstrates what you should do with all serial comms.

[code]Private Sub Serial_DataAvailable(oInstance as Serial)

dim s as string

While oInstance.BytesAvailable > 0
s = s + oInstance.ReadAll

oInstance.Poll

Wend

SendBarcodeToListeners(s)
End Sub
[/code][/quote]

I converted the code to include a while loop and when it triggers the while loop works as expected BUT it does not trigger the first (and sometimes second and even third) time the scanner reads a barcode and when it finally does it has the data from the previous scan in Me.Lookahead and Me.ReadAll.

At that point if it’s working on the Mac but not in Windows it’s probably a Windows driver issue. I’ve never used a Datalogic scanner so unsure what to advise for next steps.

Side question: Are you going through a USB to Serial converter or straight USB?

I maybe missing something, but as far as I see, and we’re using one in our project, you could use a “barcode scanner keyboard wedge”. Highly configurable and a huge amounts of codeformats and barcode fonts’.

Mileage seems to vary on this one. We’ve had zero problems with serial bar-code scanners. We’ve had huge issues with barcode scanner configured as keyboards messing around with control focus and user input.

This is straight USB, but the interface is set to simulate RS-232. This seems to be the best mode as far as Xojo even recognizing the device. I will play more with the configuration and look into drivers.

I will look at the keyboard wedge mode.

I’m wondering if this is related to a mismatch in Baud Rate, Parity or some other setting but I’m having a hard time finding the manufacturers reccomendations for those settings.

With our Honeywell VG1450-UG we set it up for “USB Serial”. The documentation notes that in Windows you need to download a driver from the Honeywell website. The driver will use the next available COM Port number. Mac’s recognize the scanner as a USB CDC class device and automatically uses a class driver.

Typically with a USB device you don’t need to worry about baud rates and parity as that’s baked into the USB protocol. So it might be you have a different mode setup than optimal. Our Honeywell must have a dozen different protocols it can handle.

Okay, I got it working. Admittedly, this is probably not the best solution, but I changed the driver from Barcode Scanner to a generic USB Serial device driver in Windows, and it works exactly as expected.

That being said, I was quite naive, and I need to use the scanner for a web app, so I had to convert the scanner to a keyboard device pair it with a KeyDown event and a custom global prefix to filter out user keystrokes.

That makes sense since a WebBrowser doesn’t have access to a USB or Serial Port! Those devices (might) only exist on the server.

So having it act as a keyboard is the only solution that I’m aware of for a web app. Well…welcome to Xojo and hopefully you find it as intriguing as I did nearly 20 years ago.