?? usbhidiocdlg.cpp
字號:
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
DisplayLastError("CreateFile (ReadHandle): ");
//Get an event object for the overlapped structure.
/*API function: CreateEvent
Requires:
Security attributes or Null
Manual reset (true). Use ResetEvent to set the event object's state to non-signaled.
Initial state (true = signaled)
Event object name (optional)
Returns: a handle to the event object
*/
if (hEventObject == 0)
{
hEventObject = CreateEvent
(NULL,
TRUE,
TRUE,
"");
DisplayLastError("CreateEvent: ") ;
//Set the members of the overlapped structure.
HIDOverlapped.hEvent = hEventObject;
HIDOverlapped.Offset = 0;
HIDOverlapped.OffsetHigh = 0;
}
}
void CUsbhidiocDlg::ReadAndWriteToDevice()
{
//If necessary, find the device and learn its capabilities.
//Then send a report and request a report.
//Clear the List Box (optional).
//m_ResultsList.ResetContent();
DisplayData("***HID Test Report***");
DisplayCurrentTime();
//If the device hasn't been detected already, look for it.
if (MyDeviceDetected==FALSE)
{
MyDeviceDetected=FindTheHID();
}
// Do nothing if the device isn't detected.
if (MyDeviceDetected==TRUE)
{
switch (ReportType)
{
case 0:
{
// Output and Input Reports
//Write a report to the device.
WriteOutputReport();
//Read a report from the device.
ReadInputReport();
break;
}
case 1:
{
// Feature reports
//Write a report to the device.
WriteFeatureReport();
//Read a report from the device.
ReadFeatureReport();
break;
}
default:
{
break;
}
}
} // else
}
void CUsbhidiocDlg::ReadFeatureReport()
{
// Read a Feature report from the device.
CString ByteToDisplay = "";
BOOLEAN Result;
//The first byte is the report number.
FeatureReport[0]=0;
//Read a report from the device.
/*
HidD_GetFeature
Returns:
True on success
Requires:
A device handle returned by CreateFile.
A buffer to hold the report.
The report length returned by HidP_GetCaps in Capabilities.InputReportByteLength.
*/
if (DeviceHandle != INVALID_HANDLE_VALUE)
{
Result = HidD_GetFeature
(DeviceHandle,
FeatureReport,
Capabilities.FeatureReportByteLength);
DisplayLastError("HidD_GetFeature: ");
}
if (!Result)
{
//The read attempt failed, so close the handles, display a message,
//and set MyDeviceDetected to FALSE so the next attempt will look for the device.
CloseHandles();
DisplayData("Can't read from device");
MyDeviceDetected = FALSE;
}
else
{
DisplayData("Received Feature report: ");
DisplayFeatureReport();
}
}
void CUsbhidiocDlg::ReadInputReport()
{
// Retrieve an Input report from the device.
CString ByteToDisplay = "";
DWORD Result;
//The first byte is the report number.
InputReport[0]=0;
// Find out if the "Use Control Transfers Only" check box is checked.
UpdateData(true);
if (m_UseControlTransfersOnly)
{
//Read a report from the device using a control transfer.
/*
HidD_GetInputReport
Returns:
True on success
Requires:
A device handle returned by CreateFile.
A buffer to hold the report.
The report length returned by HidP_GetCaps in Capabilities.InputReportByteLength.
*/
if (ReadHandle != INVALID_HANDLE_VALUE)
{
Result = HidD_GetInputReport
(ReadHandle,
InputReport,
Capabilities.InputReportByteLength);
DisplayLastError("HidD_GetInputReport: ");
}
else
{
Result = FALSE;
}
if (!Result)
{
//The read attempt failed, so close the handles, display a message,
//and set MyDeviceDetected to FALSE so the next attempt will look for the device.
CloseHandles();
DisplayData("Can't read from device");
MyDeviceDetected = FALSE;
}
else
{
DisplayData("Received Input report: ");
//Display the report data.
DisplayInputReport();
/*
USHORT ByteNumber;
CHAR ReceivedByte;
//Display the received data in the log and the Bytes Received List boxes.
//Start at the top of the List Box.
m_BytesReceived.ResetContent();
//Step through the received bytes and display each.
for (ByteNumber=0; ByteNumber < Capabilities.InputReportByteLength; ByteNumber++)
{
//Get a byte.
ReceivedByte = InputReport[ByteNumber];
//Display it.
DisplayReceivedData(ReceivedByte);
}
*/
}
}
else
{
/*API call:ReadFile
'Returns: the report in InputReport.
'Requires: a device handle returned by CreateFile
'(for overlapped I/O, CreateFile must be called with FILE_FLAG_OVERLAPPED),
'the Input report length in bytes returned by HidP_GetCaps,
'and an overlapped structure whose hEvent member is set to an event object.
*/
if (ReadHandle != INVALID_HANDLE_VALUE)
{
Result = ReadFile
(ReadHandle,
InputReport,
Capabilities.InputReportByteLength,
&NumberOfBytesRead,
(LPOVERLAPPED) &HIDOverlapped);
}
DisplayLastError("ReadFile: ") ;
/*API call:WaitForSingleObject
'Used with overlapped ReadFile.
'Returns when ReadFile has received the requested amount of data or on timeout.
'Requires an event object created with CreateEvent
'and a timeout value in milliseconds.
*/
Result = WaitForSingleObject
(hEventObject,
6000);
DisplayLastError("WaitForSingleObject: ") ;
switch (Result)
{
case WAIT_OBJECT_0:
{
DisplayData("Received Input report,");
break;
}
case WAIT_TIMEOUT:
{
ValueToDisplay.Format("%s", "ReadFile timeout");
DisplayData(ValueToDisplay);
//Cancel the Read operation.
/*API call: CancelIo
Cancels the ReadFile
Requires the device handle.
Returns non-zero on success.
*/
Result = CancelIo(ReadHandle);
//A timeout may mean that the device has been removed.
//Close the device handles and set MyDeviceDetected = False
//so the next access attempt will search for the device.
CloseHandles();
DisplayData("Can't read from device");
MyDeviceDetected = FALSE;
break;
}
default:
{
ValueToDisplay.Format("%s", "Undefined error");
//Close the device handles and set MyDeviceDetected = False
//so the next access attempt will search for the device.
CloseHandles();
DisplayData("Can't read from device");
MyDeviceDetected = FALSE;
break;
}
}
/*
API call: ResetEvent
Sets the event object to non-signaled.
Requires a handle to the event object.
Returns non-zero on success.
*/
ResetEvent(hEventObject);
//Display the report data.
DisplayInputReport();
}
}
void CUsbhidiocDlg::RegisterForDeviceNotifications()
{
// Request to receive messages when a device is attached or removed.
// Also see WM_DEVICECHANGE in BEGIN_MESSAGE_MAP(CUsbhidiocDlg, CDialog).
DEV_BROADCAST_DEVICEINTERFACE DevBroadcastDeviceInterface;
HDEVNOTIFY DeviceNotificationHandle;
DevBroadcastDeviceInterface.dbcc_size = sizeof(DevBroadcastDeviceInterface);
DevBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
DevBroadcastDeviceInterface.dbcc_classguid = HidGuid;
DeviceNotificationHandle =
RegisterDeviceNotification(m_hWnd, &DevBroadcastDeviceInterface, DEVICE_NOTIFY_WINDOW_HANDLE);
}
void CUsbhidiocDlg::ScrollToBottomOfListBox(USHORT Index)
{
/*
Scroll to the bottom of the list box.
To do so, add a line and set it as the current selection,
possibly scrolling the window.
Then deselect the line,
leaving the list box scrolled to the bottom with nothing selected.
*/
m_ResultsList.SetCurSel( Index );
m_ResultsList.SetCurSel( -1 );
}
void CUsbhidiocDlg::WriteFeatureReport()
{
//Send a report to the device.
DWORD BytesWritten = 0;
INT Index =0;
ULONG Result;
CString strBytesWritten = "";
//The first byte is the report number.
FeatureReport[0]=0;
//Can set the other report values here, or get them from the combo boxes.
//OutputReport[1]=33;
//OutputReport[2]=6;
//Get the bytes to send from the combo boxes.
//If Autoincrement is checked, increment the selection.
if (m_cbutAutoIncrement.GetCheck()>0)
{
Index=m_cboByteToSend0.GetCurSel();
Index=Index+1;
m_cboByteToSend0.SetCurSel(Index);
}
if (m_cbutAutoIncrement.GetCheck()>0)
{
Index=m_cboByteToSend1.GetCurSel();
Index=Index+1;
m_cboByteToSend1.SetCurSel(Index);
}
//Get the values from the combo boxes.
FeatureReport[1]=m_cboByteToSend0.GetCurSel();
FeatureReport[2]=m_cboByteToSend1.GetCurSel();
//The first byte is the report number.
FeatureReport[0]=0;
//Send a report to the device.
/*
HidD_SetFeature
Sends a report to the device.
Returns: success or failure.
Requires:
A device handle returned by CreateFile.
A buffer that holds the report.
The Output Report length returned by HidP_GetCaps,
*/
if (DeviceHandle != INVALID_HANDLE_VALUE)
{
Result = HidD_SetFeature
(DeviceHandle,
FeatureReport,
Capabilities.FeatureReportByteLength);
}
DisplayLastError("HidD_SetFeature: ");
if (!Result)
{
//The write attempt failed, so close the handles, display a message,
//and set MyDeviceDetected to FALSE so the next attempt will look for the device.
CloseHandles();
DisplayData("Can't write to device");
MyDeviceDetected = FALSE;
}
else
{
DisplayData("A Feature report was written to the device.");
}
}
void CUsbhidiocDlg::WriteOutputReport()
{
//Send a report to the device.
DWORD BytesWritten = 0;
INT Index =0;
ULONG Result;
CString strBytesWritten = "";
UpdateData(true);
//The first byte is the report number.
OutputReport[0]=0;
//Can set the other report values here, or get them from the combo boxes.
//OutputReport[1]=33;
//OutputReport[2]=6;
//Get the bytes to send from the combo boxes.
//If Autoincrement is checked, increment the selection.
if (m_cbutAutoIncrement.GetCheck()>0)
{
Index=m_cboByteToSend0.GetCurSel();
Index=Index+1;
m_cboByteToSend0.SetCurSel(Index);
}
if (m_cbutAutoIncrement.GetCheck()>0)
{
Index=m_cboByteToSend1.GetCurSel();
Index=Index+1;
m_cboByteToSend1.SetCurSel(Index);
}
//Get the values from the combo boxes.
OutputReport[1]=m_cboByteToSend0.GetCurSel();
OutputReport[2]=m_cboByteToSend1.GetCurSel();
//The first byte is the report number.
OutputReport[0]=0;
if (m_UseControlTransfersOnly)
{
//Send a report to the device.
/*
HidD_SetOutputReport
Sends a report to the device.
Returns: success or failure.
Requires:
The device handle returned by CreateFile.
A buffer that holds the report.
The Output Report length returned by HidP_GetCaps,
*/
if (WriteHandle != INVALID_HANDLE_VALUE)
{
Result = HidD_SetOutputReport
(WriteHandle,
OutputReport,
Capabilities.OutputReportByteLength);
DisplayLastError("HidD_SetOutputReport: ");
}
if (Result)
{
DisplayData("An Output report was written to the device.");
}
else
{
//The write attempt failed, so close the handles, display a message,
//and set MyDeviceDetected to FALSE so the next attempt will look for the device.
CloseHandles();
DisplayData("Can't write to device");
MyDeviceDetected = FALSE;
}
}
else
{
/*
API Function: WriteFile
Sends a report to the device.
Returns: success or failure.
Requires:
A device handle returned by CreateFile.
A buffer that holds the report.
The Output Report length returned by HidP_GetCaps,
A variable to hold the number of bytes written.
*/
if (WriteHandle != INVALID_HANDLE_VALUE)
{
Result = WriteFile
(WriteHandle,
OutputReport,
Capabilities.OutputReportByteLength,
&BytesWritten,
NULL);
}
//Display the result of the API call and the report bytes.
DisplayLastError("WriteFile: ");
if (!Result)
{
//The WriteFile failed, so close the handles, display a message,
//and set MyDeviceDetected to FALSE so the next attempt will look for the device.
CloseHandles();
DisplayData("Can't write to device");
MyDeviceDetected = FALSE;
}
else
{
DisplayData("An Output report was written to the device.");
strBytesWritten.Format("%s%d", "Bytes Written: ", BytesWritten);
DisplayData(strBytesWritten);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -