Using Barcode Reader for your C++ Program

As you might have heard, Bracode readers are special input devices for computers to read barcode. Barcode reader first scan the barcode using a light beam( usually a laser beam ) and analyze the width and distance between the bars. After successful scanning it convert the data into ASCII code which can be decoded by our computer. Simply we can say, a ps/2 barcode reader is same as that of a keyboard. Both generates the same ASCII code for representing an alphanumeric character. For more about ascii, read my article.

A ps/2 barcode readrer is the simplest at the same time more efficient for general purposes. In C++ the header file iostream.h defines cin, cout commands for standard input and output operations respectively. As default, keyboard is the standard input device. Since our ps/2 barcode reader can act as an alternative keyboard by reading and sending ASCII codes to the computer, surely we can use our cin command for data input.

I hope it is clear for you now. For further study, Let’s just analyze the below situation. Imagine you have a barcode containing data ‘1001’. When you scan it with barcode reader it generates a signal in ASCII code and send it to computer in the same way when you press ‘1001’ using your keyboard. The computer can’t distinguish from where the data came. So further processes can be programmed as our intentions.

There are a lot of type barcode readers. For general purposes like supermarket, library programs, i recommend ps/2 barcode reader which can be used along with our keyboard. let’s just take a sample c++ program to read from barcode reader.

#include<iostream>
#include<conio.h>
using namespace std;
void main() {
  int a = 0;
  //Scan a barcode (simple barcode containing a number ). Don't use keyboard  
  cin >> a;
  if (a) {
    cout << "n Data Received !n" << "Barcode is :" << a;
  }
  return;
}

Suggested Read : The Quick Response Codes

Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

4 COMMENTS