• AVR Freaks

get serial number

Author
harleybg
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2007/12/30 12:06:53
  • Location: Sofia, Bulgaria
  • Status: offline
2008/09/08 03:57:03 (permalink)
0

get serial number

I`m writing a win32 console application. How can I get from my device its serial number?

There is a function in_mpusbapi.cpp for getting the device descriptor, but it`s not finished (it`s not working):
///////////////////////////////////////////////////////////////////////////////
//    MPUSBGetDeviceDescriptor : Returns the Device Descriptor Data
//
//  **** INCOMPLETE ****
//
//    Note that "input" and "output" refer to the parameter designations in calls
//    to this function, which are the opposite of common sense from the
//    perspective of an application making the calls.
//
DWORD MPUSBGetDeviceDescriptor(HANDLE handle,       // Input
                               PVOID pDevDsc,       // Output
                               DWORD dwLen,         // Input
                               PDWORD pLength)      // Output
{
    GET_DESCRIPTOR_PARAMETER dscParam;

    if(pLength != NULL)*pLength = 0;
    //if(pDevDsc == NULL) return MPUSB_FAIL;

    dscParam.bType =  USB_DEVICE_DESCRIPTOR_TYPE;
    if(!DeviceIoControl(handle,
                        IOCTL_MCHPUSB_GET_DESCRIPTOR,
                        &dscParam,
                        sizeof(GET_DESCRIPTOR_PARAMETER),
                        pDevDsc,
                        dwLen,
                        pLength,
                        NULL))
    {
        printf("Get dsc error: %d",GetLastError());
        return MPUSB_FAIL;
    }//end if

    return MPUSB_SUCCESS;


#1

3 Replies Related Threads

    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    RE: get serial number 2008/09/08 12:28:46 (permalink)
    0
    Hello harleybg,

    There are two methods to retrieve the serial number from USB device.
    a) Read it out directly from the device over the device driver (when the device driver supports it)
    b) Split it from the device instance ID (on Windows)

    You are trying a) approach.
    Sorry, I don't examine Microchip device driver yet, then I can't advise to this approach. I'll show b) approach here.

    Windows assigns unique device instance ID to each device to identify the device, and records it to the registry on the first enumeration. For USB device, the device instance ID consists of VID/PID and serial number, like

    USB\VID_vvvv&PID_pppp\serial_number

    where, vvvv, pppp: 4-digit HEX number for VID/PID

    "Device Identification Strings" on MSDN
    http://msdn.microsoft.com/en-us/library/ms791083.aspx

    Then, the serial number is retrieved from the registry as the device instance ID. SetupDiGetDeviceInstanceId() is the function to do it.

    This is a snippet to use this SetupDi-API.
    In this snippet, CDC devices and their COM ports are listed up.


    //
    // CDC_COM_port_lister.cpp
    // Snippet for command line tool (MS VC++ 2005)
    // List up COM port and serial number of CDC, currently connected to this PC
    //
    // Reference:
    // http://www.codeproject.com/KB/system/setupdi.aspx?df=100&forumid=4368&exp=0&select=479661
    // portlister.cpp : Defines the entry point for the console application.
    // by Christian B
    //

    #include "stdafx.h"
    #include "atlstr.h"
    #include "Windows.h"
    #include "SetupAPI.h"

    // Add "SetupAPI.Lib" to the Project > Linker > Input


    int _tmain(int argc, _TCHAR* argv[])
    {
    // device ID of PIC CDC
    // default VID/PID: 04D8/000A
    // for custom VID/PID, modify this string
    CString deviceID( _T("USB\\VID_04D8&PID_000A\\") );

    CString instanceID;
    CString serialNumer;
    CString portName;
    DWORD requiredSize;
    // Get Class GUID for PORTS class
    GUID classGuid[1];
    if ( ! SetupDiClassGuidsFromName( _T("PORTS"), classGuid, 1, &requiredSize ) )
    return 0;
    // List up PORTS class devices in present
    HDEVINFO hDevInfoSet = SetupDiGetClassDevs( classGuid, NULL, NULL, DIGCF_PRESENT );
    if ( hDevInfoSet == INVALID_HANDLE_VALUE )
    return 0;
    // Enumerate devices in the list
    SP_DEVINFO_DATA devInfo;
    devInfo.cbSize = sizeof( SP_DEVINFO_DATA );

    for ( DWORD deviceIndex = 0;
    SetupDiEnumDeviceInfo( hDevInfoSet, deviceIndex, &devInfo );
    deviceIndex++ )
    {
    // Get device instance ID
    SetupDiGetDeviceInstanceId( hDevInfoSet, &devInfo, NULL, 0, &requiredSize );
    SetupDiGetDeviceInstanceId( hDevInfoSet, &devInfo,
    instanceID.GetBuffer(requiredSize), requiredSize, NULL );
    instanceID.ReleaseBuffer();
    // Check device ID
    if( instanceID.Find( deviceID ) != 0 )
    continue;
    // Split serial number
    serialNumer = instanceID.Right( instanceID.GetLength() - deviceID.GetLength() );
    // Open device parameters reg key
    HKEY hkey = SetupDiOpenDevRegKey( hDevInfoSet, &devInfo, DICS_FLAG_GLOBAL,
    0, DIREG_DEV, KEY_READ );
    if ( hkey == INVALID_HANDLE_VALUE )
    continue;
    // Qurey for portname
    RegQueryValueEx( hkey, _T("PortName"), NULL, NULL, NULL, &requiredSize );
    RegQueryValueEx( hkey, _T("PortName"), NULL, NULL,
    (LPBYTE)portName.GetBuffer(requiredSize), &requiredSize );
    portName.ReleaseBuffer();
    // Close reg key
    RegCloseKey( hkey );
    // Print result
    _tprintf( _T("Port: %s Serial number: %s\n"), portName, serialNumer );
    }
    // Destroy device info list
    SetupDiDestroyDeviceInfoList( hDevInfoSet );

    return 0;
    }


    In this snippet, "PORTS" setup class is applied to narrow down the first selection for CDC devices. Other than this class, "USB", "HIDCLASS" will be useful. You'll find the setup class of the device at the top of the INF file (Class=....).

    "System-Supplied Device Setup Classes" on MSDN
    http://msdn.microsoft.com/en-us/library/ms791134.aspx

    - When no serial number is assigned to the device, the instance ID becomes the port location string like "5&15323D2E&0&1".

    - You can check the device instance ID on the Device Manager - Advanced tab

    Tsuneo
    #2
    lbodnar
    Super Member
    • Total Posts : 1148
    • Reward points : 0
    • Joined: 2005/12/18 06:06:09
    • Location: UK
    • Status: offline
    RE: get serial number 2008/09/09 04:26:43 (permalink)
    0
    ORIGINAL: chinzei

    Hello harleybg,

    There are two methods to retrieve the serial number from USB device.
    a) Read it out directly from the device over the device driver (when the device driver supports it)
    b) Split it from the device instance ID (on Windows)

    You are trying a) approach.
    Sorry, I don't examine Microchip device driver yet, then I can't advise to this approach. I'll show b) approach here.
    ...
    Tsuneo


    I would disagree and suggest using path a) only.

    Courtesy Jan Axelson:
    Extract serial numbers from device pathnames at your own risk. Microsoft warns, "Do not attempt to parse the device path symbolic name. The device path can be reused across system boots."


    Leo
    #3
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    RE: get serial number 2008/09/09 04:47:32 (permalink)
    0
    Of course, when the PC device driver supports to read out the serial number, it's better way. But not all device driver does.

    MS clearly documents that the instance ID contains the serial number.
    See this MSDN page.

    Device Identification Strings
    http://msdn.microsoft.com/en-us/library/ms791083.aspx

    Instance IDs
    An instance ID contains serial number information, if supported by the underlying bus, or some kind of location information.


    Anyway, device instance ID is not the symbolic link (device path), though the relation may be egg and chicken Smile

    "How can unique IDs for identical USB HIDs be obtained?"
    https://www.usb.org/phpbb/viewtopic.php?t=13890

    Tsuneo

    #4
    Jump to:
    © 2019 APG vNext Commercial Version 4.5