I want to run my barcode scanner(model is MH-ET LIVE Scanner v3.0) with host shield. Barcode sannner is TTL3, 3V. It has a button for scanning.
I tried the code but the monitor showed just "strart". please help me
this is my USB_desc.
I use this code. But my barcode just beep and anything happen on the serial monitor. what is wrong..
#include <hidcomposite.h>
#include <hidescriptorparser.h>
#include <hidusagestr.h>
#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 <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
USB Usb;
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);
if (c)
OnKeyPressed(c);
}
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
Serial.print((char)key);
};
KbdRptParser Prs;
void setup()
{
Serial.begin(9600);
if(Usb.Init()==-1) Serial.println("OSC did not start.");
else Serial.println("Barcode Ready");
Hid.SetReportParser(0,(HIDReportParser*)&Prs); //Change "Keyboard" for "Hid"
}
void loop()
{
Usb.Task();
}
The question that has to be cleared is does the Arduino act as an USB-host or as an USB-host-controller and does the scanner play the fitting partner-role.
Usually scanners are connected to a PC where the PC acts as the USB-host-controller
anything similar to a keyboard or a mouse acts as an USB-host and requieres an USB-host-controller
as only the USB-host-**controller **can initiate communication.
The libraries names ....hid.h sound like these libraries act as USB-host.
But I have never used them so I don't know it.
If you really want to work with the USB-interface of the scanner I guess your arduino needs libraries that act as
USB-host-controller
This is another example that tinkering with microcontrollers is not superstandardised as USB-devices that are intended to be plugged into a PC-USB-socket or at least something PC-like like a rasperry pie Ordeiod etc.
If no USB-host-CONTROLLER-libs are written for Arduino
I guess it will be much easier to use a second serial interface. Even if the second serial interface is software-serial.
So changing to an ESP32 board which has GPIO-pins 16 / 17 (which us Uart 2) connected
or an Arduino Mega 2560 which has four hardware UARTS will be easier to use.
best regards Stefan
StefanL38:
The question that has to be cleared is does the Arduino act as an USB-host or as an USB-host-controller and does the scanner play the fitting partner-role.
Usually scanners are connected to a PC where the PC acts as the USB-host-controller
anything similar to a keyboard or a mouse acts as an USB-host and requieres an USB-host-controller
as only the USB-host-controller can initiate communication.
The libraries names ....hid.h sound like these libraries act as USB-host.
But I have never used them so I don't know it.
If you really want to work with the USB-interface of the scanner I guess your arduino needs libraries that act as
USB-host-controller
This is another example that tinkering with microcontrollers is not superstandardised as USB-devices that are intended to be plugged into a PC-USB-socket or at least something PC-like like a rasperry pie Ordeiod etc.
If no USB-host-CONTROLLER-libs are written for Arduino
I guess it will be much easier to use a second serial interface. Even if the second serial interface is software-serial.
So changing to an ESP32 board which has GPIO-pins 16 / 17 (which us Uart 2) connected
or an Arduino Mega 2560 which has four hardware UARTS will be easier to use.
best regards Stefan
There is no USB_Host_Controller lib... If I use a Arduino Mega 2560 what is a difference from Arduino uno.
An arduino uno has only one hardware UART. Hardware-UART means this interface can receive up to 64 bytes while the Arduino is completely busy with other things.
A serial interface based on pure software (softwareserial) must have some processor-time to make it work and the baudrate can't be very high. As a higher baudrate requires faster reaction. receiving something serial and the command delay() exclude each other. While your Arduino is delay-ING nothing serial can be received through a software-serial interface.
An Arduino Mega 2560 has four hardware based serial interfaces. Which means the processor can be busy with other things and still can receive serial bytes on four interfaces at the same time (almost) in parallel.
As the processor can NOT predict when serial data will rush in the processor has to be prepared all and any time for receiving.
An original Arduino Mega 2560 is 30 Euros. A clone maybe 14 euros. ESP32-boards that have already WiFi and Bluetooth on board are at 10 to 15 Euros. As long as the higher electricity consumption of the ESP32 (needs up to 300 mA) is not a problem my opinion is : the ESP32 is the better Arduino.
best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REAL professionals write code this way.
Congrats on getting the USB host shield working. The USB descriptors show the barcode reader is not in USB HID keyboard mode so the sketch will not work.
The barcode manual "Scanner-v3 manual.pdf" on page 4 shows how to switch the reader between UART and HID modes. Unplug the reader then move the yellow jumpers to the HID position. Plug in to a computer. The reader should appear as a USB keyboard. Plug in to the USB host shield. The USB descriptor should change to USB keyboard and the USB keyboard sketch should work.
Pagus:
Congrats on getting the USB host shield working. The USB descriptors show the barcode reader is not in USB HID keyboard mode so the sketch will not work.
The barcode manual "Scanner-v3 manual.pdf" on page 4 shows how to switch the reader between UART and HID modes. Unplug the reader then move the yellow jumpers to the HID position. Plug in to a computer. The reader should appear as a USB keyboard. Plug in to the USB host shield. The USB descriptor should change to USB keyboard and the USB keyboard sketch should work.
I change to the HID modes but it is still not working.... The serial monitor just shows "barcode ready"
Ok, the reader is now in USB HID keyboard but I do not know why the sketch does not work. Does the USB host shield with keyboard sketch works with a real USB keyboard? If so, the problem may be the 2D barcode reader draws too much current. You will have to power the reader separately, not from the USB host shield. Or try powering the Uno via its barrel connector. See the Uno specifications for recommended power supplies.
Hi chovvvy,
I suggest you refer to this article: Arduino barcode scanner - RTscan
please read the PartIII, there is an introduction for using barcode scanner module with Arduino via USB host, you can find information either for USB-HID or USB virtual com, sample codes also included.
Hope this helpful for you.