3

serial monitor outputempty file generated

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

/*
  SD card read/write
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
 */

File myFile;
//SPIClass spiSD(HSPI);
String data = "";
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);
  char c = (char)ascii;
  data += c;
  Serial.print((char)ascii);
  //myFile.print((char)ascii);
}

void MyParser::OnScanFinished() {
   digitalWrite(10, HIGH);
  digitalWrite(4, LOW);
   delay(100);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");}  
  myFile = SD.open("barcode.txt",FILE_WRITE);
  myFile.println(data);
  Serial.println(data);
   data = "";
  Serial.println(" - Finished");
//  myFile.print('\n');
  //myFile.print('\0');
  myFile.close();
  digitalWrite(4, HIGH);
  digitalWrite(10, LOW);
   delay(100);

    }

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

void setup() { 
  pinMode(4, OUTPUT);
  pinMode(10, OUTPUT);
   SPI.begin();
   digitalWrite(4, HIGH);
   digitalWrite(10, HIGH);
  Serial.begin( 9600 );
  delay(100);

  digitalWrite(4, LOW);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
   delay(100);
  digitalWrite(4, HIGH);
   delay(100);
  digitalWrite(10, LOW);
  //Serial.println("Start");

  if (Usb.Init() == -1) {
    Serial.println("OSC did not start.");
  }
  delay( 200 );
  Hid.SetReportParser(0, &Parser);
   if (!SD.begin(4)) {
    Serial.println("initialization failed!");
}}

void loop() {
  // SPI.begin();
  Usb.Task();
} 

I am using a QuickScan Lite Qw210 Datalogic USB Barcode Reader, USB Host Shield and Arduino Uno I have coded everything and file is generating and barcode being scanned properly but prob is that file which is being generated is empty . Can anyone point out where the prob is happeining in my code why even after printing in file data is not getting savedserial monitor output


#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
File myFile;
String data = "";
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);
    char c = (char)ascii;
     data += c;
     Serial.print((char)ascii);
     //myFile.print((char)ascii);
     }

      void MyParser::OnScanFinished() {
          myFile = SD.open("barcode.txt",FILE_WRITE);
         myFile.println(data);
       Serial.println(data);
       data = "";
       Serial.println(" - Finished");
           myFile.close();
                }

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

          void setup() { 
        pinMode(4, OUTPUT);
           pinMode(10, OUTPUT);
            // SPI.begin();
               //digitalWrite(4, HIGH);
           //digitalWrite(10, HIGH);
           Serial.begin( 9600 );
            //delay(100);

                //digitalWrite(4, LOW);

               if (!SD.begin()) {
        Serial.println("initialization failed!");
         return;
               }
             delay(100);
            //digitalWrite(4, HIGH);
        //  delay(100);
     // digitalWrite(10, LOW);
       //Serial.println("Start");

            if (Usb.Init() == -1) {
              Serial.println("OSC did not start.");
            }
        delay( 200 );
         Hid.SetReportParser(0, &Parser);
        }

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

Did you mean this Juraj Pls have a look on changes and let me know

5
  • put SD.begin() only in setup. don't call SPI.begin(). don't set the CS pin of the SD card. let the library handle it. – Juraj Feb 16 '20 at 17:56
  • thanks for the suggestion . I just wanted to know then how my library will know that i have connected my sd card module on 4th pin of arduino and im using usb host shield also which is using the 10 pin i think. Thanks in advance – Vikas Pathak Feb 16 '20 at 20:22
  • And what about making pins high and low for chip select? shall I remove all of those digital write things from everywhere – Vikas Pathak Feb 16 '20 at 20:23
  • yes SD.begin(4). but in setup(), only once. the CS pin is handled by the SD library – Juraj Feb 17 '20 at 6:19
  • 1
    in setup() SD.begin(4) – Juraj Feb 21 '20 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.