views:

267

answers:

3

I understand the barcode scanner is like a keyboard and acts as such. What I need is a scanner that has some sort of api (.net/c# preferably) that i can plug into my app. Is there a particular scanner hardware vendor devs have used or are currently using, that has a .net api? or can I use any arbitrary scanner and build an api around it or use one of the open source ones?

So 1 thing I would like to do, is be able to get the value off the usb scanner without placing the cursor on the particular text field.

Thanks

A: 

I have implemented a similar system using a barcode scanner. I am pretty sure (not 100% it was a long time ago) that the text came in as the one large string and only raised the one keypress event. So what you could do is match the input string against a typical barcode string e.g. KeyPressEvent, and if it matches then lookup the item. Or even just check the length of chars in the string (as if its keyboard input it should only be 1 char at a time).

Have you tested it out on a basic form?

James
A: 

Well, it depends. If you have a USB barcode scanner it acts exactly like a keyboard, reading the scanned text directly into a focused control. If you have a serial barcode scanner, you will need to use the SerialPort class in .NET.

If you are going to need to parse the data before filling a textbox, etc. then best bet would be to use a Serial Scanner.

Here's a code sample for use:

public partial class Form1 : Form
{
     SerialPort _serialPort;

     // delegate is used to write to a UI control from a non-UI thread
     private delegate void SetTextDeleg(string text);

     private void Form1_Load(object sender, EventArgs e)
     {
           // all of the options for a serial device
           // can be sent through the constructor of the SerialPort class
           // PortName = "COM1", Baud Rate = 19200, Parity = None, 
           // Data Bits = 8, Stop Bits = One, Handshake = None
           _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
           _serialPort.Handshake = Handshake.None;
           _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
           _serialPort.ReadTimeout = 500;
           _serialPort.WriteTimeout = 500;
           _serialPort.Open();
     }

     private void btnStart_Click(object sender, EventArgs e)
     {
           // Makes sure serial port is open before trying to write
           try
           {
                if(!_serialPort.IsOpen)
                     _serialPort.Open();

                _serialPort.Write("SI\r\n");
           }
           catch (Exception ex)
           {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
           }
     }

     void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
     {
          Thread.Sleep(500);
          string data = _serialPort.ReadLine();
          this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
     }

     private void si_DataReceived(string data)
     {
           textBox1.Text = data.Trim();
     }
}
Kyle Rozendo
you code will throw cross-thread call exception.
Benny
It won't. Forgot to turn on DTR and RTS though. And get rid of the Sleep().
Hans Passant
sweet example Kyle, I will be using a usb scanner, however I'll come back to grab some ideas from this here snippet.
simplyme
A: 

To stay on a scanner that acts like a keyboard you could also try, to detect all key pressed within your whole application. To get a KeyPress event within your application you should set Form.KeyPreview to true and register to the above mentioned event of your form.

Now you'll receive every key before it is send to the current active control and you can do with it, whatever you like. To prevent that it will send to the current active control after you have done your work, set e.Handled to true.

But this solution has definitely two drawbacks:

  1. It only works if your application is the active window. So if the user changes the focus to another application you won't get the key presses anymore.
  2. Maybe it can become very tricky to decide if the incoming key was sent from a scanner or a user just pressed a key on his keyboard. One possibility could maybe to cache the incoming keys and measure the delta times between them, cause a human normally doesn't enter as fast as a scanner (unless you're maybe a secretary with 300 or more keyboard hits in a minute ;-)).
Oliver
Thanks Oli this sounds more like the solution I am looking for. I will take it that scanners in general need devs to build an api to handle what behaviours they want from them scanners?
simplyme