2

I'm working on a project that needs to use USB to pass serial messages between Arduinos (I can't use I2C or SPI for a few reasons – I especially need USB's support for hot-swapping and address allocation). So far I have the USB Host Shield set up on one Arduino Uno, connected to the standard USB port on another. It appears to be working, as can be seen when I run the board_qc:

The 'slave' Arduino is just running the following code:

void loop() {
  Serial.println("Hello");
  delay(1000);
}

On the master, I create a USB object from the USB Host library (https://github.com/felis/USB_Host_Shield_2.0).

USB Usb;

void setup() {
    Serial.begin(115200);
    Usb.Init();
}

void loop() {
    if (Usb.getUsbTaskState() == USB_STATE_RUNNING)
    {
        Usb.ForEachUsbDevice(&PrintAllData);
    }
} 

void PrintAllData(UsbDevice *pdev)
{
    UsbDeviceAddress adr;
    adr.devAddress = pdev->address.devAddress;
    Serial.println(adr.devAddress, HEX);

    uint8_t msg = 0;
    uint16_t len = 0;
    uint8_t rcode = Usb.inTransfer(adr.devAddress, pdev->epinfo->epAddr, &len, msg);

    if (rcode && rcode != hrNAK) {
     Serial.print(F("\r\nData rcv: "));
     Serial.print(rcode, HEX);
   } else if (len > 0) {
     Serial.print(F("\r\nData Packet: "));
     Serial.print(msg[0]);
   }
}

I'm not getting any data through though. Does anyone have any suggestions? Or know of any helpful sample code?

closed as off-topic by Chris Stratton, user31481, jose can u c, TisteAndii, Avamander Aug 20 '17 at 22:43

  • This question does not appear to be about Arduino, within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • It would be orders of magnitude simpler and cheaper to use the UART's possibly with a software protocol for addressing and tristating the drivers if you need to connect more than two. If you really wanted to use USB, you would have to do a lot more than that - you would need to actually implement CDC/ACM protocol or whatever the slave talks, and if it is using a bridge chip, configure the baud rate, etc. – Chris Stratton May 25 '15 at 21:09
  • @ChrisStratton Are there any good Arduino libraries you know of for creating a simple USB slave device? – tomw May 27 '15 at 19:01
  • An ordinary modern Arduino already is a simple USB slave device, implementing either a vendor-specific USB serial protocol or a standard CDC/ACM function for the same purpose. – Chris Stratton May 27 '15 at 19:11
  • 2
    Can I point out that UART and USB use the exact same pins on most boards? In fact, USB is implemented by sending messages from the main chip to the serial chip using UART on most boards. – Anonymous Penguin May 30 '15 at 12:37
  • 4
    I'm voting to close this old abandoned question because it is based on a fundamental misunderstanding of USB and Arduino, and after two years of inactivity the poster seems unlikely to return to either accept an answer or revise their plans to something actually practical. – Chris Stratton Aug 18 '17 at 13:46
0

Have you tried this lib's documentation? It looks like just doing init() and then checking the task state is not enough.

Looking at the source code makes me think you might want to add a call to the main task/state machine, e.g. add a call to Usb.Task(); to your loop, before your if statement.

Not the answer you're looking for? Browse other questions tagged or ask your own question.