CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Feb 2008
    Posts
    11

    Talking to USB devices

    Hi All!

    I try to communicate with USB devices. I successfully get a device path for calling CreateFile() to communicate with USB device. But I have trouble with calling ReadFile() and WriteFile(). The calling of these functions always end with 5 code error.

    I don't understand what I do wrong. Maybe the drivers of my devices just don't allow to communicate with device or I made a mistake in my code.

    Please help.

    There is my programming code:
    Code:
    void _tmain(int argc, _TCHAR* argv[])
    {
    	//GUID hidGuid = CLSID_NULL; // Disk Device.
    	//CLSIDFromString(L"{53f56307-b6bf-11d0-94f2-00a0c91efb8b}", &hidGuid);
    
    	GUID hidGuid = CLSID_NULL; // Human Interface Device (HID).
    	HidD_GetHidGuid(&hidGuid);
    
    	//GUID hidGuid = CLSID_NULL; // USB Device.
    	//CLSIDFromString(L"{36FC9E60-C465-11CF-8056-444553540000}", &hidGuid);
    
    	//GUID hidGuid = CLSID_NULL; // USB Raw Device.
    	//CLSIDFromString(L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}", &hidGuid);
    
    	HDEVINFO devInfo = SetupDiGetClassDevs(&hidGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
    
    	SP_DEVINFO_DATA devInfoData;
    	devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    
    	for(int i = 0; SetupDiEnumDeviceInfo(devInfo, i, &devInfoData); i++)
    	{
    		//printf("Iteration Number: %d\n", i);
    		//ShowDeviceProperty(devInfo, devInfoData);
    
    		SP_DEVICE_INTERFACE_DATA devInterfaceData;
    		devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    
    		for(int j = 0; SetupDiEnumDeviceInterfaces(devInfo, &devInfoData, &hidGuid, j, &devInterfaceData); j++)
    		{
    			DWORD bufferSize = 0;
    			SetupDiGetDeviceInterfaceDetail(devInfo, &devInterfaceData, NULL, 0, &bufferSize, &devInfoData);
    
    			PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(bufferSize);
    			pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    			
    			SetupDiGetDeviceInterfaceDetail(devInfo, &devInterfaceData, pDeviceInterfaceDetailData, bufferSize, &bufferSize, &devInfoData);
    
    			SECURITY_ATTRIBUTES securityAttributes;
    			memset(&securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
    			securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
    			securityAttributes.bInheritHandle = true;
    
    			HANDLE fileHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, 
    				&securityAttributes, OPEN_EXISTING, 0, NULL);
    
    			printf("File handle value %d\n", fileHandle);
    			if ((int)fileHandle != -1)
    			{
    				/*HIDP_CAPS Capabilities;
    				PHIDP_PREPARSED_DATA PreparsedData;
    				HidD_GetPreparsedData(fileHandle, &PreparsedData);
    				printf("HidD_GetPreparsedData error: %d\n", GetLastError());
    				HidP_GetCaps(PreparsedData, &Capabilities);*/
    				
    
    				HIDD_ATTRIBUTES	attributes;
    				memset(&attributes, 0, sizeof(HIDD_ATTRIBUTES));
    				attributes.Size = sizeof(HIDD_ATTRIBUTES);
    
    				HidD_GetAttributes(fileHandle, &attributes);
    				printf("Device attribute: %d, %d, %d\n", attributes.ProductID, attributes.VendorID, attributes.VersionNumber);
    
    				char bufRead[256] = {0};
    				DWORD countRead = 0;
    				if(!ReadFile(fileHandle, bufRead, 5, &countRead, NULL))
    				{
    					printf("Reading file error %d\n", GetLastError());
    				}
    				
    				char bufWrite[] = {'7', '8', '9', ' '};
    				DWORD countWritten = 0;
    				if(!WriteFile(fileHandle, bufWrite, 3, &countWritten, NULL))
    				{
    					printf("Writing file error %d\n", GetLastError());
    				}
    			}
    			else
    			{
    				printf("Error code: %d\n", GetLastError());
    			}
    
    			free(pDeviceInterfaceDetailData);
    		}
    
    		printf("____________________\n");
    	}
    
    	SetupDiDestroyDeviceInfoList(devInfo);
    }
    Thank you much for your consideration.
    Last edited by Askold; February 6th, 2009 at 05:22 AM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Talking to USB devices

    Please use code tags when posting code: http://www.codeguru.com/forum/misc.php?do=bbcode#code
    Once you edit your post and re-copy the code with indentation, folks will be more likely to look at it.

    Few things I see:
    * Check for errors on all API functions.
    * Try using NULL instead of the empty securityAttributes.
    * Once you do open a device, you typically call HidD_GetAttributes() first thing to see if it's the ProductId/VenderId of the device you intend to work with.
    * Second parameter to CreateFile, dwDesiredAccess, is zero (as it typically for HID devices), so don't expect ReadFile/WriteFile to work. You usually just use Hid functions once you get a handle.

    gg

  3. #3
    Join Date
    Mar 2009
    Posts
    1

    Re: Talking to USB devices

    Hey Askold, did you ever get that code to work ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured