Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not get the product ID and vendor ID #121

Open
liyueMaster opened this issue Sep 19, 2017 · 6 comments
Open

Can not get the product ID and vendor ID #121

liyueMaster opened this issue Sep 19, 2017 · 6 comments
Assignees
Labels

Comments

@liyueMaster
Copy link

https://github.com/armadsen/ORSSerialPort/wiki/Getting-Vendor-ID-and-Product-ID
The method in this page can not get the product ID and vendor ID. IORegistryEntryCreateIterator got an error result and return nil for property.

@martinjbaker
Copy link

I'm seeing the same thing when I disconnect the USB device and connect it again. Works fine for the first connection.

@netizen01
Copy link

Anyone else been able to fix this?

@underscoreforbidden
Copy link

Based on https://stackoverflow.com/questions/34464672/make-a-list-of-serial-usb-device-with-specific-ids

- (int)VendorID
{
    // Variable declaration
    int vid;
    CFTypeRef cf_vendor;
    
    vid = -1;
    // Search properties among parents of the current port
    cf_vendor = IORegistryEntrySearchCFProperty(_IOKitDevice, kIOServicePlane,
                                                CFSTR(kUSBVendorID),
                                                kCFAllocatorDefault,
                                                kIORegistryIterateRecursively | kIORegistryIterateParents);
    
    // Decode & print VID
    if (cf_vendor && CFNumberGetValue(cf_vendor , kCFNumberIntType, &vid))
    {
        printf("\nUSB VID: %04X", vid);
    }
    
    // Release CFTypeRef
    if (cf_vendor)  CFRelease(cf_vendor);
    
    // Return vendor id
    return vid;
}
- (int)ProductID
{
    // Variable declaration
    int pid;
    CFTypeRef cf_product;
    
    pid = -1;
    // Search properties among parents of the current port
    cf_product = IORegistryEntrySearchCFProperty(_IOKitDevice, kIOServicePlane,
                                                CFSTR(kUSBProductID),
                                                kCFAllocatorDefault,
                                                kIORegistryIterateRecursively | kIORegistryIterateParents);
    
    // Decode & print PID
    if (cf_product && CFNumberGetValue(cf_product , kCFNumberIntType, &pid))
    {
        printf("\nUSB PID: %04X", pid);
    }
    
    // Release CFTypeRef
    if (cf_product)  CFRelease(cf_product);
    
    // Return product id
    return pid;
}

@dekosan
Copy link

dekosan commented Mar 13, 2019

Works fine for me as described by underscore forbidden but I experienced the same problem as martinjbakker: disconnecting / reconnecting the USB device gives a (null) PID/VID. So I included a workaround: restart the app when removing an USB device. I would be glad to see a solution for the cause of the problem which I couldn't find.

  • (void)relaunchAfterDelay:(float)seconds
    {
    NSTask *task = [[NSTask alloc] init];
    NSMutableArray *args = [NSMutableArray array];
    [args addObject:@"-c"];
    [args addObject:[NSString stringWithFormat:@"sleep %f; open "%@"", seconds, [[NSBundle mainBundle] bundlePath]]];
    [task setLaunchPath:@"/bin/sh"];
    [task setArguments:args];
    [task launch];
    [[NSApplication sharedApplication] terminate:nil];
    }
  • (void)serialPortsWereDisconnected:(NSNotification *)notification
    {
    NSArray *disconnectedPorts = [notification userInfo][ORSDisconnectedSerialPortsKey];
    NSLog(@"Ports were disconnected: %@", disconnectedPorts);
    [self postUserNotificationForDisconnectedPorts:disconnectedPorts];
    [self relaunchAfterDelay:0.1];
    }

@armadsen
Copy link
Owner

armadsen commented Apr 8, 2019

Unfortunately, I don't know why getting these fails after a disconnect/reconnect. I don't use the PID/VID for anything myself, so haven't noticed this. I'll look into it at some point, though.

@armadsen armadsen self-assigned this Apr 8, 2019
@armadsen armadsen added the bug label Apr 8, 2019
@KevinVitale
Copy link
Contributor

Should be fixed by #144.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants