Reputation: 857
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
Upvotes: 0
Views: 4199
Reputation: 45101
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:
Upvotes: 1
Reputation: 25277
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();
}
}
Upvotes: 4
Reputation: 82096
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?
Upvotes: 0