Load G code file with a Barcode Scanner

More
22 Apr 2016 11:47 - 24 Apr 2016 16:16 #73711 by BigJohnT
I started a new topic for discussion and comments.

I''ve been sent a 2D barcode scanner and I'm recording the process of getting a G code file loaded into Axis when scanned.

The simple way is to click on the file open icon and scan the bar code. However I'd like to do something more automatic.

First you need the Vendor ID and Device ID in hex. To get that plug in the scanner and in a terminal run lsusb. Unplug the scanner and run lsusb again and note which one is not there. That is the scanner. The scanner I have returned this. The Vendor ID is 0c2e and the Device ID is 0901 and in Python hex they would be 0x0c2e and 0x0901. The Python library to read a scanner needs to know this.
Bus 002 Device 007: ID 0c2e:0901 Metrologic Instruments

There is a Python library called pyusb that can read a usb device however in the linux version hell way you need version 1.x and it is not included in LinuxMint 17.3 so you have to get it from a github account.
sudo apt-get install python libusb-1.0-0-dev
git clone https://github.com/walac/pyusb
cd pyusb && sudo python setup.py install

To get a list of the USB devices connected and make sure you have pyusb installed properly we run the following Python program.
#!/usr/bin/python
import usb
busses = usb.busses()
for bus in busses:
	devices = bus.devices
	for dev in devices:
		print "Device:", dev.filename
		print "  idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor)
		print "  idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct)

And the output is:
Device: 
  idVendor: 3118 (0x0c2e)
  idProduct: 2305 (0x0901)
...
JT
Last edit: 24 Apr 2016 16:16 by BigJohnT.
The topic has been locked.
More
22 Apr 2016 12:37 - 22 Apr 2016 12:50 #73713 by BigJohnT
Using the example program that is in usr/share/docs/python-usb/examples/usbenum.py after you install pyusb and I get the following output for the scanner. So making progress... pyusb has no docs so it is slow going.
Device: 
  Device class: 0
  Device sub class: 0
  Device protocol: 0
  Max packet size: 64
  idVendor: 3118 (0x0c2e)
  idProduct: 2305 (0x0901)
  Device Version: 00.07
  Configuration: 1
    Total length: 73
    selfPowered: 0
    remoteWakeup: 1
    maxPower: 400
    Interface: 0
    Alternate Setting: 0
      Interface class: 3
      Interface sub class: 1
      Interface protocol: 1
      Endpoint: 0x84
        Type: 3
        Max packet size: 64
        Interval: 8
      Endpoint: 0x5
        Type: 3
        Max packet size: 64
        Interval: 8
    Alternate Setting: 0
      Interface class: 3
      Interface sub class: 0
      Interface protocol: 0
      Endpoint: 0x82
        Type: 3
        Max packet size: 64
        Interval: 8
      Endpoint: 0x7
        Type: 3
        Max packet size: 64
        Interval: 1

A side note here copying and pasting the file did not work for me, I had to copy the contents to a new file. To make life easy LinuxMint has the wisdom to make a ~bin directory part of the search path so if you add it and put your programs in there they just run with none of the silly ./myprogram.py stuff.

JT
Last edit: 22 Apr 2016 12:50 by BigJohnT.
The topic has been locked.
More
23 Apr 2016 11:27 - 23 Apr 2016 11:30 #73743 by BigJohnT
Thanks to jepler I know how to access the built in Python docs now.

Languages like Python include "documentation" in line with program source. These are called "docstrings" For python modules that can be 'imported, this documentation can be viewed in formatted form with pydoc but for main programs, I don't think this works in the general case for instance, the documentation of the 'os' module, /usr/lib/python2.7/os.py, can be displayed by 'pydoc os'


To find the docs for the main program type in.
pydoc usb

And the result is:
Help on package usb:

NAME
    usb - PyUSB - Easy USB access in Python

FILE
    /usr/local/lib/python2.7/dist-packages/usb/__init__.py

DESCRIPTION
    This package exports the following modules and subpackages:
    
        core - the main USB implementation
        legacy - the compatibility layer with 0.x version
        backend - the support for backend implementations.
        control - USB standard control requests.
        libloader - helper module for backend library loading.
    
    Since version 1.0, main PyUSB implementation lives in the 'usb.core'
    module. New applications are encouraged to use it.

PACKAGE CONTENTS
    _debug
    _interop
    _lookup
    _objfinalizer
    backend (package)
    control
    core
    legacy
    libloader
    util

DATA
    __all__ = ['legacy', 'control', 'core', 'backend', 'util', 'libloader'...
    __author__ = 'Wander Lairson Costa'
    __version__ = '1.0.0rc1'

VERSION
    1.0.0rc1

AUTHOR
    Wander Lairson Costa

(END)

You can also send the output of pydoc to a text file for easier searching like this.
pydoc usb > usb.txt

JT
Last edit: 23 Apr 2016 11:30 by BigJohnT.
The topic has been locked.
More
23 Apr 2016 12:22 #73747 by BigJohnT
And for those following along I finally found the documentation for pyusb.

libusb.sourceforge.net/api-1.0/index.html

JT
The following user(s) said Thank You: eFalegname
The topic has been locked.
More
23 Apr 2016 13:19 #73753 by BigJohnT
I've made another step in the process. Seems you need to set a udev rule and be a member of the group to get in touch with the scanner.

sudo gedit /etc/udev/rules.d/40-scanner.rules
then add the following lines with vendor and product id's to match your scanner.
# USB Barcode Scanner ID 0c2e:0901 Metrologic Instruments
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0c2e", ATTRS{idProduct}=="0901", ENV{libsane_matched}="yes", GROUP="scanner"
Now restart udev with
sudo udevadm trigger

Open up users and groups and add yourself to the scanner group. Now we can talk to the scanner.
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import usb.core
>>> dev = usb.core.find(idVendor=0x0c2e, idProduct=0x0901)
>>> dev.is_kernel_driver_active(0)
True
>>> 

Getting close now...

JT
The topic has been locked.
More
27 Apr 2016 12:27 #73949 by BigJohnT
It was suggested that using pyusb was overkill and the suggestion to use pyserial. First you have to set up the scanner to be a USB serial device. On page 32 of the manual scan the USB Serial bar code. I have a 2-D bar code for "otest.ngc" to practice scanning with.
john@cave-mint ~ $ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial('/dev/ttyACM0')
>>> print(ser.name)
/dev/ttyACM0
>>> print(ser.baudrate)
9600
>>> ser.isOpen()
True
>>> ser
Serial<id=0xb71d2e8c, open=True>(port='/dev/ttyACM0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
>>> ser.flushOutput()
>>> ser.timeout = 1
# scan the bar code
>>> ser.read(12)[3:]
'otest.ngc'

That looks a lot easier to do than all that other stuff.

JT
The topic has been locked.
Time to create page: 0.126 seconds
Powered by Kunena Forum