How to get digits from barcode reader?

Just starting out? Need help? Post your questions and find answers here.
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

How to get digits from barcode reader?

Post by dige »

I would like to use a barcode reader. It sends the recognised barcode as a sequence of digits, just like the keyboard.
What is the best way to receive this text with PB?

I don't want to use a StringGadget or EditorGadegt, but rather receive the keyboard input into a buffer and process it as soon as a #CR is recognised.

How would you go about this?

If I use the CanvasGadget, I have to make sure that the canvas has the focus and that is uncertain.

Code: Select all

OpenWindow(0, 0, 0, 400, 400, "BarCode Scanner", #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 400, 400, #PB_Canvas_Keyboard)
SetActiveGadget(0)
Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget
    If EventType() = #PB_EventType_Input
      c = GetGadgetAttribute(0, #PB_Canvas_Input)
      Debug Str(n) + " " + Chr(c) + " " + Str(c)
      n + 1
    EndIf
  EndIf
  
Until Event = #PB_Event_CloseWindow

"Daddy, I'll run faster, then it is not so far..."
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: How to get digits from barcode reader?

Post by spikey »

Can you configure it as a serial port device instead of a keyboard type device? I'd be thinking about receiving input into a thread as a virtual serial port device and 'PostEvent' a notification into the event loop when a scan is complete and the full data available. This way you maintain full control of the scanner input regardless of the state of the UI or other dialogs which might otherwise interfere with the process.
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to get digits from barcode reader?

Post by skywalk »

There should be an api or dll you can poll for the barcode reader input?
How you choose to display in your gui depends on where you put the user focus.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to get digits from barcode reader?

Post by mk-soft »

I set the barcode reader so that it sends a #STX$ first and with AddKeyboardShortcut I intercept it and set the focus to a StringGadget
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: How to get digits from barcode reader?

Post by dige »

Hello folks,

unfortunately, I have an inexpensive scanner, the Netum NT-L8, which can only send data via the keyboard.

However, I have now found a good solution using a keyboard hook.
What do you think? Is this a good solution or could it cause problems?

What I still don't like, for example, is that the keystrokes are not transmitted correctly.
For example, a URL https://www.. is transmitted as HTTPS:77WWW.

What could be the reason for this?

Code: Select all

Global G_BarCode.s

Enumeration #PB_Event_FirstCustomValue
  #Event_NewBarCode
EndEnumeration

Procedure.l KeyboardHook_Callback( nCode, wParam, *p.KBDLLHOOKSTRUCT ) 
  Static n = 0, BarCode.s
  
  If nCode = #HC_ACTION 
    If wParam = #WM_KEYDOWN Or wParam = #WM_SYSKEYDOWN
      
      Select *p\vkCode
        Case 32 To 123  
          n + 1
          BarCode + Chr(*p\vkCode)
          
          If n > 255
            BarCode = ""
            n = 0
          EndIf
          
        Case 13
          G_BarCode = BarCode
          n = 0
          BarCode = ""
          ; Debug G_BarCode
          PostEvent(#Event_NewBarCode)
        
      EndSelect 
    EndIf
  EndIf 
  
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, *p) ; Eingabe zulassen
  ; ProcedureReturn 1 ; Eingabe verhindern
EndProcedure 


Procedure.l SetKeyboardHook ( myKeyboardHook.l, enable.b = #True)
  Protected LibID, Result, FuncId, hInstance
  
  hInstance = GetModuleHandle_(#Null)
  
  LibID = OpenLibrary(#PB_Any, "user32.dll" )
  If LibID
    If enable
      FuncId = GetFunction(LibID, "SetWindowsHookExA")
      If FuncId
        ;Result = SetWindowsHookEx_( #WH_KEYBOARD_LL, myKeyboardHook, hInstance, 0)
        Result = CallFunctionFast( FuncId, #WH_KEYBOARD_LL, myKeyboardHook, hInstance, #Null )
      EndIf
    Else
      If myKeyboardHook
        FuncId = GetFunction(LibID, "UnhookWindowsHookEx")
        If FuncId
          Result = CallFunctionFast (FuncId, myKeyboardHook)
        EndIf
      EndIf
    EndIf
    CloseLibrary(LibID)
  EndIf
  ProcedureReturn Result
EndProcedure


myHook = SetKeyboardHook(@KeyboardHook_Callback(), #True)

OpenWindow(0, 0, 0, 400, 400, "BarCode Scanner", #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 400, 400, #PB_Canvas_Keyboard)
SetActiveGadget(0)

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #Event_NewBarCode
    Debug G_BarCode
;   
;   ElseIf Event = #PB_Event_Gadget
;     If EventType() = #PB_EventType_Input
;       c = GetGadgetAttribute(0, #PB_Canvas_Input)
;       ;Debug Str(n) + " " + Chr(c) + " " + Str(c)
;       n + 1
;     EndIf
;     
    
  EndIf
  
Until Event = #PB_Event_CloseWindow







"Daddy, I'll run faster, then it is not so far..."
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: How to get digits from barcode reader?

Post by HeX0R »

the virtual key codes are no ascii codes, you need additionally MapVirtualKey() to get the correct ones.
See: https://learn.microsoft.com/de-de/windo ... -key-codes
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: How to get digits from barcode reader?

Post by dige »

Thx 👍
"Daddy, I'll run faster, then it is not so far..."
Post Reply