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

📄 commview.cpp

📁 wince 5.0下的串口调试程序和源代码。
💻 CPP
字号:
// CommView.cpp : implementation of the CCommView class
//

#include "stdafx.h"
#include "Comm.h"

#include "CommDoc.h"
#include "CommView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCommView
//////
HANDLE  hPort;
HANDLE  hReadThread;
DCB     pDCB;
CString strR,str;
UINT ReadThread(LPVOID lpvoid);
HWND  m_hWnd;
BOOL  fThread;
const  WM_COMM_RXCHAR=WM_USER+1;
//////////////

IMPLEMENT_DYNCREATE(CCommView, CView)

BEGIN_MESSAGE_MAP(CCommView, CView)
	//{{AFX_MSG_MAP(CCommView)
	ON_COMMAND(ID_AUTO_RECEIVE, OnAutoReceive)
	ON_COMMAND(ID_SEND, OnSend)
	ON_COMMAND(ID_RECEIVE, OnReceive)
	ON_COMMAND(ID_AUTO_SEND, OnAutoSend)
	ON_WM_TIMER()
	ON_MESSAGE(WM_COMM_RXCHAR, ShowData)
	ON_COMMAND(ID_STOP_SEND, OnStopSend)
	ON_COMMAND(ID_STOP_RECEIVE, OnStopReceive)
	ON_COMMAND(ID_TEST_RECEIVE, OnTestReceive)
	ON_COMMAND(ID_CLOSE_THREAD, OnCloseThread)
	ON_COMMAND(ID_MONITOR, OnMonitor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommView construction/destruction

CCommView::CCommView()
{
	// TODO: add construction code here

}

CCommView::~CCommView()
{
}

BOOL CCommView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCommView drawing

void CCommView::OnDraw(CDC* pDC)
{
	CCommDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CCommView diagnostics

#ifdef _DEBUG
void CCommView::AssertValid() const
{
	CView::AssertValid();
}

void CCommView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CCommDoc* CCommView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCommDoc)));
	return (CCommDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCommView message handlers

void CCommView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
		hPort=CreateFile(L"COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
	if(hPort==INVALID_HANDLE_VALUE)
		MessageBox(L"Can not Open Port!");
	else 
	{
	//	m_hWnd=GetSafeHwnd();
    //   AfxBeginThread(ReadThread,m_hWnd);
	  MessageBox(L"Port Opened !");
	}
	pDCB.DCBlength=sizeof(DCB);
	GetCommState(hPort,&pDCB);
	pDCB.BaudRate=9600;
	pDCB.ByteSize=8;
	pDCB.Parity=NOPARITY;
	pDCB.StopBits=ONESTOPBIT;
	pDCB.fRtsControl=RTS_CONTROL_ENABLE;
	if(!SetCommState(hPort,&pDCB))
       MessageBox(L"Fail to Set Port !");
	  // DWORD    dwThreadID;
	     
	   //hReadThread=CreateThread(NULL,0,ReadThread,0,0,&dwThreadID);
	 //	if(hReadThread)
	      // CloseHandle(hReadThread);
}

void CCommView::OnAutoReceive() 
{
	// TODO: Add your command handler code here
	SetTimer(2,1000,NULL);
}

void CCommView::OnSend() 
{
	// TODO: Add your command handler code here
	DWORD dwNumBytesWritten;
	BOOL bResult;
	CString str;
	BYTE Send_Frame[]="This is a test example!";
	str=Send_Frame;
    int Send_Frame_Length=str.GetLength();//
	bResult=WriteFile (hPort,              // Port handle
			           &Send_Frame,	   // Pointer to the data to write 
                       Send_Frame_Length,  // Number of bytes to write
                       &dwNumBytesWritten, // Pointer to the number of bytes written
                       NULL);// Must be NULL for Windows CE
	if(!bResult)
		MessageBox(L"Fail to Writeto port!");
	str=Send_Frame;
	CClientDC dc(this);
	CRect rect1(10,10,400,400);
	 
    dc.ExtTextOut(10,10,2,&rect1,str,NULL);
}

void CCommView::OnReceive() 
{
	// TODO: Add your command handler code here
	BOOL bResult;
	DWORD dwBytesTransferred; 
	BYTE  Data;
	CString str1;
	int iCounter=0;
	char ReceiveBuf[100];
	memset(&Data,0,sizeof(Data));//clear Data
    memset(&ReceiveBuf,0,sizeof(ReceiveBuf));
	
	///
	do
	{
	bResult=ReadFile (hPort, &Data, 1, &dwBytesTransferred, NULL);
	if(!bResult)
		   MessageBox(L"Error") ;
	if(dwBytesTransferred==1)
	{
		ReceiveBuf[iCounter++]=Data;
		 if(ReceiveBuf[0]!='$')
		 { 
               iCounter=0;
		 }
		  if(iCounter==6)
		 {if(ReceiveBuf[5]!='C')
            iCounter=0;
		 }
	}

	//else AfxMessageBox(_T("接收不成功"));
	}while(dwBytesTransferred==1);
	iCounter=0;
    str1=ReceiveBuf;
	//f(str1.GetLength()==0)
     // str1="NO Data!";
	CClientDC dc(this);
	CRect rect1(10,10,400,400);
	 
    dc.ExtTextOut(10,10,2,&rect1,str1,NULL);
	
}

void CCommView::OnAutoSend() 
{
	// TODO: Add your command handler code here
	SetTimer(1,1000,NULL);
}

void CCommView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
		OnSend();
    if(nIDEvent==2)
        OnReceive();
	CView::OnTimer(nIDEvent);
}

