?? usbhostdlg.cpp
字號:
pDC->DrawEdge(TotalRect,EDGE_SUNKEN,BF_RECT);
}
void CUsbhostDlg::DrawLab(CDC *pDC)
{
pDC->TextOut(m_InnerLWScreen-30,m_InnerUHScreen-27,"幅度值");
pDC->TextOut(m_InnerLWScreen/2-15 + m_InnerRWScreen/2,m_InnerDHScreen+ 20, "點數(shù)值");
char str[10];
for(int i=0;i<m_YLineStart.GetSize();i++)
{
sprintf(str,"%5.1f",(MaxValue-MinValue)/6.0*i+MinValue);
pDC->TextOut(m_InnerLWScreen-45,m_YLineStart[i]-10,str);
}
for(i=0;i<m_XLineStart.GetSize();i++)
{
sprintf(str,"%d",i*32);
pDC->TextOut(m_XLineStart[i]-5,m_InnerDHScreen+5,str);
}
pDC->TextOut(m_InnerLWScreen+150,m_InnerUHScreen-27,"波形顯示");
}
///////////////////////////////////////////////////////////////////
//
// 以下是AN2131相關(guān)函數(shù)。
//
///////////////////////////////////////////////////////////////////
BOOLEAN CUsbhostDlg::UsbOpenDriver (HANDLE *phDeviceHandle, PCHAR devname)
{
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("無效設(shè)備,請重試!", "錯誤", 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("讀取設(shè)備描述符失敗!", "錯誤", 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("無效設(shè)備,請重試!", "錯誤", 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;
unsigned char outValue;
BULK_TRANSFER_CONTROL bulkControl;
unsigned long input=8;
if(g_KeepGoing)
{
MessageBox("請首先結(jié)束當前線程!", "錯誤", MB_ICONERROR | MB_OK);
return;
}
if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
{
MessageBox("無效設(shè)備,請重試!", "錯誤", MB_ICONERROR | MB_OK);
return;
}
outValue=0x1A;
bulkControl.pipeNum = 6;
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_WRITE,
&bulkControl,
sizeof(BULK_TRANSFER_CONTROL),
&outValue,
1,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("啟動AD1674失敗!");
CloseHandle(hDevice);
return;
}
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESETPIPE,
&input,
sizeof(unsigned long),
NULL,
0,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("復位管道8失敗!");
CloseHandle(hDevice);
return;
}
g_KeepGoing = true;
if(_beginthread(ReceiveThreadFunction, 0, hDevice) < 0)
{
AfxMessageBox("啟動接收數(shù)據(jù)線程失敗!");
}
}
void __cdecl ReceiveThreadFunction(HANDLE hDevice)
{
BOOLEAN bResult = FALSE;
ULONG nBytes;
BULK_TRANSFER_CONTROL bulkControl;
unsigned char InBuffer[512];
unsigned short temp,TempMax,TempMin;
CString str;
while(g_KeepGoing)
{
nBytes = 0;
bulkControl.pipeNum = 8;
g_Transfering= FALSE;
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_READ,
&bulkControl,
sizeof(BULK_TRANSFER_CONTROL),
InBuffer,
512,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("接收數(shù)據(jù)失敗!");
goto exit;
}
if (nBytes==512)
{
for (int i=0;i<256;i++)
{
temp=InBuffer[2*i]*16+InBuffer[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);
}
}
exit:
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;
unsigned char outValue;
BULK_TRANSFER_CONTROL bulkControl;
unsigned long input=8;
g_KeepGoing=false;
if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
{
MessageBox("無效設(shè)備,請重試!", "錯誤", MB_ICONERROR | MB_OK);
return;
}
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_ABORTPIPE,
&input,
sizeof(unsigned long),
NULL,
0,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("撤銷管道8失敗!");
CloseHandle(hDevice);
return;
}
outValue=0x2B;
bulkControl.pipeNum = 5;
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_WRITE,
&bulkControl,
sizeof(BULK_TRANSFER_CONTROL),
&outValue,
1,
&nBytes,
NULL);
if (bResult != TRUE)
{
AfxMessageBox("停止AD1674失敗!");
CloseHandle(hDevice);
return;
}
CloseHandle(hDevice);
}
///////////////////////////////////////////////////////////////////
//
// 以下是系統(tǒng)運行相關(guān)函數(shù)。
//
///////////////////////////////////////////////////////////////////
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("請首先結(jié)束當前線程!", "錯誤", 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("請首先結(jié)束當前線程!", "錯誤", 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("請首先結(jié)束當前線程!", "錯誤", 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("請首先結(jié)束當前線程!", "錯誤", MB_ICONERROR | MB_OK);
return;
}
OnOK();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -