Page 1 of 1

How to get digits from barcode reader?

Posted: Thu Feb 01, 2024 5:37 pm
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


Re: How to get digits from barcode reader?

Posted: Thu Feb 01, 2024 6:35 pm
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.

Re: How to get digits from barcode reader?

Posted: Thu Feb 01, 2024 7:01 pm
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.

Re: How to get digits from barcode reader?

Posted: Thu Feb 01, 2024 10:23 pm
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

Re: How to get digits from barcode reader?

Posted: Fri Feb 02, 2024 10:23 am
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








Re: How to get digits from barcode reader?

Posted: Fri Feb 02, 2024 1:44 pm
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

Re: How to get digits from barcode reader?

Posted: Fri Feb 02, 2024 2:28 pm
by dige
Thx 👍