Interfacing with LS2208 Scanner using Python

// Expert user has replied.
A Ardyn Izuya 1 year 9 months ago
543 3 0

Hi All,

 

I have an LS2208 Barcode scanner. It is plugged in to my computer using the USB. I am trying to read the barcodes scanned using the scanner. I primarily work in python language.

I scoured the internet for the correct way to do it but nothing worked. I tried to read the barcode with standard method of keyboard input as the scanner is an HID device that types data into stream. That did not work either

 

Any help in this case would be appreciated.

 

Regards,

S R U

Please Register or Login to post a reply

3 Replies

A Ardyn Izuya

Greetings.

 

Just wanted to update that i have resolved my problem.

I am able to interface with LS2208 scanner using python with the help of following python package libraries.

1. PyUSB - https://pypi.org/project/pyusb/

2. LibUSB - https://pypi.org/project/libusb/

After installing them, here are some additional resources that need to be install in order to avoid errors when the script is run. I found all these by googling so you can also find them .

To resolve No Backend Error

        -- Download the latest Windows binary:  https://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z/download

        -- Unzip using 7zip into a temp dir

        -- If on 64-bit Windows, copy MS64\dll\libusb-1.0.dll into C:\windows\system32. If on 32-bit windows, copy MS32\dll\libusb-1.0.dll    into C:\windows\SysWOW64.

Here is the python script i used to interface with the barcode scanner.

"""

Author : Ardyn Izuya   02/17/2023

 

Change Log:

    -- Initial Release 02/17/2023

 

Description:

    -- Script to read barcode data from LS2208 scanner

    -- Script uses libusb and pyusb libraries

    -- Vendor ID and Product ID are mandatory fields

 

Steps to configure the script:

    -- Get Procduct ID and Vendor ID

        -- Go to Device Manager

        -- Select the correct device under Human Interface Devices

    -- Install the python libraries

        -- https://pypi.org/project/pyusb/

        -- https://pypi.org/project/libusb/

    -- To resolve No Backend Error

        -- Download the latest Windows binary:  https://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z/download

        -- Unzip using 7zip into a temp dir

        -- If on 64-bit Windows, copy MS64\dll\libusb-1.0.dll into C:\windows\system32. If on 32-bit windows, copy MS32\dll\libusb-1.0.dll into C:\windows\SysWOW64.

 

Resources:

    -- https://pypi.org/project/pyusb/ -- pusb package download

    -- https://pypi.org/project/libusb/ -- Libusb package download

    -- https://github.com/pyusb/pyusb

    -- https://github.com/pyusb/pyusb/issues/120#issuecomment-322058585

    -- beyondlogic.org/usbinnutshell

    -- asciitable.com

 

"""

 

import usb.core     # main module

import usb.util     # contains utility functions

import string

 

 

VENDOR_ID = 0x05e0

PRODUCT_ID = 0x1300

 

while True:

    try:

        # find our device using vendor ID and Product ID

        dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)

        print(dev)

    except usb.core.USBTimeoutError as timeOutError:

        print("Scanner Read Time Out Occurred \n")

        print("Closing device Object and")

       

 

    except Exception as e:

        print(f"The following exception has occurred {e} \n . Retrying.........")


The read functionality is already present in PyUSB GitHub page. Just follow that and make sure you are trimming your non-printable characters out.

 

 

Regards,

A I

R Riad Benallou

is your LS2208 mfd after December 2014 ?

S Steve Justi

This code works for Li2208, Here is a version that also read bar codes not only check connections 
 

import usb.core

import usb.util

import time


 

VENDOR_ID = 0x05e0

PRODUCT_ID = 0x1300


 

def read_scanner(dev):

    try:

        # Set the active configuration. With no arguments, the first

        # configuration will be the active one

        dev.set_configuration()


 

        # Claim the interface

        usb.util.claim_interface(dev, 0)


 

        # Endpoint address and size depend on the specific scanner, you may need to adjust them

        endpoint = dev[0][(0, 0)][0]

        size = endpoint.wMaxPacketSize


 

        while True:

            # Read data from the endpoint

            data = dev.read(endpoint.bEndpointAddress, size, timeout=1000)


 

            # Process the scanned data (replace this with your desired processing logic)

            scanned_code = data.tobytes().decode('utf-8')

            print(f"Scanned code: {scanned_code}")


 

    except usb.core.USBError as e:

        if "Access denied" in str(e):

            print("Access denied. Make sure you have the necessary permissions.")

        else:

            print(f"USB error: {e}")


 

    except usb.core.USBTimeoutError as timeOutError:

        print("Scanner Read Time Out Occurred")


 

    except Exception as e:

        print(f"An exception occurred: {e}")


 

    finally:

        # Release the interface

        try:

            usb.util.release_interface(dev, 0)

        except usb.core.USBError as e:

            print(f"Error releasing interface: {e}")


 

        # Close the device object to release resources

        usb.util.dispose_resources(dev)


 

if __name__ == "__main__":

    while True:

        try:

            # find our device using vendor ID and Product ID

            dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)


 

            if dev is not None:

                print("Device found:")

                # print(dev)

                read_scanner(dev)

            else:

                print("Device not found.")


 

        except Exception as e:

            print(f"An exception occurred: {e}")


 

        # Add a delay before the next iteration

        time.sleep(1)


 

CONTACT
Can’t find what you’re looking for?