CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2010
    Posts
    10

    Reading the barcode through usb barcode scanner using C++

    Can anyone help me?
    I have to write a program for the usb barcode scanner to read the barcode. I haven't done Windows API programming before. I have to write the program in C++. I have gathered some information from the net. I have some basic code for connecting to usb device but I do not understand how to send and receive the reports and what other stuff would be there.

    I will appreciate any help.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Reading the barcode through usb barcode scanner using C++

    I have worked with several barcode scanners, and all of them were very easy to use, because they give you a string of characters, just like if the code was typed on a keyboard.

    First, read the manuals of your scanner.

  3. #3
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    If you have worked with the scanners, please help me to do this. This is something completely new for me. The scanner I am using is usb microscan camera

  4. #4
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    when I am scanning something, it gives me a string of characters

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Reading the barcode through usb barcode scanner using C++

    The meaning of the characters will also depend on the bar code format:

    http://en.wikipedia.org/wiki/Barcode#Types_of_barcodes
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    Its a Aztec code. Do you have any idea about this programming?

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Reading the barcode through usb barcode scanner using C++

    No, sorry, I've only worked on printing normal barcodes ( like Universal Product Code ).

    As olivthill2said , read the manuals of your scanner. The manufacturer's website might have documentation or even an SDK.
    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    olivthill2 , can you help me with this programming, as you already have worked on it.

  9. #9
    Join Date
    Apr 2009
    Posts
    598

    Re: Reading the barcode through usb barcode scanner using C++

    Sorry, I don't understand where is the problem.

    If you know how to use a string of characters coming from a keyboard, then you should know how to use a string of characters coming from a scanner.
    As a matter of fact, when I was programming for the scanner, I did not always have it near me. So, instead, I typed characters on the keyboard for my tests, and that was the same thing as if I was using a scanner.

  10. #10
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    Hi olivthill2,

    Truly speaking, I don't have any knowledge about this programming. I am new to it. I have got the project. I have to write a driver for the barcode scanner camera using C++. Can you help me with this programming. Can you provide me with some starting code.

  11. #11
    Join Date
    Nov 2010
    Posts
    30

    Exclamation Re: Reading the barcode through usb barcode scanner using C++

    Wow that was confusing,

    Assuming you do not actually need to write the drivers to control the USB communication
    And assuming that you can receive bytes or chars via some .dll delivered by the company that manufactures the barcode scanner....

    Then there's realy not much left to do, whatever is scanned by the scanner is completely variable and is dependant on the company that printed the 2D code. Just do with it what you want to do with it, we cant help you with that unless you tell us what you want to do with it.

    If you dont even receive the characters then you need to get the manual that discribes the functions implemented by there libraries that you usually can download on their website (the manufacturer of the scanner) if that fails just e-mail their technical support for information on this.

    If you work in the company that manufactures the barcode scanner, and you need to program the driver to communicate with the scanner OVER USB then you are about to enter a shitstorm. :P

  12. #12
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    I got the driver from them. The project is, user will be able to dispense the gift card from ATM. I have to fix this scanner in the ATM machine, which will scan the barcode from the gift-card coming from the ATM. The camera is usb barcode scanner.

  13. #13
    Join Date
    Nov 2010
    Posts
    30

    Question Re: Reading the barcode through usb barcode scanner using C++

    Ok, thats a start, so you have the drivers / libraries and you dodged the shitstorm.

    I got the driver from them. The project is, user will be able to dispense the gift card from ATM. I have to fix this scanner in the ATM machine, which will scan the barcode from the gift-card coming from the ATM. The camera is usb barcode scanner.
    Good now we got some context, however it's not enough. We would like to know what you want to do with the data you get from it but for now let's start at the start:

    Normally, the USB scanner comes with a manual for implementation via libs / drivers. Even more likely, the USB connection is an emulated RS232 port (the driver of the scanner would do this) and is therefor very easy to use, let's start there. Care to post the manufacturer of the scanner AND the typenumber! So I can check it out what you are dealing with and how the company intended to implement the barcode scanner? (For example Cognex is a manufacturer of 2D scanners.)

    In your original post, you talked about some basic code to connect to the device, it would help a great deal if you would post that here too so we can have a look at it. Usually that gives atleast an idea what you are trying to do.

  14. #14
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    The company is Microscan who produces this scanner. And here is the code that I got from the internet. I have to us visual studio 6:

    #include "stdafx.h"
    #include "usb1.h"
    //#include <setupapi.h>
    #include<iostream.h>

    extern "C" {
    #include "hidsdi.h"
    #include <setupapi.h>
    #include <dbt.h>
    #include <hidusage.h>
    }

    #pragma comment(lib, "setupapi.lib")
    #pragma comment(lib, "hid.lib")

    CUSBDevice::CUSBDevice()
    {
    handle = INVALID_HANDLE_VALUE;
    EventObject = INVALID_HANDLE_VALUE;
    //HIDOverlapped; whatever is in there should be fine.
    }

    CUSBDevice::~CUSBDevice()
    {
    Disconnect();
    }

    CUSBDevice::CUSBDevice(const DWORD *vendorID, const DWORD *productID)
    {
    handle = INVALID_HANDLE_VALUE;
    EventObject = INVALID_HANDLE_VALUE;
    //HIDOverlapped; whatever is in there should be fine.
    Connect(vendorID, productID);
    }


    HANDLE CUSBDevice::ConnectToIthDevice (DWORD deviceIndex)
    {
    GUID hidGUID;
    HDEVINFO hardwareDeviceInfoSet;
    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
    ULONG requiredSize;
    HANDLE deviceHandle = INVALID_HANDLE_VALUE;
    DWORD result;
    // BOOL SetupDiDestroyDeviceInfoList(HDEVINFO hardwareDeviceInfoSet);

    //Get the HID GUID value - used as mask to get list of devices
    HidD_GetHidGuid (&hidGUID);

    //Get a list of devices matching the criteria (hid interface, present)
    hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
    NULL, // Define no enumerator (global)
    NULL, // Define no
    (DIGCF_PRESENT | // Only Devices present
    DIGCF_DEVICEINTERFACE)); // Function class devices.

    deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    //Go through the list and get the interface data
    //this is the code to call the function the first time.

    result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
    NULL, //infoData,
    &hidGUID, //interfaceClassGuid,
    deviceIndex,
    &deviceInterfaceData);

    /* Failed to get a device - possibly the index is larger than the number of devices */
    if (result == FALSE)
    {
    //When finished using the hardwareDeviceInfoSet returned by SetupDiGetClassDevs, the application should call SetupDiDestroyDeviceInfoList

    SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
    return INVALID_HANDLE_VALUE;
    }

    //Get the details with null values to get the required size of the buffer
    SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
    &deviceInterfaceData,
    NULL, //interfaceDetail,
    0, //interfaceDetailSize,
    &requiredSize,
    0); //infoData))

    //Allocate the buffer
    //allocated memory for the deviceDetail structure, sets the cbSize property of deviceDetail and calls the
    //function again, passing the returned buffer size in length.

    deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);
    deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

    //Fill the buffer with the device details
    if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
    &deviceInterfaceData,
    deviceDetail,
    requiredSize,
    &requiredSize,
    NULL))
    {
    SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
    free (deviceDetail);
    return INVALID_HANDLE_VALUE;
    }

    //Open file on the device
    //Createfile: to request a handle to a device and
    deviceHandle = CreateFile (deviceDetail->DevicePath,
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, // no SECURITY_ATTRIBUTES structure
    OPEN_EXISTING, // No special create flags
    FILE_FLAG_OVERLAPPED,
    NULL); // No template file


    SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
    free (deviceDetail);
    return deviceHandle;
    }

    bool CUSBDevice::Connect(const DWORD *vendorID, const DWORD *productID)
    {
    Disconnect();
    handle = INVALID_HANDLE_VALUE;
    DWORD index = 0;
    HIDD_ATTRIBUTES deviceAttributes;
    BOOL matched = FALSE;

    while (!matched && (handle = ConnectToIthDevice (index)) != INVALID_HANDLE_VALUE)
    {
    if (!HidD_GetAttributes (handle, &deviceAttributes))
    return INVALID_HANDLE_VALUE;

    if ((vendorID == 0 || deviceAttributes.VendorID == *vendorID) &&
    (productID == 0 || deviceAttributes.ProductID == *productID)) {
    EventObject = CreateEvent(NULL, TRUE, TRUE, "");
    //Set the members of the overlapped structure.
    HIDOverlapped.Offset = 0;
    HIDOverlapped.OffsetHigh = 0;
    HIDOverlapped.hEvent = EventObject;

    return true; /* matched */
    }

    CloseHandle (handle); /* not a match - close and try again */

    index++;
    }

    return false;
    }


    #ifndef _DEBUG
    void CUSBDevice::ExploreToDebugWindow()
    {
    USHORT buffer[128];
    HANDLE deviceHandle = INVALID_HANDLE_VALUE;
    DWORD index = 0;
    HIDD_ATTRIBUTES deviceAttributes;
    BOOL matched = FALSE;

    while (!matched && (deviceHandle = connectToIthUSBHIDDevice (index)) != INVALID_HANDLE_VALUE)
    {
    if (!HidD_GetAttributes (deviceHandle, &deviceAttributes))
    return;

    HidD_GetProductString(deviceHandle, buffer, sizeof(buffer));

    CString strName;
    for (int x = 0; buffer[x]; x++) {
    strName += (char)buffer[x];
    }


    TRACE("HID Device - Vendor %x, Product %x, Version %x, String \'%s\'\r\n", deviceAttributes.VendorID, deviceAttributes.ProductID, deviceAttributes.VersionNumber, strName);
    CloseHandle (deviceHandle); /* not a match - close and try again */

    index++;
    }

    return;
    }

    #endif


    void CUSBDevice:isconnect()
    {
    if (EventObject != INVALID_HANDLE_VALUE)
    CloseHandle(EventObject);
    EventObject = INVALID_HANDLE_VALUE;

    if (handle != INVALID_HANDLE_VALUE)
    CloseHandle(handle);
    handle = INVALID_HANDLE_VALUE;
    }

    bool CUSBDevice::Write(const BYTE *bytes, int nBuffLen)
    {
    unsigned long BytesWritten;
    return WriteFile(handle, (LPCVOID)bytes, nBuffLen, &BytesWritten, NULL);
    }


    int CUSBDevice::Read(BYTE *bytes, int nBuffLen)
    {
    unsigned long BytesRead = 0;
    ReadFile(handle, (LPVOID)bytes, nBuffLen, &BytesRead, &HIDOverlapped);
    DWORD Result = WaitForSingleObject(EventObject, 50);
    if (Result == WAIT_OBJECT_0) {
    ResetEvent(EventObject);
    return BytesRead;
    }
    if(Result == WAIT_TIMEOUT) {
    Result = CancelIo(handle);
    }

    ResetEvent(EventObject);
    return 0;
    }

  15. #15
    Join Date
    Nov 2010
    Posts
    10

    Re: Reading the barcode through usb barcode scanner using C++

    its a MS-4 barcode scanner

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured