📄 usbhostdlg.cpp
字号:
char completeDeviceName[64] = "";
char pcMsg[64] = "";
strcat (completeDeviceName,"\\\\.\\");
strcat (completeDeviceName,devname);
*phDeviceHandle = CreateFile(completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (*phDeviceHandle == INVALID_HANDLE_VALUE)
return (FALSE);
else
return (TRUE);
}
void CUsbhostDlg::OnDeviceButton()
{
// TODO: Add your control notification handler code here
USB_DEVICE_DESCRIPTOR output;
HANDLE hDevice = NULL;
BOOLEAN bResult = FALSE;
ULONG nBytes;
CString str;
if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
{
MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
hDevice = NULL;
return;
}
if (hDevice != NULL)
{
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
NULL,
0,
&output,
sizeof(USB_DEVICE_DESCRIPTOR),
&nBytes,
NULL);
}
if (bResult==TRUE)
{
m_UsbList.AddString("USB Device Descriptor");
str.Format(_T("bLength:0x%x"),output.bLength);
m_UsbList.AddString(str);
str.Format(_T("bDescriptorType:0x%x"),output.bDescriptorType);
m_UsbList.AddString(str);
str.Format(_T("bcdUSB:0x%x"),output.bcdUSB);
m_UsbList.AddString(str);
str.Format(_T("bDeviceClass:0x%x"),output.bDeviceClass);
m_UsbList.AddString(str);
str.Format(_T("bDeviceSubClass:0x%x"),output.bDeviceSubClass);
m_UsbList.AddString(str);
str.Format(_T("bDeviceProtocol:0x%x"),output.bDeviceProtocol);
m_UsbList.AddString(str);
str.Format(_T("bMaxPacketSize0:0x%x"),output.bMaxPacketSize0);
m_UsbList.AddString(str);
str.Format(_T("idVendor:0x%x"),output.idVendor);
m_UsbList.AddString(str);
str.Format(_T("idProduct:0x%x"),output.idProduct);
m_UsbList.AddString(str);
str.Format(_T("bcdDevice:0x%x"),output.bcdDevice);
m_UsbList.AddString(str);
str.Format(_T("iManufacturer:0x%x"),output.iManufacturer);
m_UsbList.AddString(str);
str.Format(_T("iProduct:0x%x"),output.iProduct);
m_UsbList.AddString(str);
str.Format(_T("iSerialNumber:0x%x"),output.iSerialNumber);
m_UsbList.AddString(str);
str.Format(_T("bNumConfigurations:0x%x"),output.bNumConfigurations);
m_UsbList.AddString(str);
}
else
MessageBox("读取设备描述符失败!", "错误", MB_ICONERROR | MB_OK);
CloseHandle (hDevice);
}
void CUsbhostDlg::OnConfigButton()
{
// TODO: Add your control notification handler code here
USB_CONFIGURATION_DESCRIPTOR output;
HANDLE hDevice = NULL;
BOOLEAN bResult = FALSE;
ULONG nBytes;
CString str;
if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
{
MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
hDevice = NULL;
return;
}
if (hDevice != NULL)
{
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
NULL,
0,
&output,
sizeof(USB_CONFIGURATION_DESCRIPTOR),
&nBytes,
NULL);
}
if (bResult==TRUE)
{
m_UsbList.AddString("USB Configuration Descriptor");
str.Format(_T("bLength:0x%x"),output.bLength);
m_UsbList.AddString(str);
str.Format(_T("bDescriptorType:0x%x"),output.bDescriptorType);
m_UsbList.AddString(str);
str.Format(_T("wTotalLength:0x%x"),output.wTotalLength);
m_UsbList.AddString(str);
str.Format(_T("bNumInterfaces:0x%x"),output.bNumInterfaces);
m_UsbList.AddString(str);
str.Format(_T("bConfigurationValue:0x%x"),output.bConfigurationValue);
m_UsbList.AddString(str);
str.Format(_T("iConfiguration:0x%x"),output.iConfiguration);
m_UsbList.AddString(str);
str.Format(_T("bmAttributes:0x%x"),output.bmAttributes);
m_UsbList.AddString(str);
str.Format(_T("MaxPower:0x%x"),output.MaxPower);
m_UsbList.AddString(str);
}
else
MessageBox("读取配置描述符失败!", "错误", MB_ICONERROR | MB_OK);
CloseHandle (hDevice);
}
void CUsbhostDlg::OnStartButton()
{
// TODO: Add your control notification handler code here
HANDLE hDevice = NULL;
BOOLEAN bResult = FALSE;
ULONG nBytes;
PUCHAR buffer;
ULONG bufferLength;
ISO_TRANSFER_CONTROL IsoControl;
if(g_KeepGoing)
{
MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
return;
}
if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
{
MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
return;
}
IsoControl.PipeNum = 1;
IsoControl.BufferCount = 2;
IsoControl.FramesPerBuffer = 8;
IsoControl.PacketSize = 16;
IsoControl.PacketCount = 16;
bufferLength = IsoControl.PacketCount * (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
buffer = (PUCHAR) malloc(bufferLength);
memset(buffer, 0, bufferLength);
*buffer=0x1A;
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESETPIPE,
&IsoControl.PipeNum,
sizeof(ULONG),
NULL,
0,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("USB复位端点8失败!");
CloseHandle(hDevice);
return;
}
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_ISO_WRITE,
&IsoControl,
sizeof(ISO_TRANSFER_CONTROL),
buffer,
bufferLength,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("启动AD1674失败!");
CloseHandle(hDevice);
return;
}
g_KeepGoing = true;
if(_beginthread(ReceiveThreadFunction, 0, hDevice) < 0)
{
AfxMessageBox("启动接收数据线程失败!");
}
}
void __cdecl ReceiveThreadFunction(HANDLE hDevice)
{
BOOLEAN bResult = FALSE;
ULONG nBytes;
ULONG count;
PUCHAR inBuffer = NULL;
ULONG inBufferLength;
PUCHAR ptrIn;
ISO_TRANSFER_CONTROL IsoControlIn;
PUSBD_ISO_PACKET_DESCRIPTOR IsoDescriptor;
unsigned short temp,TempMax,TempMin;
IsoControlIn.PipeNum = 4;
IsoControlIn.BufferCount = 2;
IsoControlIn.FramesPerBuffer = 8;
IsoControlIn.PacketSize = 32;
IsoControlIn.PacketCount = 32;
inBufferLength = IsoControlIn.PacketCount * (IsoControlIn.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
inBuffer = (PUCHAR) malloc(inBufferLength);
memset(inBuffer, 0, inBufferLength);
while (g_KeepGoing)
{
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESETPIPE,
&IsoControlIn.PipeNum,
sizeof(ULONG),
NULL,
0,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("USB复位端点10失败!");
goto IsoTransferDone;
}
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_ISO_READ,
&IsoControlIn,
sizeof(ISO_TRANSFER_CONTROL),
inBuffer,
inBufferLength,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("USB同步IN传输失败!");
goto IsoTransferDone;
}
IsoDescriptor = (PUSBD_ISO_PACKET_DESCRIPTOR) (inBuffer + (IsoControlIn.PacketCount * IsoControlIn.PacketSize));
count = 0;
for (ULONG i = 0; i < IsoControlIn.PacketCount; i++)
{
if (IsoDescriptor[i].Length)
{
count++;
if (count == 1)
ptrIn = inBuffer + (i * IsoControlIn.PacketSize);
}
}
if (count==16)
{
for (i=0;i<256;i++)
{
temp=ptrIn[2*i]*16+ptrIn[2*i+1];
if(temp>4096) temp=2048;
m_PointList[i].y = temp;
}
TempMax=TempMin=m_PointList[0].y;
for (i=1;i<256;i++)
{
if(m_PointList[i].y>TempMax)
TempMax=m_PointList[i].y;
else if(m_PointList[i].y<TempMin)
TempMin=m_PointList[i].y;
}
MaxValue=TempMax+100;
MinValue=TempMin-100;
CRect r;
r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
InvalidateRect(hWindow,r,TRUE);
}
}
IsoTransferDone:
CloseHandle(hDevice);
g_KeepGoing=false;
_endthread();
}
void CUsbhostDlg::OnStopButton()
{
// TODO: Add your control notification handler code here
HANDLE hDevice = NULL;
BOOLEAN bResult = FALSE;
ULONG nBytes;
PUCHAR buffer;
ULONG bufferLength;
ISO_TRANSFER_CONTROL IsoControl;
g_KeepGoing=false;
if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
{
MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
return;
}
IsoControl.PipeNum = 3;
IsoControl.BufferCount = 2;
IsoControl.FramesPerBuffer = 8;
IsoControl.PacketSize = 16;
IsoControl.PacketCount = 16;
bufferLength = IsoControl.PacketCount * (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
buffer = (PUCHAR) malloc(bufferLength);
memset(buffer, 0, bufferLength);
*buffer=0x2B;
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESETPIPE,
&IsoControl.PipeNum,
sizeof(ULONG),
NULL,
0,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("USB复位端点9失败!");
CloseHandle(hDevice);
return;
}
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_ISO_WRITE,
&IsoControl,
sizeof(ISO_TRANSFER_CONTROL),
buffer,
bufferLength,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("停止AD1674失败!");
CloseHandle(hDevice);
return;
}
CloseHandle(hDevice);
}
///////////////////////////////////////////////////////////////////
//
// 以下是系统运行相关函数。
//
///////////////////////////////////////////////////////////////////
void CUsbhostDlg::OnAboutButton()
{
// TODO: Add your control notification handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CUsbhostDlg::OnClearlistButton()
{
// TODO: Add your control notification handler code here
int count=m_UsbList.GetCount();
for (int i=0;i < count;i++)
{
m_UsbList.DeleteString( 0 );
}
}
void CUsbhostDlg::OnSaveButton()
{
// TODO: Add your control notification handler code here
if(g_KeepGoing)
{
MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
return;
}
CFileDialog SaveDlg(FALSE,"uad","*.uad");
if (SaveDlg.DoModal()==IDOK)
{
CFile SaveFile;
if(!SaveFile.Open(SaveDlg.GetPathName(),CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox("打开文件失败!");
return;
}
else
{
unsigned short* pBuf = new unsigned short[256];
CString str;
for(int i = 0; i < 256; i++)
{
pBuf[i] = m_PointList[i].y;
str.Format(_T("[%d]:0x%x"),i,pBuf[i]);
m_UsbList.AddString(str);
}
SaveFile.Write( pBuf, 512);
SaveFile.Close();
delete pBuf;
}
}
}
void CUsbhostDlg::OnCleargraphButton()
{
// TODO: Add your control notification handler code here
if(g_KeepGoing)
{
MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
return;
}
for(int i = 0; i < 256; i++)
m_PointList[i].y=0;
MinValue=0;
MaxValue=255;
CRect r;
r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
InvalidateRect(r,TRUE);
}
void CUsbhostDlg::OnReplayButton()
{
// TODO: Add your control notification handler code here
if(g_KeepGoing)
{
MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
return;
}
CFileDialog OpenDlg(TRUE,"uad","*.uad");
if (OpenDlg.DoModal()==IDOK)
{
CFile OpenFile;
if(!OpenFile.Open(OpenDlg.GetPathName(),CFile::modeRead))
{
AfxMessageBox("打开文件失败!");
return;
}
else
{
unsigned short* pBuf = new unsigned short[256];
CString str;
UINT nBytesRead = OpenFile.Read( pBuf, 512);
for(int i = 0; i < 256; i++)
{
m_PointList[i].y=pBuf[i];
str.Format(_T("[%d]:0x%x"),i,pBuf[i]);
m_UsbList.AddString(str);
}
unsigned short TempMax,TempMin;
TempMax=TempMin=m_PointList[0].y;
for (i=1;i<256;i++)
{
if(m_PointList[i].y>TempMax)
TempMax=m_PointList[i].y;
else if(m_PointList[i].y<TempMin)
TempMin=m_PointList[i].y;
}
MaxValue=TempMax+100;
MinValue=TempMin-100;
CRect r;
r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
InvalidateRect(r,TRUE);
OpenFile.Close();
delete pBuf;
}
}
}
void CUsbhostDlg::OnExitButton()
{
// TODO: Add your control notification handler code here
if(g_KeepGoing)
{
MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
return;
}
OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -