Linux Reads Bar Codes

A hardware and device driver project for Linux.
Create the Device Driver

Any text editor will do, but here, of course we use the author's able editor, described in his book Build Your Own LINUX C Toolbox. Call your editor with a command such as able barcode. and type in the text shown in Listing 1.

Listing 1. barcode.h

Do set COM_BASE in this file to the base I/O address of the serial port you will actually be using.

To create the device-driver source file, call your editor with a command such as able bcdrv.c and enter the text shown in Listing 2 (available at

When we compile this code to create the object module that can be loaded into the kernel by insmod, the compiler needs to be told to access include files from /usr/src/linux/include, and we need to define some control flags for the preprocessor. Let us encapsulate these details in a little shell script. Call your editor with a command such as able and type in the text

#! /bin/bash
gcc -D__KERNEL__ -I/usr/src/linux/include -DMODULE\
             -Wall -O2 -c $1.c -o $1.o

Save the file and then mark it executable with the command chmod+x When called from the shell prompt using the command bcdrv this shell script becomes the command:

gcc -D__KERNEL__ -I/usr/src/linux/include -DMODULE\
            -Wall -O2 -c bcdrv.c -o bcdrv.o
This asks gcc to compile the file bcdrv.c and create the object file bcdrv.o containing our loadable kernel device-driver module.

Load the Device Driver

To load the device driver into the kernel, first promote yourself to superuser with the command su and then type in root's password. Then use the command insmod bcdrv or the command insmod bcdrv.o to load the device driver into the kernel. The module loader appears to accept either form.

Type exit to restore normal user status, and return to your own directory. The command cat /proc/modules should now show that your kernel has registered a device driver named ``bcdrv''.

Quick Test

For a quick test of the bar code hardware and software, the little program seen in Listing 3 can be used to display continuously the state of the video signal line. While it is running and displaying whether the video signal is ``WHITE'' or ``BLACK'', the program will catch control-C. The first control-C closes the device, the second control-C exits to the shell.

Listing 3. wandtest.c

A user-side program, available for download from the source listed below, reads and decodes Bookland and UPC bar codes. It is in principle not much different from this test program. Once you get the hang of scanning with a nice steady motion, the verbose output from that program--which includes the measured width of each bar--can be used to determine the quality of a printed bar code.

Remove the Device Driver

To remove the device driver, promote yourself again to superuser with the command su and type in root's password. Using the command rmmod bcdrv will drop DTR to turn off power to the wand interface and remove the driver from the system.

I/O Port Reservations

Before ever loading your device driver, entering the shell command cat /proc/ioports will probably show that the serial port you propose to use is already reserved by some kernel driver. That reservation gets pushed aside when we load our bar code device driver. If that initial reservation needs to be restored, then you have what is called an exercise for the reader: Modify the cleanup_module( ) routine such that the original port reservation gets restored when the bar code device driver is removed. It would seem that a call to reserve_region( ) with the original driver's name would do the trick, but I have not tried it, so you get to experiment.


Jack Dennon ( studied mechanical engineering at Oregon State University in Corvallis, mayhem at the Infantry School at Fort Benning and low flying at the Army Aviation School at Fort Rucker. Nowadays he uses DOS for maintenance of legacy sawmill systems, studies how to replace them with Linux and helps his wife homeschool their four children.


Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot