Author Topic: Serial numbers  (Read 29998 times)

kittu

  • Member
  • ***
  • Posts: 16
Serial numbers
« on: February 14, 2011, 12:21:30 am »
hi..

can you please tell me how would your Each devices reports a unique
serial number during USB enumeration.

thank you

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Serial numbers
« Reply #1 on: February 14, 2011, 11:12:46 am »
Place a serial number in a string descriptor and specify it in the device descriptor.

Jan

kittu

  • Member
  • ***
  • Posts: 16
Serial numbers
« Reply #2 on: February 14, 2011, 11:07:17 pm »
reply to,
Place a serial number in a string descriptor and specify it in the device descriptor.
Jan


hi..

serial no is in string descriptor which should be hard coded in the firmware

so how serial no will change with every device?

in my case i use same firmware for all devices so serial no should be same for my all devices

so on what bases i will going to differentiate two devices from each other ?


thank you

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Serial numbers
« Reply #3 on: February 14, 2011, 11:21:58 pm »
Note that I split this off from the other unrelated topic.

If you can't store unique serial numbers, don't use them.

If I recall correctly, your devices are HIDs. Each HID will get a unique handle. The serial numbers just give you a way to identify a specific device among multiple, otherwise identical devices after power down, reboot, or remove and reattach.

Jan

Guido Koerber

  • Frequent Contributor
  • ****
  • Posts: 72
Re: Serial numbers
« Reply #4 on: February 15, 2011, 06:30:36 pm »
Putting a serial number into a device should not be that much of a problem. We do have multiple controllers that have unique serial numbers. What kind of device do you need? Maybe we have some product that would fit.

kittu

  • Member
  • ***
  • Posts: 16
Re: Serial numbers
« Reply #5 on: February 16, 2011, 12:06:05 am »
hi..

yes every micro controller has there own unique id thats right.

but how to read that id?

can we read that id when device get enumerated

thanks

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Serial numbers
« Reply #6 on: February 16, 2011, 12:29:33 am »
Every microcontroller doesn't necessarily have a unique ID.

To provide a USB serial number, place the serial number in a string descriptor and specify it in the device descriptor.

Retrieve the serial number with HidD_GetSerialNumberString.

Jan


kittu

  • Member
  • ***
  • Posts: 16
Re: Serial numbers
« Reply #7 on: February 16, 2011, 12:33:11 am »
Reply to,

Note that I split this off from the other unrelated topic.

If you can't store unique serial numbers, don't use them.

If I recall correctly, your devices are HIDs. Each HID will get a unique handle. The serial numbers just give you a way to identify a specific device among multiple, otherwise identical devices after power down, reboot, or remove and reattach.

Jan



hi..

now i got the different handles for every hid device & i can store all this information in one structure such as handle, vid, pid

so now i have this collected data of how many hid devices attached with cpu(only my hid devices with same vid pid&handles are different)

now problem is that i cant use this information

because my applications cant understand which device is for which application

please let me know how can i use this information?

thanks



Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Serial numbers
« Reply #8 on: February 16, 2011, 09:59:37 am »
If the devices are identical, and you have multiple applications that each must communicate with a specific device, you need to find a way to identify the device to the application. For example, each device can return a Feature report that identifies the device. Note that the devices will then no longer be identical.

If each application can communicate with any one of the devices, open the handles with exclusive access to exclude the other applications.

Jan

kittu

  • Member
  • ***
  • Posts: 16
Re: Serial numbers
« Reply #9 on: February 16, 2011, 11:04:14 pm »
hi..

from all above discussion

i get  the idea about how to do this

i can implement this first.

& i will ack  you on this

thanks

kittu

  • Member
  • ***
  • Posts: 16
Re: Serial numbers
« Reply #10 on: February 18, 2011, 06:12:40 am »
hi.

let me know detailed about the write file& read file
WriteFile(   hDevice, //this is device handle
      &outbuffer[0], //this is o/p buffer
          Capabilities.OutputReportByteLength, //what is the roll of this three parameter
          &numBytesReturned,
         (LPOVERLAPPED) &HidOverlapped);



ReadFile(hDevice,                     /* handle to device */
      &inbuffer[0],                  /* IN report buffer to fill */
      Capabilities.InputReportByteLength,                  /* input buffer size */
            &numBytesReturned,               /* returned buffer size */
      (LPOVERLAPPED) &HidOverlapped );                  /* long pointer to an OVERLAPPED structure */


because

as i say in my previous post i can seprate out the devices  on the bases of handle & i can store there information in structure

but problem is that

information can be retrieve of that handle which will 1st enumerate

as of i have the information of all other devices with there handle

but i cant able to read the data of all next enumerated devices

so can
Capabilities.OutputReportByteLength,
          &numBytesReturned,
         (LPOVERLAPPED) &HidOverlapped);

so can this parameters will affect on data read?

Thanks
« Last Edit: February 18, 2011, 06:56:21 am by kittu »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Serial numbers
« Reply #11 on: February 18, 2011, 10:23:52 am »
After you retrieve the handle for the first device, continue searching for additional devices and open a handle to each. You will need to modify my example code to do this.

>//what is the roll of this three parameter
Capabilities.OutputReportByteLength,  /* output buffer size */
        &numBytesReturned, /* number of bytes written */
        (LPOVERLAPPED) &HidOverlapped); /* long pointer to an OVERLAPPED structure */

See:

http://msdn.microsoft.com/en-us/library/aa365747%28VS.85%29.aspx

Jan


kittu

  • Member
  • ***
  • Posts: 16
Re: Serial numbers
« Reply #12 on: February 18, 2011, 11:21:03 pm »
Hi..

yes i can do the same as you say ,
using create file i can open handle of all the devices enumerated with my system
but i suupose to not get that where to close that handles
take a look on my find hid code.

BOOL FindTheHID1()
{
   WORD Stat, address,stat;
   USHORT wrbuff[2]={0};
   USHORT k,Lock;
    //char                        UsageDescription;
    char szLockType[4],strBuf[100]={0},lpBuff[500]={0};
   int                           MemberIndex = 0,i;
    int                           rc=0,sz=0;
   LONG                        Result;   
   HIDD_ATTRIBUTES                  Attributes;
   SP_DEVICE_INTERFACE_DATA         devInfoData;
   BOOL                        LastDevice = FALSE;
    
   
   PHIDP_PREPARSED_DATA HidParsedData ;
   Length = 0;
   detailData = NULL;
   DeviceHandle=NULL;
    MyDeviceDetected = FALSE;

    My_PAK = (LPMessage2)calloc(1,sizeof(LD));

   for(i=0;i<8;i++)
   {
     lockinfo = GlobalAlloc(GPTR,sizeof(LI));
   //HidHandle=NULL;
   }
  
   MyDeviceDetected = 0;
   /*
   API function: HidD_GetHidGuid
   Get the GUID for all system HIDs.
   Returns: the GUID in HidGuid.
   */

   HidD_GetHidGuid(&HidGuid);   
   
   /*
   API function: SetupDiGetClassDevs
   Returns: a handle to a device information set for all installed devices.
   Requires: the GUID returned by GetHidGuid.
   */
   
   hDevInfo=SetupDiGetClassDevs
      (&HidGuid,
      NULL,
      NULL,
      DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
      
   devInfoData.cbSize = sizeof(devInfoData);

   //Step through the available devices looking for the one we want.
   //Quit on detecting the desired device or checking all available devices without success.
   MemberIndex = 0;
   while(SetupDiEnumDeviceInterfaces
         (hDevInfo,
         0,
         &HidGuid,
         MemberIndex,
         &devInfoData))
   {
       
         if (!SetupDiGetDeviceInterfaceDetail
            (hDevInfo,
            &devInfoData,
            NULL,
            0,
            &Length,
            NULL))
         {
            //DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
         //   oDetail.Size = 5;   // hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
            
         //Allocate memory for the hDevInfo structure, using the returned Length.

         detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
         
         //Set cbSize in the detailData structure.

         detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
            if (SetupDiGetDeviceInterfaceDetail
            (hDevInfo,
            &devInfoData,
            detailData,
            Length,
            &Required,
            NULL))
            {  
               //sprintf(devicepath[MemberIndex]->data,"%s",detailData->DevicePath);
               sprintf(lockinfo[MemberIndex]->Path,"%s",detailData->DevicePath);

                   /*
               API function: CreateFile
               Returns: a handle that enables reading and writing to the device.
               Requires:
               The DevicePath in the detailData structure
               returned by SetupDiGetDeviceInterfaceDetail.
               */

                  //HidHandle[MemberIndex]
               UsbHandle=CreateFile
                  (lockinfo[MemberIndex]->Path,
                  0,
                  FILE_SHARE_READ|FILE_SHARE_WRITE,
                  (LPSECURITY_ATTRIBUTES)NULL,
                  OPEN_EXISTING,
                  0,
                  NULL);
            
               /*
               API function: HidD_GetAttributes
               Requests information from the device.
               Requires: the handle returned by CreateFile.
               Returns: a HIDD_ATTRIBUTES structure containing
               the Vendor ID, Product ID, and Product Version Number.
               Use this information to decide if the detected device is
               the one we're looking for.
               */

               //Set the Size to the number of bytes in the structure.

               Attributes.Size = sizeof(Attributes);

               /*Result = HidD_GetAttributes
                  (HidHandle[MemberIndex],
                  &Attributes);*/
                  Result = HidD_GetAttributes
                  (UsbHandle,
                  &Attributes);
               //Is it the desired device?
                       MyDeviceDetected = FALSE;

               if (Attributes.VendorID == VendorID)//in second itration of MemberIndex = 1will get to start my devices
               {                                        //(for first iteration atMemberIndex = 0;)i can get the attributes of my hid keyboard

                   if (Attributes.ProductID == HID_OLD || Attributes.ProductID == HID_NEW)
                  {
                     
                     MyDeviceDetected = FALSE;

                     ghDeviceNew = CreateFile(lockinfo[MemberIndex]->Path,
                                    GENERIC_READ | GENERIC_WRITE,
                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                                    NULL,
                                    OPEN_EXISTING,
                                    FILE_FLAG_OVERLAPPED,
                                    NULL);
                     lockinfo[MemberIndex]->DevHandle=ghDeviceNew;

                     //ghDeviceNew = HidHandle[MemberIndex];
                     /* 6) Get the Device VID & PID to see if it's the device we want */
                     //if(ghDeviceNew != INVALID_HANDLE_VALUE)
                     if(lockinfo[MemberIndex]->DevHandle!= INVALID_HANDLE_VALUE)
                     {
                        gHID_Flag = HID_NEW;

                        if(detailData != NULL)
                        free(detailData);

                     
                        /* get a handle to a buffer that describes the device's capabilities.  This
                        line plus the following two lines of code extract the report length the
                        device is claiming to support */
                        HidD_GetPreparsedData(lockinfo[MemberIndex]->DevHandle, &HidParsedData);
                        
                        /* extract the capabilities info */
                        HidP_GetCaps( HidParsedData ,&dev_Capabilities[MemberIndex]);
                        
                        /* Free the memory allocated when getting the preparsed data */
                        HidD_FreePreparsedData(HidParsedData);   

                        //return TRUE;   /* found HID device */
                        MyDeviceDetected = TRUE;

                     }
                     
                  }
               }
            }
         }
             MemberIndex++;
   }

       for(i=0;i<8;i++)//filling the structure with my information of all enumerated devices
      {
         if(lockinfo->DevHandle!=NULL)
         {
           set_ghDeviceNew(lockinfo->DevHandle);
           set_capabilities(dev_Capabilities);
           wrbuff[1]=SET_Fearture_Report(SentryRead,0x3e,00);
           wrbuff[0]=SET_Fearture_Report(SentryRead,0x3f,00);
           lockinfo->L_Serial=wrbuff[1];
           lockinfo->L_Ext=wrbuff[0];
           memcpy(&lockinfo->L_Serial,&wrbuff[1],8);
                          memcpy(&lockinfo->L_Ext,&wrbuff[0],8);
                     
   }
      }
      /*for(i=0;i<8;i++)
      {
         GlobalFree(lockinfo);
      }*/
          
      /* free HID device info list resources */
   SetupDiDestroyDeviceInfoList(hDevInfo);
   /* for(i=0;i<5;i++)
    {
         if(HidHandle != INVALID_HANDLE_VALUE)
         {
            CloseHandle(ghDeviceNew);
         }
    }*/
   return MyDeviceDetected;

}
« Last Edit: February 18, 2011, 11:22:50 pm by kittu »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Serial numbers
« Reply #13 on: February 19, 2011, 09:32:40 am »
You close the handle(s) when you're finished communicating with the device.

Jan

kittu

  • Member
  • ***
  • Posts: 16
Re: Serial numbers
« Reply #14 on: March 08, 2011, 06:17:48 am »
Reply to,

After you retrieve the handle for the first device, continue searching for additional devices and open a handle to each. You will need to modify my example code to do this.

HI..

please let me know how can i modify your code ?

so i get the solution.



Thank you.
« Last Edit: March 08, 2011, 06:20:24 am by kittu »