问题
I am attempting to a simple windows form application, where the users scans a barcode then the application decodes the barcode, then inserts an row into a database. A basic track application. I picked up a Motorola scanner and downloaded the SDK for windows development. I read though "APPENDIX A WRITE SIMPLE APPLICATIONS USING THE SCANNER SDK API" seemed like a good place to start in the Developers guide (http://support.symbol.com/support/search.do?cmd=displayKC&docType=kc&externalId=14978402apdf&sliceId=&dialogID=311240750&stateId=1%200%20311224446). I get the 5 console examples working fine but I can't get the windows form application to work, the OnBarcode Event is never triggered. I followed this video(http://www.youtube.com/watch?v=GfIWWUw4YSc&t=15m47s) by Motorola step by step.
I seem to be having a similar problem to this: Motorola barcode scanner SDK events C# but I am using a USB connection.
I realized that the sample code was original written in .net 2.0 so I tried recompiling the project and it still didn't' work. I then tried changing CCoreScannerClass to CCoreScanner so that Embeded Interop Types would work but that didn't help either. Here is the sample code:
using CoreScanner;
namespace Scanner_WindowsFormsApplication
{
public partial class Form1 : Form
{
CCoreScanner cCoreScanner;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void OnBarcodeEvent(short eventType, ref string pscanData)
{
string barcode = pscanData;
this.Invoke((MethodInvoker)delegate { textBox1.Text = barcode; });
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//Instantiate CoreScanner Class
cCoreScanner = new CCoreScanner();
//Call Open API
short[] scannerTypes = new short[1];//Scanner Types you are interested in
scannerTypes[0] = 1; // 1 for all scanner types
short numberOfScannerTypes = 1; // Size of the scannerTypes array
int status; // Extended API return code
cCoreScanner.Open(0, scannerTypes, numberOfScannerTypes, out status);
// Subscribe for barcode events in cCoreScannerClass
cCoreScanner.BarcodeEvent += new _ICoreScannerEvents_BarcodeEventEventHandler(OnBarcodeEvent);
// Let's subscribe for events
int opcode = 1001; // Method for Subscribe events
string outXML; // XML Output
string inXML = "<inArgs>" +
"<cmdArgs>" +
"<arg-int>1</arg-int>" + // Number of events you want to subscribe
"<arg-int>1</arg-int>" + // Comma separated event IDs
"</cmdArgs>" +
"</inArgs>";
cCoreScanner.ExecCommand(opcode, ref inXML, out outXML, out status);
Console.WriteLine(outXML);
}
catch (Exception exp)
{
Console.WriteLine("Something wrong please check... " + exp.Message);
}
}
}
}
I read through the Developer guide and found this: "BarcodeEvent Triggered when a scanner captures bar codes. To receive BarcodeEvents, an application needs to execute the REGISTER_FOR_EVENTS method with the SUBSCRIBE_BARCODE event type." I am new to c# so I don't really know what this means and how it applies to the example.
If https://stackoverflow.com/users/68043/scott reads this thread I would love to know how you got the onBarcodeEvent to function in your thread: Dialog hangs when called from event
回答1:
To get a barcode event from Motorola Scanner SDK your scanner should be in IBM handheld USB or SNAPI. If it is connected to the serial port it should be in NIXDORF MODE B.If your scanner connected in HIDKB mode you can not have barcode events through SDK since scanner works as a keyboard. My advice is to first try the executable of the sample application comes with the SDK and check whether you can see the event from the provided application.
回答2:
Every time I've worked with barcode scanners, I found it was much easier to simply use the barcode scanner as a keyboard; i.e., put the focus in your WinForms app to a textbox to wait for the barcode to scan, then handle the text_changed event of the TextBox
回答3:
I just went through the same problem. After having had a beeter look at the Motorola documentation, I've found that The following two lines only tells the scanner that you want to get events. (Turn on events)
int upcode = 1001
cCoreScanner.ExecCommand(opcode, ref inXML, out outXML, out status);
Now in order to do a barcode scan you need to send opcode 2011. so the code will look kind of like this:
int upcode = 1001
cCoreScanner.ExecCommand(opcode, ref inXML, out outXML, out status);
int upcode = 2011
cCoreScanner.ExecCommand(opcode, ref inXML, out outXML, out status);
When doing this, I do get the BarcodeEvent
回答4:
using the opcode 2011 you manage to trigger the backcode event for USB HIDKB scanner?
Did you add also a piece of the following code:
"<inArgs><scannerID>1</scannerID></inArgs>"
when executing the 2011 command?
来源:https://stackoverflow.com/questions/10520639/corescanner-motorola-events-not-being-triggered-c-sharp