Printing to a HID device using a USB host shield

Hi, so i know this might be a bit unusual as most HID devices just need to send data that is then received by the Arduino but due to the nature of my project, i need it to receive data as well. To explain I’m using an Arduino UNO, and the HID device is a barcode scanner. The barcode scanner has a manual that when puts it in different modes. With other barcode scanners, I’ve been able to simple serial write to the rx pin of the scanner but unfortunately, i cannot with this setup. Is there a way to write to a HID using the USB host shield? My code is shown below, atm it is able to receive the barcodes that i scan fine but i need to write/print to it as well to put it in the correct more (the code that will be written to it will be strings)

//Modified from https://github.com/felis/USB_Host_Shield_2.0/issues/323 

#include <NeoSWSerial.h>
#include <AltSoftSerial.h>


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

AltSoftSerial altSerial; //RX = 8; TX= 9 ; Unusable PWM = 10;

String content;

class MyParser : public HIDReportParser {
  public:
    MyParser();
    void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
  protected:
    uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
    virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
    virtual void OnScanFinished();
};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  // If error or empty, return
  if (buf[2] == 1 || buf[2] == 0) return;

  for (uint8_t i = 7; i >= 2; i--) {
    // If empty, skip
    if (buf[i] == 0) continue;

    // If enter signal emitted, scan finished
    if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
      OnScanFinished();
    }

    // If not, continue normally
    else {
      // If bit position not in 2, it's uppercase words
      OnKeyScanned(i > 2, buf, buf[i]);
    }

    return;
  }
}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
  // Letters
  if (VALUE_WITHIN(key, 0x04, 0x1d)) {
    if (upper) return (key - 4 + 'A');
    else return (key - 4 + 'a');
  }

  // Numbers
  else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
    return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
  }

  return 0;
}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {
  uint8_t ascii = KeyToAscii(upper, mod, key);
  content = (char)ascii;
  Serial.print(content);
  altSerial.print(content);
}

void MyParser::OnScanFinished() {
  // Serial.println(" - Finished");
  Serial.println("");
}

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;

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

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

  delay( 200 );

  Hid.SetReportParser(0, &Parser);
  //content = ""; 
}

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

Software Serial cannot receive and send concurrently. If you are not doing that please include a schematic, not a frizzy thing, showing all the power, ground and interconnections. Also a link to the parts would also help.

gilshultz:
Software Serial cannot receive and send concurrently. If you are not doing that please include a schematic, not a frizzy thing, showing all the power, ground and interconnections. Also a link to the parts would also help.

I'm not using software serial to send data to the connected device (that is for something else). I do not have a schematic of the setup but I'll do my best to describe it.
I have a barcode scanner (mcr12) connected to a USB host shield that is on an Ardiuno UNO. The software serial is to send the received barcode to another ardiuno.
All i need to do is send some commands to the barcode scanner to put it in certain modes using this manual: https://cdn-shop.adafruit.com/product-files/1203/MCR12_Scanner_Manual.pdf

The scanner: https://uk.rs-online.com/web/p/sensor-development-tools/1245452/

(I've been told that you can treat the barcode scanner like a keyboard but not too sure)

You need to read pages 46 and 47 if that manul more carefully.

Oh, and read https://forum.arduino.cc/index.php?topic=556599.0

ieee488:
You need to read pages 46 and 47 if that manul more carefully.

Oh, and read https://forum.arduino.cc/index.php?topic=556599.0

I have read those pages as well as been to that link and it doesn't really help the problem that I am having

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.