Giter VIP home page Giter VIP logo

pyusb-keyboard-alike's Introduction

pyusb-keyboard-alike

Handler for keyboards and keyboard-alike devices like bar code scanners, RFID readers.

You will find some reusable base classes and few example classes handling data reading from USB bar code scanner and RFID reader that emulate keyboards.

Requirements

  • pyusb 1.X (not 0.4)

How to use

The keyboard_alike folder holds a re-usable Reader class. In most case you will be able to use it just by passing correct arguments.

There are device examples:

  • lindy_bar_code_scanner.py - Lindy USB bar code scanner
  • black_rfid_reader.py - USB 125 kHz RFID reader

If you have matching device - connect it and run the code. Under Linux will have to run the code as root/sudo or give your user permission to access given device.

PyQt4 application example

The pyqt_example.py is an example PyQt4 desktop application using the RFID reader. It will add IDs of read tags to the list widget.

The code flow is quite simple:

  • On init we connect to the USB reader and start a thread that will end when the USB device returns a result (or exception)
  • When thread is finished a signal is emitted that will be handled by _receive_data method. It checks if the thread set a return value and if so it will add it to list. On error it will print it and if it's repeating (device gone wild) it will terminate the application. In more production-ready applications the exception control should be more strict (like skipping trash-reads exception, but not exceptions preventing operations on the device).
  • If all is good _receive_data will start the reading thread again allowing the application to work continuously.

Exceptions

Sometimes pyusb/Reader class will throw exceptions, more often at start when it may read some leftover data (will throw "Got X bytes instead of Y"). Some devices support resetting, which seems to prevent any weird reads (Lindy bar code scanner works with reset, while the RFID reader stops working if reset is called).

Lack of permissions will also cause exceptions. The code will have to run as root/sudo or you will have to use udev rules to add permissions for given device.

Sometimes unknown exceptions may show up. In many cases retrying to run the code works. In some other the device must be re-connected. Example:

keyboard_alike.reader.DeviceException: Could not set configuration: [Errno None] Unknown error

Handling new devices

If you want to handle your device then at start some experimenting/debugging is required to get to know the device.

  • Assuming you are using Linux - connect the device and print dmesg (or use lsusb) to get the VENDOR and DEVICE id
  • For the code the vendor and device IDs are 0xTHE_VALUE_YOU_GOT (the 0x at start)
  • Next stage is to check the raw output from the device - a list of digits that actually is a set of smaller lists-chunks that we will have to find
  • Use debug=True and some data_size/chunk_size - it will print the list with raw data and quite likely it will raise an exception that it didn't got data_size of data
  • Look on the list and see how many elements is in one chunk. In every chunk the value must be at the same index, like those are 6 element chunks:
0, 0, 31, 0, 0, 0,
0, 0, 27, 0, 0, 0
  • Some raw data examples can be found in the examples_tests.py
  • Set the correct chunk and data size - now it should work

Pull requests

You got your device up and running? Provide a basic example with a pull request or send me a link to your blog post :)

More on

You can visit my sites for more tutorials and stuff:

Credits

pyusb-keyboard-alike's People

Contributors

riklaunim avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.