riandanualdy:
Thanks bro, finally i can read the barcode scanner, and the output is same with the barcode.
and my question is, can you explain me what the meaning of your code give to me?
Glad that it seems to be working for you.
I'm guessing that the problem is as follows:
The Arduino/USB host polls the connected barcode reader periodically for new data (pseudo keypresses).
A real keyboard is quite slow because it is operated by a human, but the pseudo keyboard presented by the barcode reader is much faster.
Presumably the barcode reader only offers a piece of new data for a fixed period of time before it overwrites it with the next piece of data; if the piece of data is not collected on time - tough luck.
The Arduino with the USB host library is too slow to collect the data on time so some gets discarded.
In actual fact, the Arduino/USB library is not too slow, it just doesn't poll often enough. But the twist is that the polling time it uses is actually the time specified by the device! You can see it here in the USB_desc output from your first post:
Polling interval: 20
That is 20 USB frame times and seems to be too long for things to work properly.
The change to the code was just a hack to overwrite the polling interval supplied by the device with a fixed value of 3.
Polling more often will obviously put more USB load on your Arduino so it will have less time for other tasks. Polling less often will risk losing key presses. You can tune the value to balance this out to your satisfaction.
arduarn:
Glad that it seems to be working for you.
I'm guessing that the problem is as follows:
The Arduino/USB host polls the connected barcode reader periodically for new data (pseudo keypresses).
A real keyboard is quite slow because it is operated by a human, but the pseudo keyboard presented by the barcode reader is much faster.
Presumably the barcode reader only offers a piece of new data for a fixed period of time before it overwrites it with the next piece of data; if the piece of data is not collected on time - tough luck.
The Arduino with the USB host library is too slow to collect the data on time so some gets discarded.
In actual fact, the Arduino/USB library is not too slow, it just doesn't poll often enough. But the twist is that the polling time it uses is actually the time specified by the device! You can see it here in the USB_desc output from your first post:
Polling interval: 20
That is 20 USB frame times and seems to be too long for things to work properly.
The change to the code was just a hack to overwrite the polling interval supplied by the device with a fixed value of 3.
Polling more often will obviously put more USB load on your Arduino so it will have less time for other tasks. Polling less often will risk losing key presses. You can tune the value to balance this out to your satisfaction.
Thanks for explain that code, actually i have one problem again.
i want connect barcode scanner and rs232 device to arduino uno, and i want to display a output from barcode and rs232 device.
rs232 device data is (ST,+00000.00 g) and my rs232 device send a 10 data/second.so i want display like this (8994096215986 ST,+00000.00 g)
and this is my code, but the output just display a barcode scanner
#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>
#define DISPLAY_WIDTH 16
int i=1;
char c;
String Barcode;
#include <SoftwareSerial.h>
#define rxPin 8
#define txPin 9
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int j=1;
char d;
String Berat;
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);
if (c)
OnKeyPressed(c);
}
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
for (i=0;i < 1;i++){
c = ((char)key);
Barcode = String(Barcode +c);
if (Barcode.length()>=14){;
Serial.print(Barcode);
Barcode="";}}
//Serial.print( (char)key ); //Add char to print correct number in ASCII
for (j=0;j < 17;j++){
if (mySerial.available()) {
//Serial.println("SSS");
d = mySerial.read();
Serial.print(d);
Berat = String(Berat +d);
if (Berat.length()>=17){;
Serial.print(Berat);
Berat ="";
}
}}};
KbdRptParser Prs;
void setup()
{
// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(9600);
Serial.begin( 9600 );
Serial.println("Start");
if (Usb.Init() == -1) {
Serial.println("OSC did not start.");
}
Hid.SetReportParser(1, (HIDReportParser*)&Prs); //Here I change "Keyboard" for "Hid"
}
void loop()
{
Usb.Task();
}
I am using Honeywell barcode scanner + USB host shield (spark fun)+ arduino Uno and the bellow code. But the scanner is not powered up or nothing..... I am stucked...pls help... what to do....
#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>
USB Usb;
USBHub Hub(&Usb); //I enable this line
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);
};