Barcode scanner + mega2560 + usb shield host

Hello everyone

i have downloaded the project (Arduino code modified – Bar Code Scanner and USB Host Shield.) from https://www.electroingenio.com but it didnt work.....

the compile and the upload going well after i have modified the file hid.h to usbhid.h and added USB_ to

parameter : HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard(&Usb);

HIDBoot<USB_HID_PROTOCOL_KEYBOARD> Keyboard(&Usb);

but the barscanner doesn't work, it is beep but no data over serial monitor or LCD.

when i use mouse or keyboard instead barscanner ,it is OK and there is a data in serial mointor and lcd.

i use arduino mega2560 + usb shield host + lcd + barscanner made in china

note: the barscanner works well when i use it with the notpad.

I appreciate your help
thanks.

this is the code :

#include <usbhid.h>                           //Add to Oleg Mazurov code to Bar Code Scanner
#include <hiduniversal.h>                  //Add to Oleg Mazurov code to Bar Code Scanner
#include <usbhub.h>

#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <LiquidCrystal_I2C.h> //Download & include the code library can be downloaded below


#define DISPLAY_WIDTH 16

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE); // Initialize LCD Display at address 0x27 

USB     Usb;
USBHub     Hub(&Usb);                                          //I enable this line
HIDUniversal      Hid(&Usb);                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);

class KbdRptParser : public KeyboardReportParser
{
      void PrintKey(uint8_t mod, uint8_t key);             // Add this line to print character in ASCII
protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) 
{
  uint8_t c = OemToAscii(mod, key);

  if (c)
      OnKeyPressed(c);
}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key) 
{
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  

  if( millis() > next_time ) {
    lcd.clear();
    current_cursor = 0;
    delay( 5 );  //LCD-specific 
    lcd.setCursor( 0,0 );
  }//if( millis() > next_time ...

  next_time = millis() + 200;  //reset watchdog

  if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen
    lcd.setCursor( 0,1 );
  }

  Serial.print( (char)key );      //Add char to print correct number in ASCII
  lcd.print( (char)key );          //Add char to print correct number in ASCII
};

KbdRptParser Prs;

void setup()
{
  Serial.begin( 115200 );
  Serial.println("Start");
  if (Usb.Init() == -1) {
      Serial.println("OSC did not start.");
  }

  delay( 500 );

  Hid.SetReportParser(0, (HIDReportParser*)&Prs);        //Here I change  "Keyboard" for "Hid"
  // set up the LCD's number of columns and rows: 
  lcd.begin(DISPLAY_WIDTH, 2);
  lcd.clear();
  lcd.noAutoscroll();
  lcd.print("Ready");
  delay( 500 );
}

void loop()
{
Usb.Task();
}

No Answer !! OR My Question is WRONG

No Answer !! OR My Question is WRONG

Maybe you don't have enough patience. At least your link to the project is not a direct link, don't expect everybody to search for the project in that blog... ->BarCode Scanner + Arduino USB Shield – Electronics and Projects

i have downloaded the project (Arduino code modified - Bar Code Scanner and USB Host Shield.) from https://www.electroingenio.com but it didnt work.....

He modified the code from Oleg Mazurov to work with his specific model of barcode scanner. Why do you think that your scanner has exactly the same USB protocol? Even if you also have a Datalogic Quickscan, there are several models of this series and it's not clear if they all use the same protocol to talk to the PC.

note: the barscanner works well when i use it with the notpad.

That means only that it's not broken.

BTW: are you using exactly the same USB host shield? The one from circuitsathome?

pylon:
Maybe you don't have enough patience. At least your link to the project is not a direct link, don't expect everybody to search for the project in that blog... ->BarCode Scanner + Arduino USB Shield – Electronics and Projects

He modified the code from Oleg Mazurov to work with his specific model of barcode scanner. Why do you think that your scanner has exactly the same USB protocol? Even if you also have a Datalogic Quickscan, there are several models of this series and it's not clear if they all use the same protocol to talk to the PC.

That means only that it's not broken.

BTW: are you using exactly the same USB host shield? The one from circuitsathome?

sorry about my patience and thank you for your reply.

about the barcode scanner i can use any one as MR circuitsathome guy said and Mr. electroingenio said just it must has same protocol and class and sub class :

(It is necessary to consider the recommendation of Oleg Mazurov in verifying the parameters of our device, which identify the CLASS and communication PROTOCOL as follows:

Intf. Class: 03
Intf. Subclass: 01
Intf. Protocol: 01

and my barcode scanner has the same information above.

my scanner made in china obm-6800
my usb host shield is the same but i buy it from the local market i will show you a picture for it

But why it works well with mouse or keyboard instead scanner ?

i load the orginal code now from https://www.electroingenio.com/arduino-en/bar-code-scanner-arduino-usb-shield-2/
after i found the right library
but i still in the same situation

this is the code :

#include <hid.h>                           //Add to Oleg Mazurov code to Bar Code Scanner
#include <hiduniversal.h>                  //Add to Oleg Mazurov code to Bar Code Scanner
#include <usbhub.h>
#include <Wire.h> // Include Wire.h to control I2C
//#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <LiquidCrystal_I2C.h> //Download & include the code library can be downloaded below


#define DISPLAY_WIDTH 16

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE); // Initialize LCD Display at address 0x27 

USB     Usb;
USBHub     Hub(&Usb);                                          //I enable this line
HIDUniversal      Hid(&Usb);                                  //Add this line so that the barcode scanner will be recognized, I use "Hid" below 
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
 
class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);             // Add this line to print character in ASCII
protected:
	virtual void OnKeyDown	(uint8_t mod, uint8_t key);
	virtual void OnKeyPressed(uint8_t key);
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)	
{
    uint8_t c = OemToAscii(mod, key);
 Serial.print( (char)mod);      //Add char to print correct number in ASCII
    lcd.print( (char)mod ); 
    if (c)
        OnKeyPressed(c);
}
 
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)	
{
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  
 
    if( millis() > next_time ) {
      lcd.clear();
      current_cursor = 0;
      delay( 5 );  //LCD-specific 
      lcd.setCursor( 0,0 );
    }//if( millis() > next_time ...
 
    next_time = millis() + 200;  //reset watchdog
 
    if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen
      lcd.setCursor( 0,1 );
    }
 
    Serial.print( (char)key );      //Add char to print correct number in ASCII
    lcd.print( (char)key );          //Add char to print correct number in ASCII
};
 
KbdRptParser Prs;
 
void setup()
{
    Serial.begin( 115200 );
    Serial.println("Start");
 
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
 
    delay( 200 );
 
    Hid.SetReportParser(0, (HIDReportParser*)&Prs);        //Here I change  "Keyboard" for "Hid"
    // set up the LCD's number of columns and rows: 
    lcd.begin(DISPLAY_WIDTH, 2);
    lcd.clear();
    lcd.noAutoscroll();
    lcd.print("Ready");
    delay( 200 );
}
 
void loop()
{
  Usb.Task();
}

again no data just beep ..but it fine when i use keyboard or mouse
Plz see the picture attached

But why it works well with mouse or keyboard instead scanner ?

Because mouse and keyboard are two best standardized USB devices classes. Barcode scanners are far from that.

again no data just beep ..but it fine when i use keyboard or mouse

And what serial output do you get?

What does "fine when i use keyboard or mouse" mean? Do you get output if you connect a mouse?

pylon:
Because mouse and keyboard are two best standardized USB devices classes. Barcode scanners are far from that.

And what serial output do you get?

What does "fine when i use keyboard or mouse" mean? Do you get output if you connect a mouse?

Yes i get output ,numbers and letters when i connect mouse (when i move it obviously ) to the usb host shield, on the serial monitor and on the LCD , also when i connect keyboard i get output.
I read that the barcode scanner behaves exactly like a keyboard
I think the problem is covered with straw

I guess the problem is here :

uint8_t c = OemToAscii(mod, key);

I don't know

I read that the barcode scanner behaves exactly like a keyboard

Not usually.
A keyboard sends ‘key codes’, that are standardised, and map to the individual key, ctrl, alt, up, down states etc.
2D Barcode scanners typically send a string of ASCII characters that match the barcode.

Break your project down to remove everything but the USB, barcode and serial monitor.
Then work through what is being received and it’s format.
Your scanner may also have configurable modes - ACSCII, binary etc - make sure you have the mode you are planning to use.

Once you eliminate all the other stuff, and have meaningful barcode data working by itself, you can stitch that back in to your main program.

lastchancename:
Not usually.
A keyboard sends ‘key codes’, that are standardised, and map to the individual key, ctrl, alt, up, down states etc.
2D Barcode scanners typically send a string of ASCII characters that match the barcode.

Break your project down to remove everything but the USB, barcode and serial monitor.
Then work through what is being received and it’s format.
Your scanner may also have configurable modes - ACSCII, binary etc - make sure you have the mode you are planning to use.

Once you eliminate all the other stuff, and have meaningful barcode data working by itself, you can stitch that back in to your main program.

Thank you for your advice, i did it , i eliminated all other parts, still i have same situation.
I don't think that the scanner be configurable ascii or binary..do you have any program do that ?.... my own: OEM- 6800 https://www.google.com/url?sa=t&source=web&rct=j&url=https://m.alibaba.com/amp/product/1901948032.html&ved=2ahUKEwi6n7WXmsjhAhURExoKHQJCAm0QFjALegQIBxAC&usg=AOvVaw2gszSGCNGThlrUEkYjFQzM&ampcf=1&cshid=1554991384579

That product page says the scanner has RS232 capability.
WAAAY easier to implement an extra serial port on the MEGA 2560 with 3 extra hardware ports.

The serial RS232 interface uses simple ASCII character streams.
Not sure what the USB device presents as, you’d need to read the manual (possibly a virtual USB com port, or USB keyboard.)

Buy an RS232 converter (EBAY $5 with 2-wires plus 0V), then you can talk to the scanner with standard Serial1/2/3 commands)

You still have some work to do, but about 20% of that required for hosting a USB device.
Maybe once you’re running, you can go back to explore the feasibility of USB

lastchancename:
That product page says the scanner has RS232 capability.
WAAAY easier to implement an extra serial port on the MEGA 2560 with 3 extra hardware ports.

The serial RS232 interface uses simple ASCII character streams.
Not sure what the USB device presents as, you’d need to read the manual (possibly a virtual USB com port, or USB keyboard.)

Buy an RS232 converter (EBAY $5 with 2-wires plus 0V), then you can talk to the scanner with standard Serial1/2/3 commands)

You still have some work to do, but about 20% of that required for hosting a USB device.
Maybe once you’re running, you can go back to explore the feasibility of USB

you are right ,the RS232 communication is my second option.