2

I have a bar-code scanner with USB interface, so it appears as an HID.

There are reasons why I must breakpoint on the first character.

That brings my Delphi IDE to the front, it stops on the breakpoint and the rest of the bar-code is injected into my code as if it had been typed front the keyboard (which, in a way it has, as the USB scanner is just another HID).

Is there any way to avoid this? Or to add an initial check as to the source of the input?

It annoys me so much that I am now looking for a scanner with an RS232 interface.

Mawg says reinstate Monica
  • 34,839
  • 92
  • 281
  • 509
  • 1
    Perhaps you could create some testing barcodes with only the one character at a time? – Charles Mar 30 '12 at 02:47
  • +1 not a perfect solution, but it might make life easier. Thanks. – Mawg says reinstate Monica Mar 30 '12 at 03:35
  • 2
    Can't you intercept the windows messages (wm_char ?), put them in an array of some kind and set the msg to handled and then feed them through code to your input? – Pieter B Mar 30 '12 at 07:43
  • 1 Thanks for the suggestion. I don't know about WM_CHAR, (sorry, such a n00b), but will go Google. For some reason I thought it best just to hook the keyboard, but can't remember where I go that from. Maybe your suggestion is better. Let me check & get back to you – Mawg says reinstate Monica Mar 30 '12 at 08:27
  • Do you have a URL for an example? – Mawg says reinstate Monica Mar 30 '12 at 08:31
  • 1
    Many modern scanners that are USB have a CDC/serial mode, that is definitely better for debug purposes. Personally I would just refactor my code (I'm sure you can do it!) and not obsess over where your breakpoints go. Use two layers, one that captures all the data, and waits for an enter key, and when the last key comes in, then start your processing. – Warren P Mar 30 '12 at 13:30
  • 1
    You can head to this interesting thread "[USB Barcode Reader and KeyPress Options](http://groups.google.com/group/borland.public.delphi.language.delphi.general/browse_thread/thread/a9c37af9d4630ed7/1af243b0218ed325?q=barcode+reader)". – menjaraz Mar 31 '12 at 09:09

1 Answers1

1

The problem is that HID "devices" send information back to the computer in packets known as "reports".
In the case of a mag-swipe, or bar-code scanner, the "report" contains the entire number.

Which is to say, you don't receive reports character-by-character, but the entire string at once. (In the case of a mag-swipe, you will receive all two, or three, tracks in the same report).

So your code doesn't break "on the first character", it breaks "on the entire report". The fact that the remaining characters are there you can think of as a bonus (if you don't want to look at them you don't have to). But you won't be receiving any more "reports" for subsequent characters.

i cannot imagine any reason why you wouldn't want to know the bar-code at once. But most mag-swipe and bar-code scanners can be configured to emulate a keyboard, rather than a generic HID device. In that case you will receive multiple WM_KEYDOWN messages.

Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125