📄 usbhidiocdlg.cpp
字号:
ReadFile
Returns:
A report in InputReport.
The number of bytes actually read in BytesRead.
Requires:
A device handle returned by CreateFile.
The report length returned by HidP_GetCaps in Capabilities.InputReportByteLength.
*/
ActualBytesRead = BytesRead;
Result = ReadFile \
(ReadHandle, \
InputReport, \
Capabilities.InputReportByteLength, \
&BytesRead, \
NULL);
ActualBytesRead = BytesRead;
if (Result == 0)
{
//The ReadFile failed, so close the handle, display a message,
//and set DeviceDetected to FALSE so the next attempt will look for the device.
CloseHandle(ReadHandle);
DisplayData("Can't read from device");
DeviceDetected = FALSE;
}
DisplayCurrentTime();
DisplayInputReport();
}
//Exit the loop if the device is no longer detected or the user has clicked the close button.
while ((DeviceDetected == TRUE) && (ApplicationActive == TRUE));
return 0;
}
#include "math.h"
static int i = 0;
void CUsbhidiocDlg::WriteReport()
{
//Send a report to the device.
DWORD BytesWritten = 0;
INT Index0 =0,Index1 =0,Index2 =0,Index3 =0,Index4 =0;
CHAR OutputReport[5];
ULONG Result;
CString strBytesWritten = "";
//The first byte is the report number.
OutputReport[0]=0;
OutputReport[1]=0;
OutputReport[2]=0x0f;
OutputReport[3]=0x0f;
//Get the values from the combo boxes.
OutputReport[0]=m_cboByteToSend0.GetCurSel();
OutputReport[1]=m_cboByteToSend1.GetCurSel();
OutputReport[2]=m_cboByteToSend2.GetCurSel();
OutputReport[3]=m_cboByteToSend3.GetCurSel();
OutputReport[4]=m_cboByteToSend4.GetCurSel();
//Get the bytes to send from the combo boxes.
//If Autoincrement is checked, increment the selection.
if (m_cbutAutoIncrement0.GetCheck()>0)
{
Index0=m_cboByteToSend0.GetCurSel();
Index0=Index0+1;
m_cboByteToSend0.SetCurSel(Index0);
}
if (m_cbutAutoIncrement1.GetCheck()>0)
{
Index1=m_cboByteToSend1.GetCurSel();
Index1=Index1+1;
m_cboByteToSend1.SetCurSel(Index1);
}
if (m_cbutAutoIncrement2.GetCheck()>0)
{
Index2=m_cboByteToSend2.GetCurSel();
Index2=Index2+1;
m_cboByteToSend2.SetCurSel(Index2);
}
if (m_cbutAutoIncrement3.GetCheck()>0)
{
Index3=m_cboByteToSend3.GetCurSel();
Index3=Index3+1;
m_cboByteToSend3.SetCurSel(Index3);
}
if (m_cbutAutoIncrement4.GetCheck()>0)
{
Index4=m_cboByteToSend4.GetCurSel();
Index4=Index4+1;
m_cboByteToSend4.SetCurSel(Index4);
}
//OutputReport[3]=pow(2,i);
i++;
if (i==1)
{
OutputReport[1]=0x30;
}
if (i==2)
{
OutputReport[1]=0x00;
i=0;
}
/*
WriteFile
Sends a report to the device.
Returns: success or failure.
Requires:
The device handle returned by CreateFile.
The Output Report length returned by HidP_GetCaps,
A report to send.
*/
Result = WriteFile \
(DeviceHandle, \
OutputReport, \
Capabilities.OutputReportByteLength, \
&BytesWritten, \
NULL);
if (Result == 0)
{
//The WriteFile failed, so close the handle, display a message,
//and set DeviceDetected to FALSE so the next attempt will look for the device.
CloseHandle(DeviceHandle);
DisplayData("Can't write to device");
DeviceDetected = FALSE;
}
//Display the result of the API call and the report bytes.
DisplayLastError("WriteFile: ");
strBytesWritten.Format("%s%d", "Bytes Written: ", BytesWritten);
DisplayData(strBytesWritten);
}
/*
Display-related routines
*/
// 二进制倍
union Abb
{
char data;
struct wei
{
UINT a1:1;
UINT a2:1;
UINT a3:1;
UINT a4:1;
UINT a5:1;
UINT a6:1;
UINT a7:1;
UINT a8:1;
}X;
};
bool rec;
void CUsbhidiocDlg::DisplayInputReport()
{
USHORT ByteNumber;
CString MessageToDisplay = "";
CHAR ReceivedByte;
//Display the number of bytes read.
MessageToDisplay.Format("%s%d", "Number of Bytes Read: ", ActualBytesRead);
DisplayData(MessageToDisplay);
//Display the received data in the log and the Bytes Received List boxes.
//Start at the top of the List Box.
//m_BytesReceived.ResetContent();
/*
if (InputReport[2]==0x51)
{
rec=!rec;
}
//Step through the received bytes and display each.
//for (ByteNumber=0; ByteNumber < Capabilities.InputReportByteLength; ByteNumber++)
if (rec && (InputReport[2]!=0x11) && (InputReport[1]!=0x00))*/
// {
// if (InputReport[2]==0x51)
// {
// m_BytesReceived.ResetContent();
// }
// CString strRead;
// for (ByteNumber=0; ByteNumber < Capabilities.InputReportByteLength; ByteNumber++)
// {
// ReceivedByte = InputReport[ByteNumber];
// //Display it.
// CString strByteRead;
// //Convert the value to a 2-character Cstring.
// strByteRead.Format("%02X", ReceivedByte);
// strByteRead = strByteRead.Right(2);
// strRead+=strByteRead+" ";
// }
{
CString strRead, strByteRead; // 字节字符串
Abb unByte1, unByte2;
for (ByteNumber=0; ByteNumber < Capabilities.InputReportByteLength; ByteNumber++)
{
ReceivedByte = InputReport[ByteNumber];
//Display it.
CString strByteRead;
//Convert the value to a 2-character Cstring.
strByteRead.Format("%02X", ReceivedByte);
strByteRead = strByteRead.Right(2);
strRead+=strByteRead+" ";
if ( !(ByteNumber>0 && ByteNumber<3) )
continue;
// 得到第二个字节和第三个字节
ByteNumber == 1 ? unByte1.data = InputReport[ByteNumber] : unByte2.data = InputReport[ByteNumber];
}
strByteRead.Format(" %d%d%d%d %d%d%d%d ", // 字节顺序
unByte1.X.a7, unByte1.X.a8,
unByte2.X.a1, unByte2.X.a2, unByte2.X.a3,
unByte2.X.a5, unByte2.X.a6, unByte2.X.a7
);
strRead += strByteRead; // 连接二个串
//Display the value in the Bytes Received List Box.
CTime curTime = CTime::GetCurrentTime();
CString CurrentTime = curTime.Format( "%H:%M:%S" );
m_BytesReceived.InsertString(-1, strRead+" "+CurrentTime);
m_BytesReceived.SetCurSel(m_BytesReceived.GetCount()-1);
/*
//Get a byte.
ByteNumber=1;
{
CTime curTime = CTime::GetCurrentTime();
CString CurrentTime = curTime.Format( "%H:%M:%S" );
ReceivedByte = InputReport[ByteNumber];
//Display it.
CString strByteRead;
//Convert the value to a 2-character Cstring.
strByteRead.Format("%02X", ReceivedByte);
strByteRead = strByteRead.Right(2);
//Display the value in the Bytes Received List Box.
m_BytesReceived.InsertString(-1, strByteRead+" "+CurrentTime);
//DisplayReceivedData(ReceivedByte);
}
ByteNumber=2;
{
ReceivedByte = InputReport[ByteNumber];
//Display it.
DisplayReceivedData(ReceivedByte);
}
*/
}
}
void CUsbhidiocDlg::DisplayCurrentTime()
{
//Get the current time and date and display them in the log List Box.
CTime curTime = CTime::GetCurrentTime();
CString CurrentTime = curTime.Format( "%H:%M:%S, %B %d, %Y" );
DisplayData(CurrentTime);
}
void CUsbhidiocDlg::DisplayData(CString cstrDataToDisplay)
{
//Display data in the log List Box
USHORT Index;
Index=m_ResultsList.InsertString(-1, (LPCTSTR)cstrDataToDisplay);
ScrollToBottomOfListBox(Index);
}
void CUsbhidiocDlg::DisplayLastError(CString Operation)
{
//Display a message and the last error in the log List Box.
LPVOID lpMsgBuf;
USHORT Index = 0;
CString strLastError = "";
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL
);
//Display the last error.
strLastError = Operation + (LPCTSTR)lpMsgBuf;
//Trim CR/LF from the error message.
strLastError.TrimRight();
Index = m_ResultsList.InsertString(-1, strLastError);
ScrollToBottomOfListBox(Index);
LocalFree(lpMsgBuf);
}
void CUsbhidiocDlg::DisplayReceivedData(char ReceivedByte)
{
//Display data received from the device.
CString strByteRead;
//Convert the value to a 2-character Cstring.
strByteRead.Format("%02X", ReceivedByte);
strByteRead = strByteRead.Right(2);
//Display the value in the Bytes Received List Box.
m_BytesReceived.InsertString(-1, strByteRead);
//Display the value in the log List Box (optional).
//MessageToDisplay.Format("%s%s", "Byte 0: ", strByteRead);
//DisplayData(MessageToDisplay);
}
void CUsbhidiocDlg::OnChangeResults()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
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 );
}
/*
Misc. routines.
*/
void CUsbhidiocDlg::OnTimer(UINT nIDEvent)
{
//The timer event.
//Read and Write one pair of reports.
ReadAndWriteToDevice();
CDialog::OnTimer(nIDEvent);
}
void CUsbhidiocDlg::OnOK()
{
CDialog::OnOK();
}
void CUsbhidiocDlg::OnDblclkList2()
{
// TODO: Add your control notification handler code here
m_ResultsList.ResetContent();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -