Re: [linux-usb-devel] Grabbing characters from a Symbol barcode scanner

2007-06-05 Thread Andy Stewart
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Jiri Kosina wrote:
 On Wed, 30 May 2007, Andy Stewart wrote:
 
 I have a Symbol USB barcode scanner that appears to work with Linux.  
 If I bring up a Konsole window and give it focus, characters are typed 
 into that window as I scan barcodes. However, what I'd like to do is 
 grab those characters in a C program.  I suppose that I should be able 
 to open the proper device and the characters would be available.  
 
 Yes, just open the proper evdev interface in /dev/input/eventX and read 
 the events directly from there. See Documentation/input/input.txt, section 
 5.
 
 You can use ioctl(EVIOCGID) to obtain vendor id/product id corresponding 
 to the given input device, so you can easily look up the event device 
 corresponding to the device you are looking for.
 

HI Everybody,

I'm sorry to take so long to get back to you.  Thank you for the replies.

I hacked up some code which uses the /dev/input/eventX interface.

I have attached my code to this email.  The code seems to work for me,
and perhaps it will be useful to someone else.  Constructive critiques
are most welcome.

Thanks again for your help!

Andy

- --
Andy Stewart, Founder
Worcester Linux Users' Group (http://www.wlug.org)
Chelmsford Linux Meetup Group (http://linux.meetup.com/393)
Amateur Radio:  KB1OIQ
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFGZiyuHl0iXDssISsRAv1+AJ9AS1/K2AtPz7B08tPz6aGlrVnc+ACfW8Fs
YE9neuBBe06+Jk4Cezjzv7M=
=bSA3
-END PGP SIGNATURE-
/*
 * Symbol Barcode Scanner Reader Software
 * Written by Andy Stewart
 * June 5, 2007
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Set the debug variable to 1 to get some debug printouts.
 *
 * One must have the Linux kernel sources installed in order for this to compile.
 *
 * To build it:
 * gcc -o read_bc_scanner read_bc_scanner.c
 * 
 * Note: this code was tested on Linux kernel rev. 2.6.16.21 (SuSE 10.1)
 *
 */

#include stdio.h

#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include string.h

#include linux/input.h

/* defined at the end of this file */
char cvt_ev_char(int);  
void debug_rcvd_event(struct input_event *);

#define MAX_IN_SIZE 30

main(int argc, char *argv[]) {

  int debug = 0;/* set this to 1 to get debug info */

  struct input_event ev[64];/* each scan causes more than one event...get them all */

  int fd = -1;  /* file descriptor for the scanner device */
  int bytes_read = 0;   /* number of bytes read by the read function */
  int i;/* loop variable */
  char scanner_in[30];  /* set this bigger if scanned input is more than 30 characters */
  char scan_char;   /* this holds one char at a time...will be concatenated into scanner_in[] */
  int wr_ptr = 0;   /* points into scanner_in so we know where to put the next scan_char */

  printf(Go ahead and scan something - press ctrl_C when finished.\n);
  printf(Look for printouts which say: Input from Scanner\n);
  printf(Ignore other printouts\n);

  /* The device interface can be found in /dev/input/by-id/*Symbol*, which should be a link
   * to /dev/input/eventN, where N is some number.  One may need to adjust the permissions of the
   * eventN file.
   *
   * Yeah, I know it sucks for me to have hardcoded this value...sorry, I was too lazy to compute it.
   */

  if ((fd = open(/dev/input/event3, O_RDONLY))  0) {
printf(Error opening file descriptor - do you have sufficient permission? Maybe it is incorrectly hardcoded - check the source. Exiting.\n);
return -1;
  }

  while (1) {
bytes_read = read(fd, ev, sizeof(struct input_event) * 64);

if (bytes_read  0) {
  printf(ERROR: can't properly read from device\n);
  return -1;
}

for (i=0; i  (int) (bytes_read / sizeof(struct input_event)); i++) {

  /* Look for a key press event */
  if ((ev[i].type == EV_KEY)  (ev[i].value == 1)) {

	if (ev[i].code != KEY_LEFTSHIFT) {
	  scan_char = cvt_ev_char(ev[i].code); /* Extract the character from the event */
	
	  if (debug) {
	debug_rcvd_event(ev[i]);
	printf(Scan char: %c\n, scan_char);
	  }

	  if (ev[i].code != KEY_ENTER) {
	

Re: [linux-usb-devel] Grabbing characters from a Symbol barcode scanner

2007-05-30 Thread Jiri Kosina
On Wed, 30 May 2007, Andy Stewart wrote:

 I have a Symbol USB barcode scanner that appears to work with Linux.  
 If I bring up a Konsole window and give it focus, characters are typed 
 into that window as I scan barcodes. However, what I'd like to do is 
 grab those characters in a C program.  I suppose that I should be able 
 to open the proper device and the characters would be available.  

Yes, just open the proper evdev interface in /dev/input/eventX and read 
the events directly from there. See Documentation/input/input.txt, section 
5.

You can use ioctl(EVIOCGID) to obtain vendor id/product id corresponding 
to the given input device, so you can easily look up the event device 
corresponding to the device you are looking for.

-- 
Jiri Kosina

-
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
___
linux-usb-devel@lists.sourceforge.net
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel