Main Site Documentation

UsbHost events are too slow for a 2D barcodescanner


#1

Hello,
i have the problem that my spider can’t get all chars from my connected barcodescanner. Is there a way that the events are triggered faster?

My source:

public void ProgramStarted()
        {

            USBHostController.DeviceConnectedEvent += UsbHostControllerOnDeviceConnectedEvent;
            USBHostController.DeviceDisconnectedEvent += USBHostController_DeviceDisconnectedEvent;
                       
            _tickTimer.Tick += timer_Tick;
            _tickTimer.Start();
            
        }

        void timer_Tick(GT.Timer timer)
        {
            string output = "";
            if (CharQueue.Count > 0)
            {
                output += (string)CharQueue.Dequeue();
            }
            PrintMessage(output);
        }

        static void USBHostController_DeviceDisconnectedEvent(USBH_Device device)
        {
            PrintMessage("Device diconnected event!");
        }

        static void UsbHostControllerOnDeviceConnectedEvent(USBH_Device device)
        {
            PrintMessage("DeviceType: " + device.TYPE.ToString());

            if (device.TYPE == USBH_DeviceType.Keyboard)
            {
                Debug.Print("Keyboard Connected");
                _kb = new USBH_Keyboard(device);
                _kb.InternalThreadPriority = ThreadPriority.Highest;
                _kb.CharDown += CharUp;
            }
        }

        private static void PrintMessage(string message)
        {
            Lcd.Clear();
            Lcd.DrawText("QR-Code Reader", Font, Colors.Purple, 10, 10);
            Lcd.DrawText(message, Font, Colors.White, 10, 20);
            Lcd.Flush();
        }

       static void CharUp(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
       {
           var typedChar = args.KeyAscii.ToString();

           CharQueue.Enqueue(typedChar);

            // Display the pressed key
            Debug.Print(args.KeyAscii.ToString());
        }

Thanks
Bruno


#2

Have you tried in Debugger or also without debugger (fresh reboot)?
For me some things work extremly slow in debugger.


#3

Try without Debug.Print calls.


#4

There is no code shown for the character queue, so we can only guess what it does.

Upon a timer event, the contents of the queue are displayed on the LCD. This
assumes that the contents of an entire message are present in the queue, which might not be true.


#5

@ Bst - Not sure if this is related but I like to mention that someone had problem with USB mouse not receiving all events when moved quickly. Maybe we are not pooling the USB bus fast enough or the device is not able to keep up. We will investigate this some more.

If you have urgent needs then let us know as this is not high priority otherwise,

Welcome to the community.


#6

I think I was the one talking about the mouse events, it seems some mice needed a huge scale (20) while other only needed a small one (2-3). Once I got that sorted I think the events came through just fine.


#7

Thanks for your responses. I tryed different things. Without debug, without print calls etc. I set the transmission speed (intercharacter delay) on the barcodescanner to slow. In this case the USB Host poll rate is ~255 ms. Result: I get from a QR code with 30 characters only about 10-11. Is this poll rate really to fast?


#8

Hi,
I just plugged a USB scanner into my FEZ spider, running a testprogram that displays Mouse or keyboard actions, and it actually displays barcodes as should be…
(nothing modified to the code… )
the Spider seems to pick up the scanner as keyboard and when i bleep a barcode each character is picked u by the USBH_KeyboardEventHandler(keyb_KeyDown) event

As my testcode collects pressed keycodes and displays when RETURN is pressed, the scanner output is nicely displayed on my display, including detected RETURN.

http://www.ghielectronics.com/community/forum/topic?id=11535&page=1#msg117341

regards
Bert


#9

Hi Folks,

Did you solved this issue?
I’m having the exactly the same problem, I tried somethings, like disabling debug, deploying assemblies as Release, running without debugger attached already, none solved, but I still have more to try.

This issue is kind of urgent, for me, i have to deploy the project at the end of October.

Regards,
DeLL


#10

Which barcode scanner are you using? We would need same/similar one to try. Or send us one and we will look into it.


#11

Hey Gus,

Actually, it is also happening if I use a Keyboard.


#12

Always or when you type fast?

Is your app doing anything else beside the keyboard?


#13

The first time I type anything it is doing fine.
But then it gets really slow.

The app is doing other things, it is checking if the reflective sensor are getting anything, each 200ms.
It is also has a TimeoutTimer witch is set 30s, and if during this 30s nothing happens it fire an event, and display a message.

And when I press Enter at the Keyboard it displays the buffer in a DisplayN18.

I’ll try to isolate each of the components today, to check what is causing this behavior.


#14

It would be great if you can try an app with only keyboard and only scanner.


#15

Gus, just tried the app with only the scanner and one display N18.

I passed a barcode throw the scanner many times and it showed me the following result:
"\n8385857817171918585858585858585"
It should have read “7898215151319”

Following is the code, No Debug, I even tried it without the debugger attached.

public partial class Program
    {
        USBH_Keyboard keyboard;
        void ProgramStarted()
        {
            display_N18.SimpleGraphics.BackgroundColor = GT.Color.White;
            USBHostController.DeviceConnectedEvent += USBHostController_DeviceConnectedEvent;
            var devices = USBHostController.GetDevices();
            if (devices.Length > 0)
                InitializeBarcodeReader(devices[0]);
        }

        void USBHostController_DeviceConnectedEvent(GHI.Premium.System.USBH_Device device)
        {
            if (device.TYPE != GHI.Premium.System.USBH_DeviceType.Keyboard)
                return;
            InitializeBarcodeReader(device);
        }

        private void InitializeBarcodeReader(GHI.Premium.System.USBH_Device device)
        {
            keyboard = new USBH_Keyboard(device);
            keyboard.InternalThreadPriority = ThreadPriority.Highest;
            keyboard.KeyDown += keyboard_KeyDown;
        }

        string line;
        void keyboard_KeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            if (args.Key == USBH_Key.Enter)
            {
                display_N18.SimpleGraphics.DisplayText(line, Resources.GetFont(Resources.FontResources.small), GT.Color.Black, 10, 10);
                line = "";
            }
            else
                line += args.KeyAscii;
        }
    }

#16

what about with a keyboard


#17

Just tried, it’s the same.
It seems that doesn’t matter how fast or slow I type, it just miss most of the keys.


#18

we will test tomorrow


#19

You do string manipulation in the keypress handler, can you try with just an array to see if that actually has a material change?


#20

Gus,

It seems to be the USBHostController that is slow.

I tried the code below now, and it seems to be working fine, I’ll do some more testing.

    public partial class Program
    {
        USBH_Keyboard keyboard;
        void ProgramStarted()
        {
            display_N18.SimpleGraphics.BackgroundColor = GT.Color.White;
            usbHost.KeyboardConnected += usbHost_KeyboardConnected;
            
            //USBHostController.DeviceConnectedEvent += USBHostController_DeviceConnectedEvent;
            //var devices = USBHostController.GetDevices();
            //if (devices.Length > 0)
            //    InitializeBarcodeReader(devices[0]);
        }

        void usbHost_KeyboardConnected(UsbHost sender, USBH_Keyboard Keyboard)
        {
            keyboard = Keyboard;
            keyboard.KeyDown += keyboard_KeyDown;
        }

        string line;
        void keyboard_KeyDown(USBH_Keyboard sender, USBH_KeyboardEventArgs args)
        {
            if (args.Key == USBH_Key.Enter)
            {
                display_N18.SimpleGraphics.DisplayText(line, Resources.GetFont(Resources.FontResources.small), GT.Color.Black, 10, 10);
                line = "";
            }
            else
                line += args.KeyAscii;
        }

Does it make any sense? The USBHostController being slow?