Вы находитесь на странице: 1из 13

Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 1

Barcode Scanner
COM Object
C# Documentation

Written By: Brandon Fogerty


brandon@jujikasoft.com
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 2

Table of Contents

1. Table of Contents p. 2
2. What do you need? p. 3
3. Setting up your Environment p. 4
4. Start Coding p. 8
5. Thanks And Contact Information p. 13
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 3

What do you need?

You will need an NCR kiosk along with a scanner attached to the NCR device.
You will also need to install the proper software so that the kiosk can recognize the scanner
device. When we write code that utilizes the NCR barcode scanner, we will be using the OPOS
COM object.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 4

Setting up your Environment


Start Visual Studio.NET

Create a New C# Windows Application Project.

Now we will need to add the OPOS barcode scanner COM object.

Do the following,
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 5

Then Select the COM Object OPOS Scanner Control. Then click OK.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 6

Now we will need to add the COM object to our toolbox. We can just right click on the tool box
and select “Customize Toolbox”.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 7

Now we will select our OPOS Scanner Control. Then click OK.

Now you will see the OPOS Scanner Control in your toolbox.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 8

Start Coding
Now it is time to explain how we will write code in C# that will take advantage of the
barcode scanner. First, drag and drop the OPOS Scanner Control onto your form.

Now we need start writing some code.

We should have a new object called “axOPOSScanner1” by default. This object contains methods
and events that will access the barcode scanner and return the information we need such as the
barcode. The barcode will be returned as a string.

Our first step will be to initialize the barcode scanner. We can do so buy putting the following code in
the Form_Load event.
private void Form1_Load(object sender, System.EventArgs e)
{
// Initialize everything
axOPOSScanner1.BeginInit();

axOPOSScanner1.Open("NCRScanner.1"); // The profile name of the device.


axOPOSScanner1.ClaimDevice(0);
axOPOSScanner1.DeviceEnabled=true;
axOPOSScanner1.DataEventEnabled = true;
axOPOSScanner1.DecodeData = true;

When you run this code, you will hear a BEEP sound when the kiosk starts up the application.
This will let you know that your application has accessed the barcode scanner correctly.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 9

The OPOS scanner control has a special event that will interrupt the application when a book has been
scanned. This event will run constantly in the background. This event is called

private void axOPOSScanner1_DataEvent_1(object sender,


AxOposScanner_1_8_Lib._IOPOSScannerEvents_DataEventEvent e)

To access the raw data that has been scanned by the barcode scanner, we can set a string equal to the
barcode scanner’s “ScanData” property.
String strISBN = axOPOSScanner1.ScanData.ToString()

The string returned by ScanData will be a RAW data string. It will not be formatted in the manner
you want. You will need to remove the first 4 characters from the string. Lastly you will need to
compute the check digit for the 10th character in the string. When these additional steps have been
taken, you will have the correct ISBN number. I have already written functions that will do this for
you.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 10

The following function will computer the last check digit for you.

int iComputeCheckDigit(int isbn)


{
int iCheck = 0;
int num = 0;
int iSums = 0;
int counter = 10;

String isbnString = Convert.ToString(isbn);

char []isbnArray = new char[isbnString.Length];

isbnArray = isbnString.ToCharArray(0, isbnString.Length);

counter = isbnString.Length+1;

for(int i=0; i<isbnString.Length; i++)


{
num = Convert.ToInt16(isbnArray[i].ToString());
iSums += (num * counter);
counter--;
}

counter = 0;
while((iSums + counter)%11 != 0)
{
counter++;
}

iCheck = counter;

return iCheck;
}
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 11

The following will truncate the first 4 characters and give you the correct ISBN number.

string strBarcode2ISBN(String strBarcode)


{
if(strBarcode != "")
{
// Delete the 4 digits at the beginning of the string.
strBarcode = strBarcode.Remove(0,4);
// Delete the last digit of the string.
strBarcode = strBarcode.Remove(strBarcode.Length-1,1);

// Now computer the Check Digit to be the new last character


// of the string.
if( iComputeCheckDigit(Int32.Parse(strBarcode))==10)
{
// If the check digit is 10, then the check digit should
// be X.
strBarcode = strBarcode + "X";
}
else
{
strBarcode = strBarcode +
iComputeCheckDigit(Int32.Parse(strBarcode)).ToString();
}
}
else
{
strBarcode = "BARCODE STRING IS NULL!";
}

return strBarcode;
}

The string returned by the strBarcode2ISBN will return a string will the correct ISBN number. The
reason it returns the ISBN as a string and not a digit is because if the check digit is equal to 10, the
check digit is then ‘X’.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 12

The code for your Data_Event can look like the following.

//Our event for when a book has been scanned


private void axOPOSScanner1_DataEvent_1(object sender,
AxOposScanner_1_8_Lib._IOPOSScannerEvents_DataEventEvent e)
{

textBox2.Text= axOPOSScanner1.ScanData.ToString();
textBox2.Text += "\r\nScan Label: " + axOPOSScanner1.ScanDataLabel.ToString();

// String data found by the ScanData property.


String strISBN = axOPOSScanner1.ScanData.ToString();
strISBN = strBarcode2ISBN(strISBN);

axOPOSScanner1.ClearInput(); // Clear the Data Buffer.

axOPOSScanner1.DataEventEnabled = true; // Set to true EACH time!!!!


}

It is very important that you remember to set the DataEventEnabled property to true each and
Everytime you use the DataEvent event. If not, you will not be able to keep scanning for barcodes.

That is all there is to writing a C# application that takes advantage of the barcode scanner in the kiosk.
Kiosk Barcode Scanner C# Tutorial By: Brandon Sean Fogerty 13

Thanks and Contact Information


Special Thanks goes to:
Louis Bravo
JP Clementi
Dr. Shah
Dr. Peltsverger
NCR
And most importantly, GOD!!!!

I hope and pray that you have enjoyed this tutorial. If you have any questions, please feel free to
email me at
brandon@jujikasoft.com

or visit my webpage at

http://www.jujikasoft.com

Thank you so much for reading


And May GOD Bless You Always!!!!!!!!

-- Brandon Sean Fogerty


GSW State University

Вам также может понравиться