📄 usbdlg.cpp
字号:
// USBDlg.cpp : implementation file
//
#include "stdafx.h"
#include "USB.h"
#include "USBDlg.h"
//#include "main.h"
#include "ezusbsys.h"
#include "devioctl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern HANDLE m_hDevice;
BYTE data[1024];
bool threadController=0;
DWORD lenBytes=0;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//-----------------------
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUSBDlg dialog
CUSBDlg::CUSBDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUSBDlg::IDD, pParent)
{ Display_Scale=2;
//{{AFX_DATA_INIT(CUSBDlg)
m_COMBO1_VALUE = -1;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUSBDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUSBDlg)
DDX_Control(pDX, IDC_COMBO1, m_combo1);
DDX_Control(pDX, IDC_EDIT0, m_edit0);
DDX_CBIndex(pDX, IDC_COMBO1, m_COMBO1_VALUE);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUSBDlg, CDialog)
//{{AFX_MSG_MAP(CUSBDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_LED0, OnLed0)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_EDITLED, OnChangeEditled)
ON_EN_CHANGE(IDC_EDIT0, OnChangeEdit0)
ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUSBDlg message handlers
BOOL CUSBDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
IFLED=FALSE;
SetTimer(1,250,NULL);
//m_Pause.SetAlign(CButtonST::ST_ALIGN_VERT);
return TRUE; // return TRUE unless you set the focus to a control
}
void CUSBDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUSBDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CUSBDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUSBDlg::OnClose()
{
CDialog::OnClose();
}
void CUSBDlg::DrawWave(CDC *pDC)
{ CRect rect;
CString str;
int i;
int m_left,m_top,m_right,m_bottom;
int m_Interval = (1024-0)/10;
if (m_Interval<1)
m_Interval =1;
CWnd *pWnd = GetDlgItem(IDC_EDIT2);
pWnd->GetClientRect(&rect);
pDC->Rectangle(&rect);
m_left = rect.left+20;
m_top = rect.top+10;
m_right = rect.right-10;
m_bottom = rect.bottom-20;
int m_IntervalPan = (m_right -m_left)/11.0;
if (m_IntervalPan<1)
m_IntervalPan=1;
//创建画笔对象
CPen *pPenRed = new CPen;
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
CPen *pPenBlue = new CPen;
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0,255));
CPen *pPenGreen = new CPen;
pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));
CGdiObject * pOldPen = pDC->SelectObject(pPenRed);
//绘制坐标轴
pDC->MoveTo(m_left,m_top);
pDC->LineTo(m_left,m_bottom);
pDC->LineTo(m_right,m_bottom);
//写绘X刻度值和刻度
for(i=0;i<10;i++)
{ str.Format(_T("%d"),0+i*m_Interval*Display_Scale);
pDC->TextOut(m_left+i*(m_right-m_left)*10.0/m_Interval,m_bottom+3,str);
}
str.Format(_T("%d"),1024*Display_Scale);
pDC->TextOut(m_right-25,m_bottom+3,str);//pDC->TextOut(m_left+10*m_IntervalPan,m_bottom+3,str);
//写绘Y刻度值和刻度
str.Format(_T("%.1f"),0.6);
pDC->TextOut(m_left-20,m_bottom-6,str);
str.Format(_T("%.1f"),1.6);
pDC->TextOut(m_left-20,m_bottom-6-127.5/2.0,str);
str.Format(_T("%.1f"),2.6);
pDC->TextOut(m_left-20,m_bottom-6-255/2.0,str);
for(i=m_left;i<m_right-20;i+=5)
{
if((i&1)==0)
{
pDC->MoveTo(i+10,m_bottom);
pDC->LineTo(i+10,m_bottom+4);
}
else
{
pDC->MoveTo(i+10,m_bottom);
pDC->LineTo(i+10,m_bottom+2);
}
}
pDC->MoveTo(m_right-5,m_bottom-5);
pDC->LineTo(m_right,m_bottom);
pDC->LineTo(m_right-5,m_bottom+5);
pDC->MoveTo(m_left-5,m_top+5);
pDC->LineTo(m_left,m_top);
pDC->LineTo(m_left+5,m_top+5);
pDC->SelectObject(pPenGreen);
int iTemp = (m_bottom -m_top)/5;
for (i=1;i<=5;i++)
{
pDC->MoveTo(m_left,m_bottom-i*iTemp);
pDC->LineTo(m_right,m_bottom-i*iTemp);
}
int xTemp;
double yTemp;
pDC->SelectObject(pPenBlue);
for(i=0;i<1024;i++)
{ xTemp =(m_left+i*(m_right-m_left)/1024.0)/Display_Scale;
yTemp =m_bottom-bulk_buffer[i]/2.0;
if (yTemp<m_top)yTemp=m_top;
if((xTemp>=m_left)&&(xTemp<=m_right))
{
pDC->MoveTo(xTemp,m_bottom);
//pDC->LineTo(xTemp,yTemp);//以蓝色竖线长度表示取样值大小
pDC->SetPixel(xTemp,yTemp,RGB(0,0,0));//画实际曲线
}
}
pDC->SelectObject(pOldPen);
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
return;
}
//接收数据并显示
void CUSBDlg::OnOK()
{ BULK_TRANSFER_CONTROL myrequest;
//PUCHAR bulk_buffer = NULL;
char printbuffer1[6];
char printbuffer2[10240]="";
unsigned char *end;
float AD_Data[1024];
long average;
BOOLEAN bResult= FALSE;
ULONG bufferSize = 0;
myrequest.pipeNum=0;
//bulk_buffer =(unsigned char *)malloc(1024);
bufferSize=1024;
if (m_hDevice != NULL)
{
bResult = DeviceIoControl (m_hDevice,
IOCTL_EZUSB_BULK_READ,
&myrequest,
sizeof(BULK_TRANSFER_CONTROL),
bulk_buffer,
bufferSize,
(unsigned long *)&lenBytes,
NULL);
}
//保存数据
FILE *fp;
SYSTEMTIME time;
char save_buffer[10240];
GetLocalTime(&time); //获取本地时间
fp=fopen( "usb_data.txt ", "a+");
for(int k=0;k<1024;k++)
{ AD_Data[k]=0.6+bulk_buffer[k]*2/255.0+0.076;
}
if(fp!=NULL)
{ fprintf(fp,"Day:%d/%d/%d Time:%d:%d:%d\r\n",time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond);
//fprintf(fp,"Day:%d Time:\r\n",time.wHour);
for(int i=0;i<1024;i++)
fprintf(fp," %-6.3f",AD_Data[i]);//fprintf(fp," %-4d",bulk_buffer[i]);
//fwrite(bulk_buffer,sizeof(bulk_buffer),1,fp);
}
fclose(fp);
/* //保存数据到数据库
end = strmov(bulk_buffer,"INSERT INTO test_table values(");
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"What's this",11);
*end++ = '\'';
*end++ = ',';
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"binary data: \0\r\n",16);
*end++ = '\'';
*end++ = ')';
*/
//显示前处理
for(int l=0;l<1024;l++)
{
if(l%16==0)
{
if(l!=0)
strcat(printbuffer2,"\r\n"); //此处回车要用“/r/n”
sprintf(printbuffer1,"%4d",(l+1));
strcat(printbuffer2,printbuffer1);
}
sprintf(printbuffer1,"%6.3f",AD_Data[l]);
strcat(printbuffer2,printbuffer1);
}
m_edit0.SetWindowText(printbuffer2);//显示数据
//绘图
CRect rect;
CDC memDC; //定义内存DC
CBitmap memBitmap;
CWnd* pWnd =GetDlgItem(IDC_EDIT2);
pWnd->GetClientRect(&rect);
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
CBitmap* pOldBmp=NULL;
memDC.CreateCompatibleDC(pDC);
memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
pOldBmp=memDC.SelectObject(&memBitmap);
memDC.BitBlt(rect.left,rect.top,rect.right,rect.bottom,pDC,0,0,SRCCOPY);
DrawWave(&memDC);
pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&memDC,0,0,SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.DeleteDC();
memBitmap.DeleteObject();
//KillTimer(1);
//CDialog::OnOK();
}
void CUSBDlg::OnAbout() //开始转换
{ VENDOR_OR_CLASS_REQUEST_CONTROL myRequest;
char *buffer;
ULONG bufferSize = 0;
//char *pBuf;
BOOLEAN bResult= FALSE;
myRequest.request = (UCHAR) 0xB0;
buffer=NULL;
myRequest.value = (USHORT) 0;
myRequest.index = (USHORT) 0xBEEF;
myRequest.direction = 0;//0表示主机到设备
//bufferSize = 1;
//buffer = (char *)pBuf;
myRequest.requestType=2; // vendor specific request type (2)
myRequest.recepient=2; // recepient is device (0)/endpoint (2)
if (m_hDevice != NULL)
{
bResult = DeviceIoControl (m_hDevice,
IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
&myRequest,
sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
buffer,
bufferSize,
(unsigned long *)&lenBytes,
NULL);
}/* if valid driver handle */
//m_editstring=buffer;
//CAboutDlg dlg;
//dlg.DoModal();
}
void CUSBDlg::OnLed0()
{
// TODO: Add your control notification handler code here
/*
VENDOR_OR_CLASS_REQUEST_CONTROL myRequest;
char *buffer;
ULONG bufferSize = 0;
char *pBuf="a";
BOOLEAN bResult= FALSE;
myRequest.request = (UCHAR) 0xbc;
myRequest.value = (USHORT) 0;
myRequest.index = (USHORT) 0xBEEF;
myRequest.direction = 1;
bufferSize = 1;
buffer = (char *)pBuf;
myRequest.requestType=2; // vendor specific request type (2)
myRequest.recepient=0; // recepient is device (0)
if (m_hDevice != NULL)
{
bResult = DeviceIoControl (m_hDevice,
IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
&myRequest,
sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
buffer,
bufferSize,
(unsigned long *)&lenBytes,
NULL);
}//if valid driver handle
m_editstring=buffer;
if((char)buffer[0]>=0&&(char)buffer[0]<=9) (char)buffer[0]=(char)(buffer[0]+'0');
else (char)buffer[0]=(char)(buffer[0]+'A'-10);
GetDlgItem(IDC_EDITLED)->SetWindowText(CString(buffer[0]));
if (bResult==TRUE)
{
}
else
{
}
*/
}
void CUSBDlg::OnTimer(UINT nIDEvent)
{ /*
// TODO: Add your message handler code here and/or call default
OnLed0();
CDialog::OnTimer(nIDEvent);
*/
}
void CUSBDlg::OnChangeEditled()
{
// 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 CUSBDlg::OnChangeEdit0()
{
// 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 CUSBDlg::OnSelchangeCombo1()
{
int symbol;
UpdateData(true);
symbol=m_combo1.GetCurSel();
switch (symbol)
{ case 0:
Display_Scale=2;
break;
case 1:
Display_Scale=1;
break;
case 2:
Display_Scale=4;
break;
default:
break;
}
//更新绘图,以保证显示比例变化时实现动态更新
CRect rect;
CDC memDC; //定义内存DC
CBitmap memBitmap;
CWnd* pWnd =GetDlgItem(IDC_EDIT2);
pWnd->GetClientRect(&rect);
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
CBitmap* pOldBmp=NULL;
memDC.CreateCompatibleDC(pDC);
memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
pOldBmp=memDC.SelectObject(&memBitmap);
memDC.BitBlt(rect.left,rect.top,rect.right,rect.bottom,pDC,0,0,SRCCOPY);
DrawWave(&memDC);
pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&memDC,0,0,SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.DeleteDC();
memBitmap.DeleteObject();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -