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

📄 commview.cpp

📁 一个好用的串口通信库(Pcomm)的编程实现
💻 CPP
字号:
// CommView.cpp : implementation of the CCommView class
//

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

#include "CommDoc.h"
#include "CommView.h"
#include "SetupDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int PortNumber=1;
int rate=B9600;

void CALLBACK RecieveDataCRCIrq(int port);      //接收到数据

/////////////////////////////////////////////////////////////////////////////
// CCommView

IMPLEMENT_DYNCREATE(CCommView, CFormView)

BEGIN_MESSAGE_MAP(CCommView, CFormView)
	//{{AFX_MSG_MAP(CCommView)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BGET, OnBget)
	ON_COMMAND(ID_SET, OnSet)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
	ON_MESSAGE(WM_USER+2,OnReceive)
END_MESSAGE_MAP()

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

CCommView::CCommView()
	: CFormView(CCommView::IDD)
{
	//{{AFX_DATA_INIT(CCommView)
	m_T = _T("30゜C");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	nLinesPerPage=40;
    sio_close(PortNumber);
	Sleep(500);
	if(sio_open(PortNumber)!=0)
	{ CString s;
	s.Format("cann't open  port %d",PortNumber);
	AfxMessageBox(s);
	return;}
	//AfxMessageBox("ok");
	sio_ioctl(PortNumber,rate,P_NONE|BIT_8|STOP_1);
	sio_flush(PortNumber,2);
	//读字符中断函数启动
	sio_term_irq( PortNumber,RecieveDataCRCIrq, 0x0F);   //接收到0xFF作为结束后调用函数	   
}

CCommView::~CCommView()
{
}

void CCommView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCommView)
	DDX_Control(pDX, IDC_LIST2, m_List);
	DDX_Text(pDX, IDC_ST, m_T);
	//}}AFX_DATA_MAP
}

BOOL CCommView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
    cs.cx=800;
    cs.cy=600;
	return CFormView::PreCreateWindow(cs);
}

void CCommView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	m_List.DeleteAllItems( );
	m_List.DeleteColumn(0);
	m_List.InsertColumn(0, "序号");
    m_List.DeleteColumn(1);
    m_List.InsertColumn(1, "温度(゜C)");
	m_List.DeleteColumn(2);
	m_List.InsertColumn(2, "说明");
	m_List.SetColumnWidth(0, 70);
    m_List.SetColumnWidth(1, 100);
    m_List.SetColumnWidth(2, 300);
	//////////////
	//m_List.
	//m_List.InsertItem(0,"1");
    //m_List.SetItem(0, 1, LVIF_TEXT, "30", 0, 0, 0, 0);
	Display();
}

/////////////////////////////////////////////////////////////////////////////
// CCommView printing

BOOL CCommView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	pInfo->SetMaxPage(3);
	return DoPreparePrinting(pInfo);
}

void CCommView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CCommView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CCommView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
	// TODO: add customized printing code here
	 int        i, nStart, nEnd, nHeight;
    CString    str;
    CPoint     point(720, -1440);//1440=1英寸
    CFont      font;
    TEXTMETRIC tm;

    pDC->SetMapMode(MM_TWIPS);
    CCommDoc* pDoc = GetDocument();
    m_nPage = pInfo->m_nCurPage; // for PrintPageFooter's benefit
    nStart = (m_nPage - 1) * nLinesPerPage;
    nEnd = nStart + nLinesPerPage;
     // 14-point fixed-pitch font
    font.CreateFont(-280, 0, 0, 0, 400, FALSE, FALSE,
                    0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                    CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                    DEFAULT_PITCH | FF_MODERN, "Courier New");
                    // Courier New is a TrueType font
    CFont* pOldFont = (CFont*) (pDC->SelectObject(&font));
    PrintPageHeader(pDC);
    pDC->GetTextMetrics(&tm);
    nHeight = tm.tmHeight + tm.tmExternalLeading;
    for (i = nStart; i < nEnd; i++) {
        if (i >120) {
            break;
        }
        str.Format("%6d    %6d", i + 1,pDoc->DataBuffer[i]);
        point.y -= nHeight;
        pDC->TextOut(point.x, point.y, str);
    }
    PrintPageFooter(pDC);
    pDC->SelectObject(pOldFont);
}

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

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

void CCommView::Dump(CDumpContext& dc) const
{
	CFormView::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
LONG CCommView::OnReceive(WPARAM wparam,LPARAM lparam)
{	int ReadStatus;
            char *buf;
			int i,j;
			j=0;
            int len=(int)wparam;
			buf=new char[len];
            ReadStatus=sio_read(PortNumber,buf,len);
	       if (ReadStatus>=0)
	        {
	          for(i=0;i<len;i++)
			  {
               if((buf[i]==0x0c)|(buf[i]==0x0d))
				   break;
			  }
			  switch(buf[i])
			  {
			  case 0x0c:
                        m_T.Format("%d゜C",buf[i+1]);
						UpdateData(FALSE);
						break;
			  case 0x0d:
				  CCommDoc *pDoc=GetDocument();
				  for(j=0;j<120;j++){
				  i++;
				  pDoc->DataBuffer[j]=buf[i];
				  }
				  Display();
				  AfxMessageBox("数据更新完毕!");
				  break;
			  }
	          
		   }
		   delete buf;
	      sio_flush(PortNumber,0);
              return 0L;
}
 //读字符中断函数
void CALLBACK RecieveDataCRCIrq(int port)          //接收到数据 
{
	int length;
	length=sio_iqueue(PortNumber);  //判断端口3是否有数据
	if (length>0)
    ((CFrameWnd*)AfxGetMainWnd())->GetActiveView()->SendMessage(WM_USER+2,(WPARAM)length,0);
}

void CCommView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(m_List.GetSafeHwnd())
	{CRect rect(150,0,cx,cy);
	 m_List.MoveWindow(&rect);
	}
}

void CCommView::Display()
{m_List.DeleteAllItems( );
 CCommDoc *pDoc=GetDocument();
 CString s;
 for(int i=0;i<120;i++)
 {  
	s.Format("%d",i+1);
	m_List.InsertItem(i,s);
    s.Format("%d",pDoc->DataBuffer[i]);
    m_List.SetItem(i, 1, LVIF_TEXT, s, 0, 0, 0, 0);
 }
}

void CCommView::OnBget() 
{
	// TODO: Add your control notification handler code here
	char c;
	c='R';
	sio_write(PortNumber,&c,1);

}

void CCommView::OnSet() 
{
	// TODO: Add your command handler code here
	sio_close(PortNumber);
	CSetupDialog  dlg;
	if(dlg.DoModal()==IDOK)
	{
	 PortNumber=dlg.m_port;
     rate=B19200-dlg.m_rate;
	 //CString s;
	 //s.Format("%d",dlg.m_rate);
	 //AfxMessageBox(s);
	}
	if(sio_open(PortNumber)!=0)
	{ CString s;
	s.Format("cann't open  port %d",PortNumber);
	AfxMessageBox(s);
	return;}
	//AfxMessageBox("ok");
	sio_ioctl(PortNumber,rate,P_NONE|BIT_8|STOP_1);
	sio_flush(PortNumber,2);
	//读字符中断函数启动
	sio_term_irq( PortNumber,RecieveDataCRCIrq, 0x0F);   //接收到0xFF作为结束后调用函数	   
}

void CCommView::PrintPageHeader(CDC *pDC)
{
CString str;

    CPoint point(0, 0);
    pDC->TextOut(point.x, point.y, " 温度记录单");
    point += CSize(720, -720);
    str.Format(" %6.8s    %8.12s       %6.6s",
               "序号", "温度(゜C)", "说明");
    pDC->TextOut(point.x, point.y, str);

}

void CCommView::PrintPageFooter(CDC *pDC)
{
 CString str;

    CPoint point(0, -14400); // Move 10 inches down
    CCommDoc* pDoc = GetDocument();
    str.Format(" 文件: %s", (LPCSTR) pDoc->GetTitle());
    pDC->TextOut(point.x, point.y, str);
    str.Format("Page %d", m_nPage);
    CSize size = pDC->GetTextExtent(str);
    point.x += 11520 - size.cx;
    pDC->TextOut(point.x, point.y, str); // right-justified
}

⌨️ 快捷键说明

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