Reading and writing to USB (HID) interrupt endpoints on Mac

后端 未结 3 410
广开言路
广开言路 2020-12-08 01:43

I am attempting to communicate with a rather specific USB device and developing both Windows and Mac code to do so.

The device is a USB device with a HID interface

3条回答
  •  难免孤独
    2020-12-08 02:13

    I ran into this same kIOReturnExclusiveAccess. Instead of fighting it (building a kext, etc). I found the device and used the POSIX api's.

    //My funcation was named differently, but I'm using this for continuity..
    void DemiUSBDevice::device_attach_callback(void * context, 
        io_iterator_t iterator)
    {
    DeviceManager *deviceManager = (__bridge DADeviceManager *)context;
      io_registry_entry_t device;
      while ((device = IOIteratorNext(iterator))) {
    
        CFTypeRef prop;
        prop = IORegistryEntrySearchCFProperty(device,
                                               kIOServicePlane,
                                               CFSTR(kIODialinDeviceKey),
                                               kCFAllocatorDefault,
                                               kIORegistryIterateRecursively);
        if(prop){
          deviceManager->devPath = (__bridge NSString *)prop;
          [deviceManager performSelector:@selector(openDevice)];
        }
      }
    }
    

    once devPath is set you can call open and read/write..

    int dfd;
    dfd = open([devPath UTF8String], O_RDWR | O_NOCTTY | O_NDELAY);
      if (dfd == -1) {
        //Could not open the port.
        NSLog(@"open_port: Unable to open %@", devPath);
        return;
      } else {
        fcntl(fd, F_SETFL, 0);
      }
    

提交回复
热议问题