Barcodatron – A barcode reader / label generation environment

barcodatron_screengrab

IMG_7117

barcodatron is a system for reading and generating barcodes using common low-cost scanners and printers. I use it for tracking electronic components for assembly. It certainly has a wide variety of other applications, so I thought I would open-source it. It consists of python scripts which can operate stand-alone or be called as functions. They are called from the command line or, from a web interface. The web interface is implemented using Flask web server framework which is written in python.  A low-cost hand-held scanner is used to read barcodes from labels, reports, and cheat-sheets. The web interface can be used to look-up parts in an external database via RESTful http GET calls. Printed barcodes are generated using the reportlab python library which generates pdfs. The system also recognizes barcodes to generate labels which are sent to Zebra  printers, which are commonly used for shipping labels. The current environment runs under Windows XP, however it could easily be ported to IOS or Linux. The Flask framework is very lightweight and would run very well on a raspberry pi or beaglebone black

barcodedfeeders

Architecture

The following diagram describes the overall architecture

barcodatron_architecture

A web server running locally delivers .html, js, and css to render the UI. When a barcode is scanned, it will parse the request and send it to the local server which will either forward the request to an external server if it is a database look-up, or execute locally if it is a command to generate a label on the printer.

PREREQUISITES

To run barcodatron you will need the following installed on your system:

Python 2.7

Available from http://www.python.org/

Git for windows

Available from http://msysgit.github.io/ You could skip it and just use the windows command prompt, but my instructions assume you are using the git bash command prompt.

Once you’ve installed git, go to into windows->control panel->system->Advanced->Environment ->Variables and add C:\python27\;C:\python27\scripts; to your $PATH

Python’s setuptools.py

Again, not strictly necessary, but the easy_install script makes it much easier to install python libraries

From here on out the instructions assume you’ve started git bash and are entering from the command line

curl https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py > ez_setup.py; 
python ez_setup.py

Flask & Flask Bootstrap

Available from http://flask.pocoo.org/ 

easy_install flask
easy_install flask-bootstrap

Reportlab

Reportlab is a huge magical blob of python which lets you generate postscript (.ps,.pdf) and actually works most of the time. Documentation: http://www.reportlab.com/software/opensource/

easy_install reportlab

Win32 API

Win32api allows much better access the windows API from python. We use it to call acrobat to generate the pdfs

http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/pywin32-218.win32-py2.7.exe/download

Requests

Requests is an http protocol toolkit for python

easy_install requests

BARCODATRON

Check it out from github

git clone https://github.com/cogwheelcircuitworks/barcodatron.git

Try it out

cd barcodatron
python http_barcodes.py

At this point you should be able to navigate to http://localhost/ and see the UI:

barcodatron

Print out barcode_list.pdf It contains a sheet of sample barcodes and commands. If it doesn’t exists, you can generate another one with python barcode_list.py You can look at the source code to get an idea of how to use reportlab to generate reports and cheat-sheets with barcodes on them. The system works with triplets of information; Two nouns – A Job number and a Part number, and a Verb – A command to generate a certain kind of label. Scan the sample Job Number, then the Sample Part Number from the barcode list, then scan a command to preview a label. At this point acrobat will start and show you an image of the label.

External Database Access

The script sb_aj_query.py is in charge of making queries to the external DB, but in this example, it is stubbed out and responds locally to the sample part queries that are in barcode_list.pdf At the top of the script you will see where to add a URL and key to make calls to an external web server. In my case, it is a wordpress plugin which makes use of the wordpress ajax api

#----------------  
# server constants
server_ajax_url = 'http://www.YOUR_SERVER.com/wp-admin/admin-ajax.php' # server's ajax api
server_auth_key = 'fake_query' #  replace w/an auth key your server checks for

kitlabels.py

This is the work-horse script which generates the pdf for the barcode labels. It may be invoked from the command line. Use python kitlabels.py -h to get list of commands:

python kitlabels.py -h

OPTIONS:

    -t [ 0 = no test, 1 = test 1, 2 = test 2 ]
    -v [ 0 = no verbose, 1 = verbose ]
    -s [ 0 = 8.5 x 11 paper, 1 = 4 x 6 labels, 2 = 2.25 x 1.25 labels 3 = 3.5 x 1 ]
    -p [ <printer name>, acrobat = send to previewer ]

EXAMPLES:

  Print 2 sheets of test labels on 8.5 x 11 paper to the acrobat reader:

     python kitlabels.py -s 0 -t 1 -p acrobat -v 1

  Print 1 4x6 test label:

     python kitlabels.py -s 1 -t 1 -p acrobat -v 1

  Print 1 2.25 x 1.25 test label:

     python kitlabels.py -s 2 -t 1 -p acrobat -v 1

NOTES:

  Takes input on stdin of the format:

  First token is one of [logo,barcode,keyval]

  LOGO w/no arg renders logo512x512.png
  LOGO C renders the a very large character C in its place
  BARCODE f1 f2 renders a barcode consisting of f1:f2, then renders 'f1:f2' underneath the barcode in plain text
  KEYVAL f1 f2 f3 fn renders 'f1:' in bold, followed by 'f2 f3 fn', wrapping text lines if necessary

  See tgetl.testlines in source code for example input