UINT ReadThread(LPVOID lpvoid)
{
   
	 BOOL bResult;
	DWORD dwBytesTransferred,dwComm; 
	BYTE  Data;
	//COMSTAT Comstat;
	int iCounter=0;
	char ReceiveBuf[256];
	memset(&Data,0,sizeof(Data));//clear Data
    memset(&ReceiveBuf,0,sizeof(ReceiveBuf));
	
while(fThread)
{
	while(hPort!=NULL)
	{   
		
		SetCommMask(hPort,EV_RXCHAR);
		WaitCommEvent(hPort, &dwComm,0);
		if(dwComm==EV_RXCHAR)
		{
			do
			{
	           bResult=ReadFile (hPort, &Data, 1, &dwBytesTransferred, NULL);
			   
         if(!bResult)
		       AfxMessageBox(L"Error") ;
	     if(dwBytesTransferred==1)
		 {
		 
		   ReceiveBuf[iCounter++]=Data;
		    if(ReceiveBuf[0]!='$')
			{ 
               iCounter=0;
			   memset(&ReceiveBuf,0,sizeof(ReceiveBuf));
			}
		       if(iCounter==6)
			   {
			      if(ReceiveBuf[5]!='C')
				  {
		               memset(&ReceiveBuf,0,sizeof(ReceiveBuf));
                       iCounter=0;
				  }
			   }
			 }
              
	
// ::SendMessage(m_hWnd,WM_COMM_RXCHAR,0,0);
	//else AfxMessageBox(_T("接收不成功"));
	}while(dwBytesTransferred==1);
	iCounter=0;
	
    str=ReceiveBuf;
	if(str.GetLength()!=0)
   ::PostMessage(HWND(lpvoid),WM_COMM_RXCHAR,0,0);
   Sleep(500);
    //AfxMessageBox(str);
	memset(&ReceiveBuf,0,sizeof(ReceiveBuf));
	GetCommModemStatus(hPort,&dwComm);
	//::SendMessage(m_hWnd,WM_COMM_RXCHAR,0,0);
	}
	//else str=L"";

	}
 
}
	return 0;
}

LONG CCommView::ShowData(WPARAM wParam,LPARAM lParam)
{
	///显示接收到的数据
	CClientDC dc(this);
	CRect rect1(10,10,400,40);
	   
	   
   dc.ExtTextOut(10,10,2,&rect1,str,NULL);
   //MessageBox(str);
   //str=L"";
   return str.GetLength();

}

void CCommView::OnStopSend() 
{
	// TODO: Add your command handler code here
	KillTimer(1);
}

void CCommView::OnStopReceive() 
{
	// TODO: Add your command handler code here
	KillTimer(2);
}

void CCommView::OnTestReceive() 
{
	// TODO: Add your command handler code here
    fThread=TRUE;
	m_hWnd=GetSafeHwnd();
    hReadThread=AfxBeginThread(ReadThread,m_hWnd);
}

void CCommView::OnCloseThread() 
{
	// TODO: Add your command handler code here
  fThread=FALSE;
  CloseHandle(hReadThread);
}

void CCommView::OnMonitor() 
{
	// TODO: Add your command handler code here
	int n=ShowData(0,0);
	CString str11;
	str11.Format(L"%d",n);
	MessageBox(str11);
}

⌨️ 快捷键说明

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