How to read / write on two devices on USB hub via USB host shield?

Hello all,
I operate an Arduino Minima with a keyestudio USB host shield 1.5. I connected a USB hub and I am trying to read or write from/to a thumb drive and an SD-card that are both connected to the hub. I am using the USB_Host_Shield_Library_2.0. So far, I can only access the storage that is located on the port with the lowest port number. I can understand that without selecting a specific port on purpose might result in writing always to the same port.

What is needed to add to my code to access the other storage for read/write actions or to access specific ports on purpose?

This is the code that I adapted from the hub_demo example:

#include <usbhub.h>
#include <Usb.h>
#include <UsbFat.h>
#include <masstorage.h>
#include "pgmstrings.h"

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

USB Usb;            
USBHub Hub1(&Usb); 

BulkOnly bulk(&Usb), bulk1(&Usb); 

// File system.
UsbFat thumb(&bulk);
UsbFat SDC(&bulk1); 
// Test file.
File file;  

uint32_t next_time; 
oid setup() {
  Serial.begin(115200);
#if !defined(__MIPSEL__)
  while (!Serial)
    ;  // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  Serial.println("Start");

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

  delay(200);

  next_time = (uint32_t)millis() + 1000;
}

void loop() {
  Usb.Task();

  if (Usb.getUsbTaskState() == USB_STATE_RUNNING) {
    if ((int32_t)((uint32_t)millis() - next_time) >= 0L) {

      if (!thumb.begin()) {  
        Serial.println("thumb.begin failed");
      }
      delay(200);

      file.open("testfile.txt", O_CREAT | O_RDWR);
      file.println("Hello USB-Stick von thumb");
      file.close();
      file.flush();
      Serial.println("Writing Done");

      file.open("testfile.txt", O_CREAT | O_RDWR);
      char readout[50] = { 0 };
      int text;
      int i = 0;
      while (file.available()) {
        readout[i] = file.read();
        i++;
      }
      file.close();
      file.flush();
      for (int j = 0; j < i; j++) {
        Serial.print(readout[j]);
      }
      Serial.println("Reading Done");
      while (1)
        ;  //stop
    }
  }
}

I am quite new to controller programming and I might overlook obvious items. Therefore, I am looking for help.

I would check the sample code, and read the hdr file for clues.

1 Like

Thanks for your answer. Can you please explain what a hdr file is?

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