⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optkeytestdlg.cpp

📁 键盘测试,数据系统操作键盘测试, 键盘测试,数据系统操作键盘测试,
💻 CPP
字号:
// OptKeyTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OptKeyTest.h"
#include "OptKeyTestDlg.h"

#include "crc.h"

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


#include "afxmt.h"

#define		WM_COMMRECVNOTIFY  WM_USER+100
#define		WM_COMMSENDNOTIFY  WM_USER+101
#define		WM_LED_NOTIFY		WM_USER+102

	DWORD WINAPI CommWatchThread(LPDWORD lpdwParam);
	HANDLE comThreadHwnd;

	HANDLE hCommDev;
	OVERLAPPED read_os;
	HANDLE postRecvEvent;
	HWND postRecvHwnd;

	OVERLAPPED write_os;
	HANDLE postSendEvent;
	HWND postSendHwnd;

	CEvent eventLed;

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

/////////////////////////////////////////////////////////////////////////////
// COptKeyTestDlg dialog

COptKeyTestDlg::COptKeyTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COptKeyTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COptKeyTestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void COptKeyTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COptKeyTestDlg)
	DDX_Control(pDX, IDC_AUTO, m_CAuto);
	DDX_Control(pDX, IDC_BAUND_KEY3, m_CBaund3);
	DDX_Control(pDX, IDC_BAUND_KEY2, m_CBaund2);
	DDX_Control(pDX, IDC_BAUND_KEY, m_CBaund);
	DDX_Control(pDX, IDC_PRESS_KEY, m_CPress);
	DDX_Control(pDX, IDC_SCAN_KEY, m_CScan);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COptKeyTestDlg, CDialog)
	//{{AFX_MSG_MAP(COptKeyTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_ALL_OPEN, OnAllOpen)
	ON_BN_CLICKED(IDC_ALL_CLOSE, OnAllClose)
	ON_BN_CLICKED(IDC_ALL_INV, OnAllInv)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_AUTO, OnAuto)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_COMMSENDNOTIFY,OnSend)
	ON_MESSAGE(WM_COMMRECVNOTIFY,OnRec)
	ON_MESSAGE(WM_LED_NOTIFY,OnLed )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COptKeyTestDlg message handlers

BOOL COptKeyTestDlg::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);
		}
	}

	// 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
	SetTimer( 0,500,NULL );

	tooltip.Create(this);
	tooltip.AddTool(this,"");
	tooltip.Activate( TRUE );
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COptKeyTestDlg::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 COptKeyTestDlg::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);
		dc.LineTo( 100,100 );
	}
	else
	{
		CPaintDC dc(this); // device context for painting

		U32 i;
		U32 j;
		U8 temp;

		for( i=0; i<7; i++ )
		{
			temp = 0x80;

			for( j=0; j<8; j++ )
			{
				if( cLedDat[i] & temp )
				{
					dc.FillSolidRect( 50+j*50,100+i*32,40,20,RGB( 255,0,0 ) );
				}
				else
				{
					dc.FillSolidRect( 50+j*50,100+i*32,40,20,RGB( 180,180,180 ) );
				}
				temp >>= 1;
			}
		}

		CDialog::OnPaint();
	
	}
}

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

int COptKeyTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	//接收
	postRecvEvent=CreateEvent(NULL,
		TRUE,
		TRUE,
		NULL);
	ASSERT(postRecvEvent);
	//发送
	postSendEvent=CreateEvent(NULL,
		TRUE,
		TRUE,
		NULL);
	ASSERT(postSendEvent);

	postRecvHwnd=m_hWnd;
	postSendHwnd=m_hWnd;


	hCommDev=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
	
	if( hCommDev == INVALID_HANDLE_VALUE )
	{
		MessageBox("不能打开通讯口!","错误",MB_ICONERROR);
		return FALSE;
	}
	DCB dcb;
	GetCommState(hCommDev,&dcb);
	
	dcb.BaudRate = 9600;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	
	
	SetCommState(hCommDev,&dcb);
	COMMTIMEOUTS CommTimeOuts;
	CommTimeOuts.ReadIntervalTimeout=100;
	CommTimeOuts.ReadTotalTimeoutMultiplier=500;
	CommTimeOuts.ReadTotalTimeoutConstant=1000;
	CommTimeOuts.WriteTotalTimeoutMultiplier=50;
	CommTimeOuts.WriteTotalTimeoutConstant=1000;
	
	SetCommTimeouts(hCommDev,&CommTimeOuts);
	
	PurgeComm(hCommDev,PURGE_TXCLEAR);
	PurgeComm(hCommDev,PURGE_RXCLEAR);
	
	memset(&read_os,0,sizeof(OVERLAPPED));
	read_os.hEvent=CreateEvent(NULL,
		TRUE,
		FALSE,
		NULL);
	ASSERT(read_os.hEvent);
	
	memset(&write_os,0,sizeof(OVERLAPPED));
	write_os.hEvent=CreateEvent(NULL,
		TRUE,
		FALSE,
		NULL);
	ASSERT(write_os.hEvent);
	
	DWORD dwThreadID;
	comThreadHwnd=CreateThread(NULL,
		0,
		(LPTHREAD_START_ROUTINE )CommWatchThread,
		NULL,
		0,
		&dwThreadID);
	return 0;
}

//串行口线程
DWORD WINAPI CommWatchThread(LPDWORD lpdwParam)
{
	DWORD dwEvtMask,dwTransfer;
	OVERLAPPED os;
	//HANDLE *hCommDev=(HANDLE*)lpdwParam;
	memset(&os,0,sizeof(os));
	os.hEvent=CreateEvent(NULL,
		TRUE,
		FALSE,
		NULL);
	ASSERT(os.hEvent);

	if(!SetCommMask(hCommDev,EV_RXCHAR))//|EV_TXEMPTY))
	{
		return FALSE;
	}
		
	for(;;)
	{
		dwEvtMask=0;

		if(!WaitCommEvent(hCommDev,&dwEvtMask,&os))
		{
			if(ERROR_IO_PENDING==GetLastError())
				GetOverlappedResult(hCommDev,&os,&dwTransfer,TRUE);
		}

		if((dwEvtMask&EV_RXCHAR)==EV_RXCHAR)
		{
			WaitForSingleObject(postRecvEvent,INFINITE);
			ResetEvent(postRecvEvent);
			::PostMessage(postRecvHwnd,WM_COMMRECVNOTIFY,NULL,NULL);
			
		}
		if((dwEvtMask&EV_TXEMPTY)==EV_TXEMPTY)
		{
			WaitForSingleObject(postSendEvent,0xffffffff);
			ResetEvent(postSendEvent);
			::PostMessage(postSendHwnd,WM_COMMSENDNOTIFY,NULL,NULL);
		}
		
	}
	CloseHandle(os.hEvent);
	return 0;
}

//接收
LRESULT COptKeyTestDlg::OnRec(WPARAM wParam,LPARAM lParam)
{

	char strRecvBuf[256];
	DWORD iRecvStrLen;
	DWORD dwErrorFlag;
	COMSTAT comStat;
	
	ClearCommError(hCommDev,&dwErrorFlag,&comStat);
	//DWORD dw;
	iRecvStrLen=comStat.cbInQue;
	
	if(iRecvStrLen>0)
	{
		BOOL bReturn=ReadFile(hCommDev,strRecvBuf,iRecvStrLen,&iRecvStrLen,&read_os);
		if(!bReturn)
		{
			if(ERROR_IO_PENDING==GetLastError())
			{
				if(WaitForSingleObject(read_os.hEvent,1000))
					iRecvStrLen=0;
			}
			MessageBox("Error on read File");
		}
		//char j=rxd_dptr;
		for(DWORD i=0;i<iRecvStrLen;i++)
		{
			cRxdBuf[cRxdCount++]=strRecvBuf[i];
		}

		if( cRxdCount>=4 )
		{//接收完成
			//测试接收正确
			if( crc.CheckCrc( cRxdBuf, 4 ) )
			{

				CString str;
				str.Format( "%02X", cRxdBuf[1] );
				switch( cRxdBuf[0] )
				{
				case 0:
					m_CScan.SetWindowText( str );
					break;
				case 1:
					m_CPress.SetWindowText( str );
					break;
				case 2:
					m_CBaund.SetWindowText( str );
					break;
				case 3:
					m_CBaund2.SetWindowText( str );
					break;
				case 4:
					m_CBaund3.SetWindowText( str );
					break;
				default:
					break;
				}
				//发送LED状态

			}
			cRxdCount = 0;
		}
	}
	SetEvent(postRecvEvent);
	return 0L;
}


//发送
LRESULT COptKeyTestDlg::OnSend(WPARAM wParam,LPARAM lParam)
{
	SetEvent(postSendEvent);
	return 0L;
}

//LED
LRESULT COptKeyTestDlg::OnLed( WPARAM wParam, LPARAM lParam )
{
	U8 i;
	for( i=0; i<7; i++ )
	{
		cTxdBuf[i] = cLedDat[i];
	}
	crc.AddCrc( cTxdBuf, 9 );
	DWORD dw;
	WriteFile(hCommDev,cTxdBuf,9,&dw,&write_os);
	return 0L;
}

void COptKeyTestDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CRect rect;
	U32 j;
	U32 i;

	for( i=0; i<7; i++ )
	{
		for( j=0; j<8; j++ )
		{
			rect.left = 50+j*50;
			rect.top = 100+i*32;
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 20;
			if( rect.PtInRect( point ) )
			{
				cLedDat[i] ^= (0x80>>j);
				::PostMessage(postSendHwnd,WM_LED_NOTIFY,NULL,NULL);
				this->InvalidateRect( rect );
			}
		}
	}

	CDialog::OnLButtonDown(nFlags, point);
}

void COptKeyTestDlg::OnAllOpen() 
{
	// TODO: Add your control notification handler code here
	U8 i;
	for( i=0; i<7; i++ )
	{
		cLedDat[i] = 0xff;
	}
	::PostMessage(postSendHwnd,WM_LED_NOTIFY,NULL,NULL);
	this->Invalidate();

}

void COptKeyTestDlg::OnAllClose() 
{
	// TODO: Add your control notification handler code here
	U8 i;
	for( i=0; i<7; i++ )
	{
		cLedDat[i] = 0x00;
	}
	::PostMessage(postSendHwnd,WM_LED_NOTIFY,NULL,NULL);
	this->Invalidate();
}

void COptKeyTestDlg::OnAllInv() 
{
	// TODO: Add your control notification handler code here
	U8 i;
	for( i=0; i<7; i++ )
	{
		cLedDat[i] ^= 0xff;
	}
	::PostMessage(postSendHwnd,WM_LED_NOTIFY,NULL,NULL);
	this->Invalidate();
}

void COptKeyTestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	//启动送
	static U32 cStep = 0;
	CRect rect;
	switch( nIDEvent )
	{
	case 0:
		::PostMessage(postSendHwnd,WM_LED_NOTIFY,NULL,NULL);	
		break;
	case 1:

		rect.left = 50+((cStep%8)*50);
		rect.top = 100+(cStep/8)*32;
		rect.right = rect.left + 40;
		rect.bottom = rect.top + 20;

		cLedDat[cStep/8] ^= (0x80>>(cStep%8));
		cStep++;
		cStep %= 56;
		this->InvalidateRect(rect);

		break;
	default:
		break;
	}
	CDialog::OnTimer(nIDEvent);
}

void COptKeyTestDlg::OnAuto() 
{
	// TODO: Add your control notification handler code here
	if( m_CAuto.GetCheck() )
	{
		SetTimer( 1, 1000, NULL );
	}
	else
	{
		KillTimer( 1 );
	}
}

void COptKeyTestDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
						
	
	CRect rect;
	U32 j;
	U32 i;
	BOOL flag = FALSE;

	for( i=0; i<7; i++ )
	{
		for( j=0; j<8; j++ )
		{
			rect.left = 50+j*50;
			rect.top = 100+i*32;
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 20;
			if( rect.PtInRect( point ) )
			{
				CString str;
				CString str1;
				tooltip.GetText( str1,this );
				str.Format( "%d, %d", i, j );
				if( str1 != str )
				{
					tooltip.UpdateTipText(str,this);
				}
				flag = TRUE;
				break;
			}
		}
		if( flag )
		{
			break;
		}
	}
	if( !flag )
	{
		CString str;
		tooltip.GetText( str,this );
		if(!str.IsEmpty())
		{
			tooltip.UpdateTipText("",this);
		}
	}

	CDialog::OnMouseMove(nFlags, point);
}

BOOL COptKeyTestDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(tooltip)
	{
		tooltip.RelayEvent(pMsg);
	}
	return CDialog::PreTranslateMessage(pMsg);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -