Problem connecting barcode scanner to native USB on Arduino Due

I'm quite new to Arduino, i got my first boards two months ago, and I've mostly tried the basic stuff, like led control, pwm, character displays, steppers and so on. But I've also built a "useless box" and a "spirograph", all with a good portion of help from google.

Anyway I got the idea to make a kind of cash register for the kids, with a barcode reader to scan some empty stuff from the kitchen. Kind of two birds with one stone, making something for the kids, I thought, considering the amount of time my wife claims that I have spent in "my room" the last two months. :o

And everything started out quite well, with a Uno board and a USB shield. I snatched some code from a forum somewhere, and with some tweeking i got it to work with my barcode scanner. It printed the name of the grocery (hard coded in string arrays) on a 4 row character display, together with the price and the sum. All well until my i2c display suddenly died while everything was still under construction.

Somehow a Due and a 3.5" tft touch shield ended up in my mailbox about the same time, and all of a sudden plans where changed. Every cash register needs a touch screen, of course. So I kind of started all over, with a GUI. And the GUI part of it is going quite well, with touch buttons, sums, scrolling and stuff, once again with good help from google and all the info supplied with the GFX and Touchscreen libraries.

But then I connected the barcode scanner, and everything fell apart. Well, at least my plans for the cash register.. It's very hard to find any useful info about the native USB-port on the Arduino Due. Combining that with my very limited C++ knowledge = I'm stuck.

So my hope is that someone here can point me in the right direction, to get the barcode scanner going on the Due. (Yes, if I spend some time educating myself on C++ I might figure it out myself.. maybe, but I'm far too impatient).

The code below is more or less the code I snatched for the barcode scanner, and on a UNO with the USB shield this code prints a string with the barcode to the serial monitor.

#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <SPI.h>

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;
String       code = "";

class MyParser : public HIDReportParser {
  public:
    MyParser();
    void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  // If error, return
  // I don't know why it starts on 2, I just following the example
  if (buf[2] == 1) return;

  // If empty, return
  // I check on 2 because the previous if check on 2 too
  if (buf[2] == 0) return;

  // Like above, WHY it starts on 2 ?
  // What is the purpose of bit in 0 and 1 ?
    if(buf[2] == 39) {
      code = code + 0;
    } else if(buf[2] < 39) {
      code = code + (buf[2] - 29);
    } else if(buf[2] == 40) {
      Serial.println(code);
      code = "";     
    }
}

void setup() {
  Serial.begin( 115200 );
  Serial.println("Start");

  if (Usb.Init() == -1)
    Serial.println("OSC did not start.");

  delay( 200 );

  Hid.SetReportParser(0, &Parser);
}

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

The comments in the code aren't mine, and it's obvious that the guy writing the code had some issues with it also. If I've got it right I need to use the USBHost library to get the native USB port going, but because I don't really understand what's going on with the parsing part in the above code, im basically lost. (PS: I used to be a Java guy, so pointers make me dizzy) :confused:

Help, anyone...?

A bar code scanner can be used as a 1/TTL device or 2/as an USB HID device.

A few links that may help you out with this project:

1/ might be the simplest way to go

2/ is much more complex since you will have to write some code. There is a library for the DUE to connect a USB mouse or a USB keyboard, and another one to connect a USB joystick. The last one can give you some hints to write your own code for a specific bar code scanner.

Furthermore, since you will have to power your board with a battery pack , you have to know that, by default, the Native port will cease powering the USB Device connected due to UOTGVBOF bit. See the thread I provided to solve this issue.

https://forum.arduino.cc/index.php?topic=135399.0

Thank you for the google help! :slight_smile: I'll take a look at the links. But if I'm going to use it as a TTL device, the way to go will be to cut off the usb plug and connect the wires directly, right? :confused:

After googling my eyes out, I gave up the native USB port. I set up the scanner in RS232 mode with the supplied barcode, chopped off the USB plug from the scanner and connected the wires directly to the Due, and voila, it worked like a charm! :slight_smile:

Thanks for the guidance, guys!