Home > Software engineering >  USB, speaking, reading and writing function ReadFile problem
USB, speaking, reading and writing function ReadFile problem

Time:09-17

BOOL Cusbhid: : ReadDatafromHidDevice (int devicepid, int devicevid, cstrings * readdata)
{
Cstrings temp=NULL;
DWORD strSize=0, requiredSize=0;
BOOL result1, result2, success;
//define some variables, and then would use the
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
//PSP_DEVICE_INTERFACE_DETAIL_DATA test;
HIDD_ATTRIBUTES devAttr;
PHIDP_PREPARSED_DATA PreparsedData;
HIDP_CAPS "Capabilities;
//the first step: get deviceID
GUID deviceId;
HidD_GetHidGuid (& amp; DeviceId);

The second step://equipment information
HDEVINFO handle;
Handle=SetupDiGetClassDevs (& amp; DeviceId, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);//Get only HID devices

//the third step: the enumeration of all the equipment
//SetupDiEnumDeviceInterfaces ();
Result1=false;//define some variables
Result2=false;
Success=false;

int index=0;

Do
{
DeviceInterfaceData. CbSize=sizeof (SP_DEVICE_INTERFACE_DATA);
Result1=SetupDiEnumDeviceInterfaces (
Handle,
NULL,//IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL
& DeviceId,
Index++,
& DeviceInterfaceData//specified equipment information
);
If (result1)
{

//get the equipment detailed data (for detailed information on the required storage space)
SetupDiGetDeviceInterfaceDetail (
Handle,
& DeviceInterfaceData,
NULL,
0,
& StrSize,
NULL
);

RequiredSize=strSize;
DeviceInterfaceDetailData=https://bbs.csdn.net/topics/(PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc (requiredSize);
DeviceInterfaceDetailData - & gt; The cbSize=sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
DeviceInfoData. CbSize=sizeof (SP_DEVINFO_DATA);

//to get detailed data
SetupDiGetDeviceInterfaceDetail (
Handle,
& DeviceInterfaceData,
DeviceInterfaceDetailData,
StrSize,
& RequiredSize,
& DeviceInfoData
);

//get the device path (the most important part of the)
Temp=DeviceInterfaceDetailData - & gt; DevicePath;
Free (DeviceInterfaceDetailData);

HANDLE hCom=CreateFile (
Temp,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL
);
If (hCom==INVALID_HANDLE_VALUE) continue;

DevAttr. Size=sizeof (HIDD_ATTRIBUTES);

if (! HidD_GetAttributes (hCom, & amp; DevAttr))
{
The CloseHandle (hCom);
continue;
}
If ((devicepid!=devAttr. ProductID) | | (devicevid!=devAttr. VendorID))
{
The CloseHandle (hCom);
continue;
}
if(! HidD_GetPreparsedData (hCom, & amp; PreparsedData))
{
The CloseHandle (hCom);
continue;
}
if(! HidP_GetCaps (PreparsedData, & amp; "Capabilities))
{
The CloseHandle (hCom);
continue;
}
Int readsize="Capabilities. InputReportByteLength;
Unsigned long numbytesreturned;
Unsigned char * readbuffer=(unsigned char *) malloc (readsize);
Memset (readbuffer, 0, readsize);
OVERLAPPED OVERLAPPED;
Overlapped. HEvent=CreateEvent (NULL, TRUE, FALSE, NULL);
Overlapped. Offset=0;
Overlapped. OffsetHigh=0;
If (ReadFile (hCom, readbuffer, readsize, & amp; Numbytesreturned, & amp; Overlapped))
{
Free (readbuffer);
The CloseHandle (hCom);
Success=true;
break;
}
Free (readbuffer);
The CloseHandle (hCom);
}
} while (result1);

The return success.
}

The above is I need to open loop in the thread reads the specified HID device to upload data; But now I met two problems:
1, unsigned char * readbuffer=(unsigned char *) malloc (readsize); Dynamically allocated memory is ReadFile function error, the program crashes, why? Is the memory allocated small? But I use local define the size of an array is no problem
2, every time when ReadFile use synchronous read data to be able to successfully read (with NULL replace CrearFile FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED and ReadFile & amp; Overlapped), but when using asynchronous read occasionally can read data, but most of the time often can't read the data, why? Is to upload the data has not been performed to ReadFile performed what function will be emptied the buffer data, but in the ReadFile die also can't read data such as
The above problems are all is to ensure that there are data, IN trouble and have to explain, thank you

CodePudding user response:

You don't read every time to the device enumeration, a one-time deal?
Asynchronous read and write USB device connections in the initialization time and read, written incident response thread, according to the reading and writing incident response corresponding operation, patients
 
//create a USB device connection
Void CHidUsbThread: : BuildConnectUSB (cstrings devName)
{
//==========take the device handle===========
HDev=OpenDevice (devName);
If (hDev==INVALID_HANDLE_VALUE)
{
//handle to invalid
ErrorReport (DEV_HANDLE_NULL);
return;
}
//=======create USB reader thread=======
if(! BulidReadEvent ())
{
//read the event thread creation failed
ErrorReport (READ_EVENT_BULID_FAILED);
return;
}
//=======create USB write event thread=======
if(! BulidWriteEvent ())
{
//write event thread creation failed
ErrorReport (WRITE_EVENT_BULID_FAILED);
return;
}
//===========report equipment connection success============
ShowDeviceStatus (true);
}

CodePudding user response:

reference 1st floor wxhxj0268 response:
every time you don't read to the device enumeration, a one-time deal?
Asynchronous read and write USB device connections in the initialization time and read, written incident response thread, according to the reading and writing incident response corresponding operation, patients
 
//create a USB device connection
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull