// CH372UpDownDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CH372UpDown.h"
#include "CH372UpDownDlg.h"
#include <malloc.h>

#include "CH375DLL.H"
#pragma comment (lib,"CH375DLL")

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

ULONG mIndex;
HWND WndHandle; //窗体句柄
 // 设备事件通知回调程序
HANDLE DevHandle;
ULONG DownCount,UpCount,InterCount;
VOID CALLBACK CH375NOTIFYROUTINE( ULONG	iEventStatus );  // 设备事件和当前状态: 0=设备拔出事件, 3=设备插入事件
void EnumCH375Device();

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCH372UpDownDlg dialog

CCH372UpDownDlg::CCH372UpDownDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCH372UpDownDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCH372UpDownDlg)
	m_uplen1 = 0;
	m_uplen2 = 0;
	m_downdata2 = _T("");
	m_downlen2 = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_open = FALSE;
	mIndex = 0;
}

void CCH372UpDownDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCH372UpDownDlg)
	DDX_Control(pDX, IDC_BUTTON2_UP, m_btnup2);
	DDX_Control(pDX, IDC_BUTTON2_DOWN, m_btndown2);
	DDX_Control(pDX, IDC_BUTTON1_UP, m_btnup1);
	DDX_Control(pDX, IDC_LIST1_UPDATA, m_listup1);
	DDX_Text(pDX, IDC_EDIT1_UPLEN, m_uplen1);
	DDX_Text(pDX, IDC_EDIT2_UPLEN, m_uplen2);
	DDX_Text(pDX, IDC_EDIT2_DOWNDATA, m_downdata2);
	DDX_Text(pDX, IDC_EDIT2_DOWNLEN, m_downlen2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCH372UpDownDlg, CDialog)
	//{{AFX_MSG_MAP(CCH372UpDownDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2_DOWN, OnButton2Down)
	ON_BN_CLICKED(IDC_BUTTON2_UP, OnButton2Up)
	ON_BN_CLICKED(IDC_BUTTON1_UP, OnButton1Up)
	ON_BN_CLICKED(IDC_OpenCH375Device, OnOpenCH375Device)
	ON_BN_CLICKED(IDC_ClearD2Data, OnClearD2Data)
	ON_BN_CLICKED(IDC_ClearU2Data, OnClearU2Data)
	ON_BN_CLICKED(IDC_ClearU1Data, OnClearU1Data)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCH372UpDownDlg message handlers

BOOL CCH372UpDownDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    LONG i=0;
	char str[3]="";
	// 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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_trdup1 = FALSE;
	m_trdup2 = FALSE;
	m_trddown2 = FALSE;
	m_downdata2 = _T("");
	m_downlen2 = 0;
	m_uplen1 = 8;
	m_uplen2 = 64;

	WndHandle = this->m_hWnd ;
	T2DHandle = INVALID_HANDLE_VALUE;    //端点2下传设备打开句柄
	T2UHandle = INVALID_HANDLE_VALUE;    //端点2上传设备打开句柄
	T1Handle = INVALID_HANDLE_VALUE;	 //端点1上传设备打开句柄

	for(i=0;i<mCH375_MAX_NUMBER;i++){//填充设备序号(0-15)		
		itoa(i,str,10);
		::SendDlgItemMessage(WndHandle,IDC_DeviceIndex,CB_ADDSTRING,0,(LPARAM) (LPCTSTR)str);				
		if(CH375OpenDevice(i)!=INVALID_HANDLE_VALUE){//初始化时检测是否有已插入的CH372/CH375设备
			char tem[50]="";
			::sprintf(tem,"检测到有CH372/CH375设备已插入.");
			::SetDlgItemText(WndHandle,IDC_DeviceStatus,tem);
			CH375CloseDevice(i);
		}
	}
	::SendDlgItemMessage(WndHandle,IDC_DeviceIndex,CB_SETCURSEL,0,0); //初始化设备序号选项
	CH375SetDeviceNotify(0,NULL,CH375NOTIFYROUTINE);//启动指定序号设备插拔监视
	DevHandle = INVALID_HANDLE_VALUE; 
	DownCount=0;
	UpCount=0;
	InterCount=0;
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCH372UpDownDlg::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 CCH372UpDownDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCH372UpDownDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void ShowUpdata(PCHAR addbuf,ULONG AddLen){//向上传数据框内添加数据
	PUCHAR TextBuf;
	int TextLen;
	ULONG NewTextLen;
	TextLen = ::GetWindowTextLength(GetDlgItem(WndHandle,IDC_UPDATABuf));
	if(TextLen>0){
		NewTextLen = TextLen + AddLen +2;
		TextBuf = (PUCHAR)_alloca(NewTextLen);//程序退出后会自动释放内存
		if(TextBuf==NULL){
			MessageBox(WndHandle,"计算机没有足够内存","CH372DBG",0);
			return;
		}
		ZeroMemory(TextBuf,NewTextLen);
		::GetDlgItemText(WndHandle,IDC_UPDATABuf,(PCHAR)TextBuf,NewTextLen);
		::strcat((PCHAR)TextBuf,"\r\n");
		::strcat((PCHAR)TextBuf,addbuf);
		::SetDlgItemText(WndHandle,IDC_UPDATABuf,(PCHAR)TextBuf);
	}
	else{//当前数据框内没有数据
		::SetDlgItemText(WndHandle,IDC_UPDATABuf,addbuf);
	}	
	return;
}

void CCH372UpDownDlg::OnButton2Down()	//端点2下传
{
	UpdateData(TRUE);
	CWinThread * mTrdDown2 = NULL;
	if(!m_open)	{
		MessageBox("请先打开设备!","DEBUG372",MB_OK|MB_ICONSTOP);
		return;			
	}
	if(T2DHandle == INVALID_HANDLE_VALUE)
	{
		UCHAR DeviceName[128];
		memcpy(&DeviceName[0],CH375GetDeviceName(mIndex),sizeof(DeviceName));
		T2DHandle = CreateFile( (char *)&DeviceName[0], GENERIC_READ | GENERIC_WRITE,  // 打开设备
										FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
		if(T2DHandle == INVALID_HANDLE_VALUE)
		{
			MessageBox("无法打开设备！","DEBUG372",MB_OK|MB_ICONSTOP);
			return;
		}
	}
	
	//if(m_downlen2 == 0 || m_uplen2 > mCH375_PACKET_LENGTH*8*2 )
	if(m_uplen2 > mCH375_PACKET_LENGTH*64 )
	{
		MessageBox("批量下传数据长度一次最大为4096！","DEBUG372",MB_OK);
		return;
	}
	//线程句柄有效,说明上次线程已退出
	if(mTrdDown2 != INVALID_HANDLE_VALUE){
		CloseHandle(mTrdDown2); //释放线程句柄
		mTrdDown2 = NULL;
	}
    //建立线程
	mTrdDown2 = AfxBeginThread(mThreadDown2,this,THREAD_PRIORITY_NORMAL,0,0,NULL); 
	if(mTrdDown2)
	{
		m_trddown2 = TRUE;		//线程启动标志
	}
	else
	{
		MessageBox("端点2下传线程启动失败","DEBUG372",MB_OK|MB_ICONSTOP);
		m_trddown2 = FALSE;
		return;
	}
}

UINT mThreadDown2(LPVOID pParam)		//端点2下传线程
{
	ULONG dlen,datalen;
	UCHAR mBuf[mCH375_PACKET_LENGTH*8];

	CCH372UpDownDlg *pDlg = (CCH372UpDownDlg *)pParam;
	pDlg->m_btndown2.EnableWindow(FALSE);
	if(pDlg->m_open && pDlg->m_trddown2)
	{
		/**************在线程中用CreateFile打开设备,因为每个句柄同一时候只能执行一个API**************************/		
		if ( pDlg->T2DHandle != INVALID_HANDLE_VALUE ) 
		{
			datalen = strlen(pDlg->m_downdata2);
			if((LONG)datalen/2 > pDlg->m_downlen2)		//取数据长度与输入长度中偏小值
				datalen = pDlg->m_downlen2;		
			else{
				datalen = datalen/2;
				pDlg->m_downlen2 = datalen;
			}

			memcpy(mBuf,pDlg->m_downdata2,datalen*2);
			pDlg->mStrtoVal(&mBuf[0],datalen*2);
			dlen = datalen;			

			if(!CH375WriteData((ULONG)pDlg->T2DHandle,&mBuf[0],&dlen))
			{
				if ( !pDlg->m_close ) MessageBox(NULL,"端点2下传失败！如果设备断开过那么请先关闭本程序","DEBUG372",MB_OK|MB_ICONSTOP);
			}
			else{//更新长度显示
				char tem[50]="";
				DownCount=DownCount+dlen;
				sprintf(tem,"Down: %d",DownCount);
				SetDlgItemText(pDlg->m_hWnd,IDC_DownCount,tem);
			}
		}
	}
	pDlg->m_trddown2 = FALSE;
	pDlg->m_btndown2.EnableWindow(TRUE);
	//ExitThread(0);
	return 0;
}

void CCH372UpDownDlg::OnButton2Up()		//端点2上传
{
	CWinThread * mTrdUp2 = NULL;
	UpdateData(TRUE);
	if(!m_open)	{
		MessageBox("请先打开设备!","DEBUG372",MB_OK|MB_ICONSTOP);
		return;			
	}
	if(T2UHandle == INVALID_HANDLE_VALUE)
	{
		UCHAR DeviceName[128];
		memcpy(&DeviceName[0],CH375GetDeviceName(mIndex),sizeof(DeviceName));
		T2UHandle = CreateFile( (char *)&DeviceName[0], GENERIC_READ | GENERIC_WRITE,  // 打开设备
									FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
		if(T2UHandle == INVALID_HANDLE_VALUE)
		{
			MessageBox("无法打开设备！","DEBUG372",MB_OK|MB_ICONSTOP);
			return;
		}
	}
	if(m_uplen2 > mCH375_PACKET_LENGTH*8 )
	{
		MessageBox("上传长度无效！","DEBUG372",MB_OK);
		return;
	}
	//线程句柄有效,说明上次线程已自然退出
	if(mTrdUp2 != INVALID_HANDLE_VALUE){
		CloseHandle(mTrdUp2); //释放线程句柄
		mTrdUp2 = NULL;
	}
    //建立上传线程
	mTrdUp2 = AfxBeginThread(mThreadUp2,this,THREAD_PRIORITY_NORMAL,0,0,NULL); 
	if(mTrdUp2)
	{
		m_trdup2 = TRUE;		//线程启动标志
	}
	else
	{
		MessageBox("端点2上传线程启动失败","DEBUG372",MB_OK|MB_ICONSTOP);
		m_trdup2 = FALSE;
		return;
	}
	Sleep(10);
	UpdateData(FALSE);
}

UINT mThreadUp2(LPVOID pParam)		//端点2上传线程
{
	UCHAR mBuf[mCH375_PACKET_LENGTH*8];
	ULONG dlen,datalen;	
	CCH372UpDownDlg *pDlg = (CCH372UpDownDlg *)pParam;
	pDlg->m_btnup2.EnableWindow(FALSE);

	if(pDlg->m_open && pDlg->m_trdup2)		//设备打开，线程启动
	{
	/**************在线程中用CreateFile打开设备,因为每个句柄同一时候只能执行一个API**************************/
		if ( pDlg->T2UHandle != INVALID_HANDLE_VALUE ) {  // 打开设备成功	
			datalen = min( pDlg->m_uplen2, sizeof(mBuf));
			dlen = datalen;
			if(CH375ReadData((ULONG)pDlg->T2UHandle,&mBuf[0],&dlen)){//操作成功输出数据
				CHAR buffer[mCH375_PACKET_LENGTH*8*2+1];
				ULONG i,j;
				for(i=0,j=0;i<dlen;i++)
				{
					sprintf(&buffer[j],"%02X ",mBuf[i]);
					j += 3;
				}
				buffer[j] = '\0';
				ShowUpdata(buffer,j); //显示数据
				{//更新长度显示
					char tem[50]="";					
					UpCount=UpCount+dlen;
					sprintf(tem,"Up: %d",UpCount);
					SetDlgItemText(pDlg->m_hWnd,IDC_UpCount,tem);
				}
			}
			else if ( !pDlg->m_close ) MessageBox(NULL,"端点2上传失败！如果设备断开过那么请先关闭本程序","DEBUG372",MB_OK|MB_ICONSTOP);
		}
	}
	pDlg->m_trdup2 = FALSE;
	pDlg->m_btnup2.EnableWindow(TRUE);
	//ExitThread(0);	
	return 0;
}

void CCH372UpDownDlg::OnButton1Up()		//端点1上传
{
	CWinThread * mTrdUp1 = NULL;
	UpdateData(TRUE);
	if(!m_open)	{
		MessageBox("请先打开设备!","DEBUG372",MB_OK|MB_ICONSTOP);
		return;			
	}
	if(T1Handle == INVALID_HANDLE_VALUE)
	{
		UCHAR DeviceName[128];
		memcpy(&DeviceName[0],CH375GetDeviceName(mIndex),sizeof(DeviceName));
		T1Handle = CreateFile( (char *)&DeviceName[0], GENERIC_READ | GENERIC_WRITE,  // 打开设备
									FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
		if(T1Handle == INVALID_HANDLE_VALUE)
		{
			MessageBox("无法打开设备！","DEBUG372",MB_OK|MB_ICONSTOP);
			return;
		}
	}
	if((m_uplen1 > 8))
	{
		MessageBox("中断上传长度长度不大于8","DEBUG372",MB_OK);
		return;
	}
	//线程句柄有效,说明上次线程已退出
	if(mTrdUp1 != INVALID_HANDLE_VALUE){
		CloseHandle(mTrdUp1); //释放线程句柄
		mTrdUp1 = NULL;		
	}
    //建立线程
	mTrdUp1 = AfxBeginThread(mThreadUp1,this,THREAD_PRIORITY_NORMAL,0,0,NULL); 
	if(mTrdUp1)
	{
		m_trdup1 = TRUE;		//线程启动标志
	}
	else
	{
		MessageBox("端点1上传线程启动失败","DEBUG372",MB_OK|MB_ICONSTOP);
		m_trdup1 = FALSE;
		return;
	}
	Sleep(10);
	UpdateData(FALSE);
}

UINT mThreadUp1(LPVOID pParam)		//端点1上传线程
{
	UCHAR mBuf[mCH375_PACKET_LENGTH*8];
	ULONG dlen,datalen;

	CCH372UpDownDlg *pDlg = (CCH372UpDownDlg *)pParam;
	pDlg->m_btnup1.EnableWindow(FALSE);

	if(pDlg->m_open && pDlg->m_trdup1)
	{
		/**************在线程中用CreateFile打开设备,因为每个句柄同一时候只能执行一个API**************************/

		if ( pDlg->T1Handle != INVALID_HANDLE_VALUE ) 
		{
			datalen = pDlg->m_uplen1;
			dlen = datalen;
			if(CH375ReadInter((ULONG)pDlg->T1Handle,&mBuf,&dlen))
			{
				CHAR buffer[mCH375_PACKET_LENGTH*8*3+1];
				ULONG i,j;
				for(i=0,j=0;i<dlen;i++)
				{
					sprintf(&buffer[j],"%02X ",mBuf[i]);
					j += 3;
				}
				buffer[j] = '\0';
				SetDlgItemText(pDlg->m_hWnd,IDC_EDIT1_UPDATA,buffer);
				{
					char tem[50]="";
					InterCount=InterCount+dlen;
					sprintf(tem,"Up: %d",InterCount);
					SetDlgItemText(pDlg->m_hWnd,IDC_InterCount,tem);
				}				
			}
			else if ( !pDlg->m_close ) MessageBox(NULL,"端点1上传失败！如果设备断开过那么请先关闭本程序","DEBUG372",MB_OK|MB_ICONSTOP);
		}
	}
	pDlg->m_trdup1 = FALSE;
	pDlg->m_btnup1.EnableWindow(TRUE);
	//ExitThread(0);
	return 0;
}

PUCHAR CCH372UpDownDlg::mStrtoVal(PUCHAR str, ULONG strlen)
{
	ULONG i,j;
	ULONG len;
	UCHAR strRev[mMAX_BUFFER_LENGTH];
	if( strlen % 2 != 0 )
	{
		str[strlen] = 0;
		strlen += 1;
	}
	len = strlen / 2;
	for(i=0,j=0;i<strlen;i++,j++)
	{
		strRev[j] = (UCHAR)((mCharToBcd(str[i])<<4) + mCharToBcd(str[i+1]));
		i++;
	}
	strRev[j]='\0';
	memcpy(  str,strRev,len);
	return str;
}

UCHAR CCH372UpDownDlg::mCharToBcd(UCHAR iChar)
{
	UCHAR	mBCD;
	if ( iChar >= '0' && iChar <= '9' ) mBCD = iChar -'0';
	else if ( iChar >= 'A' && iChar <= 'F' ) mBCD = iChar - 'A' + 0x0a;
	else if ( iChar >= 'a' && iChar <= 'f' ) mBCD = iChar - 'a' + 0x0a;
//	else mBCD = 0xff;
	else mBCD = 0;
	return( mBCD );
}

void CCH372UpDownDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	mCloseWin();
	CDialog::OnClose();
}

//退出程序关闭设备、线程、句柄
void CCH372UpDownDlg::mCloseWin()
{
	m_close = TRUE;
	if(m_open)  // 退出未完成的线程,关闭设备
	{
		if(m_trddown2)
		{
			CH375AbortWrite(mIndex);
		}
		if(m_trdup2)
		{
			CH375AbortRead(mIndex);
		}
		if(m_trdup1)
		{
			CH375AbortInter(mIndex);
		}
		Sleep(50);
		if ( T2DHandle != INVALID_HANDLE_VALUE )
		{
			CloseHandle( T2DHandle );		//关闭端点2下传句柄
			T2DHandle = INVALID_HANDLE_VALUE;
		}
		if ( T2UHandle != INVALID_HANDLE_VALUE )
		{
			CloseHandle( T2UHandle );		//关闭端点2上传句柄
			T2UHandle = INVALID_HANDLE_VALUE;
		}
		if ( T1Handle != INVALID_HANDLE_VALUE )
		{
			CloseHandle( T1Handle );		//关闭端点1句柄
			T1Handle = INVALID_HANDLE_VALUE;
		}		
		CH375CloseDevice(mIndex);
		DevHandle = INVALID_HANDLE_VALUE; 
		m_open=FALSE;
		Sleep(20);
		::SetDlgItemText(WndHandle,IDC_OpenCH375Device,"打开设备&O");//等效于单击打开设备按钮
	}
}

//打开设备
void CCH372UpDownDlg::OnOpenCH375Device() 
{
	char WndTitle[50]="";
	ULONG DeviceID=0;
	if(m_open){ //设备已被打开,先关闭		
		mCloseWin();
		sprintf(WndTitle," 厂商ID:%04X,设备ID:%04X",0,0);
		::SetDlgItemText(WndHandle,IDC_DeviceInfor,WndTitle);
		m_open=FALSE;
	}
	else{
		mIndex = ::SendDlgItemMessage(WndHandle,IDC_DeviceIndex,CB_GETCURSEL,0,0);
		// 使用之前必须打开设备
		DevHandle = CH375OpenDevice( mIndex ) ;
		if (DevHandle == INVALID_HANDLE_VALUE ){  		
			m_open = FALSE;
			sprintf(WndTitle,"打开%d#设备失败!",mIndex);
			MessageBox(WndTitle,"DEBUG372",MB_OK|MB_ICONSTOP);		
			return;
		}
		m_open = TRUE;
		DeviceID = CH375GetUsbID(mIndex); //获取当前打开的USB设备ID,返回数据中,低16位为厂商ID,高16位为产品ID,错误时返回全0(无效ID)
		sprintf(WndTitle," 厂商ID:%04X,设备ID:%04X",LOWORD(DeviceID),HIWORD(DeviceID));
		::SetDlgItemText(WndHandle,IDC_DeviceInfor,WndTitle);
		sprintf(WndTitle,"关闭%d#设备(&C)",mIndex);
		::SetDlgItemText(WndHandle,IDC_OpenCH375Device,WndTitle);		
	}
	return;
}


BOOL CCH372UpDownDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	CH375SetDeviceNotify(mIndex,NULL,NULL);//关闭设备插拔通知
	if(m_open){ //关闭设备
		mCloseWin();//释放先前打开设备的资源		
	}
	return CDialog::DestroyWindow();
}

 /* 设备事件通知回调程序,在此程序里不宜作过多的操作,主要是因为中断服务程序优先级高,
	不宜进行USB传输，通常只是发个消息或者置个全局变量通知主程序处理。*/
VOID CALLBACK CH375NOTIFYROUTINE( 
	        	ULONG iEventStatus ){  //设备事件和当前状态: 0=设备拔出事件, 3=设备插入事件	
	char tem[100]="";
	if(iEventStatus==CH375_DEVICE_ARRIVAL){ //检测到设备插入事件
		::sprintf(tem," 检测到有CH372/CH375设备已插入.");
		::SetDlgItemText(WndHandle,IDC_DeviceStatus,tem);//等效于单击打开设备按钮
	}
	else if(iEventStatus==CH375_DEVICE_REMOVE){//检测到设备拔出事件
		::sprintf(tem," 检测到有CH372/CH375设备已拔出.");		
		::SetDlgItemText(WndHandle,IDC_DeviceStatus,tem);//等效于单击打开设备按钮		
		if(DevHandle != INVALID_HANDLE_VALUE)
			::SendDlgItemMessage(WndHandle,IDC_OpenCH375Device,BM_CLICK,0,0);//等效于单击打开设备按钮			
	}
	return;
}

//清空下传数据
void CCH372UpDownDlg::OnClearD2Data() 
{
	{//清除累计计数
		char tem[50]="";
		DownCount=0;
		sprintf(tem,"Down: %d",DownCount);
		::SetDlgItemText(m_hWnd,IDC_DownCount,tem);
	}

	this->m_downdata2="";
	UpdateData(FALSE);	
	return;
}

void CCH372UpDownDlg::OnClearU2Data() 
{	
	{//清除累计计数
		char tem[50]="";
		UpCount=0;
		sprintf(tem,"Up: %d",UpCount);
		::SetDlgItemText(m_hWnd,IDC_UpCount,tem);
	}

	::SetDlgItemText(WndHandle,IDC_UPDATABuf,"");	
	return;
}

void CCH372UpDownDlg::OnClearU1Data() 
{
	{//清除累计计数
		char tem[50]="";
		InterCount=0;
		sprintf(tem,"Down: %d",InterCount);
		::SetDlgItemText(m_hWnd,IDC_InterCount,tem);
	}
	SetDlgItemText(IDC_EDIT1_UPDATA,"");
	return;
}
