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

📄 oem4pci_demodlg.cpp

📁 C++编写的针对CP5611 PCI卡的通讯程序Demo
💻 CPP
字号:
// oem4pci_demoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "oem4pci_demo.h"
#include "oem4pci_demoDlg.h"
#include "oem4pcidll.h"

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



UCHAR init_data[17]={0x13,0x06,0x0fa,0x06,0x1f,0x2f,0x1f,0x2f,0x1f,0x2f,0,0,0,0,48,48,0};
UCHAR da_buf[48];
UCHAR prm_buf[48];
UCHAR send_buf[48];
UCHAR rev_pause_en=0;
UCHAR temp=0;
UINT rev_cnt=0;
UCHAR display_cnt=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()

/////////////////////////////////////////////////////////////////////////////
// COem4pci_demoDlg dialog

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

void COem4pci_demoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COem4pci_demoDlg)
	DDX_Control(pDX, ID_CLR_CNT, m_clr);
	DDX_Text(pDX, IDC_REVDATA, m_revda);
	DDX_Text(pDX, IDC_COUNT, m_count);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COem4pci_demoDlg, CDialog)
	//{{AFX_MSG_MAP(COem4pci_demoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_OPEN_DEV, OnOpenDev)
	ON_WM_TIMER()
	ON_BN_CLICKED(ID_CLR_CNT, OnClrCnt)
	ON_BN_CLICKED(ID_PAUSE_REV, OnPauseRev)
	ON_BN_CLICKED(ID_CLR_REV, OnClrRev)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COem4pci_demoDlg message handlers

BOOL COem4pci_demoDlg::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
	
	// ************** 开机初始化数据

	m_count=0;
	m_revda.Empty();
	UpdateData(FALSE);			//更新编辑框内容

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COem4pci_demoDlg::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 COem4pci_demoDlg::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 COem4pci_demoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COem4pci_demoDlg::OnOpenDev() //打开PCI卡,并进行初始化
{
	UCHAR k,l;
	if ( OPEN_PCICARD == INVALID_HANDLE_VALUE )  // 使用之前必须打开设备 
	{
		MessageBox( "无法打开CH365设备","提示");
		CDialog::OnCancel();//退出
	}
	else
	{
		CLOSE_INT;//关闭中断	
							
		for(k=0;k<5;k++)
		{			
			l=pcicard_init(init_data,17);
			if(l==0x10)
			{
				l=1;
				break;
			}
			else l=0;
		}
		if(l==1)
		{
			MessageBox("初始化成功","提示");
			SetTimer(1,250,NULL);//打开1S定时器(250MS定时),发送数据
			SetTimer(2,10,NULL);//打开1S定时器(10MS定时),接收数据
		}
		else
		{
			MessageBox("初始化失败","提示");
			CDialog::OnCancel();//退出
			exit_pcicard();  //关闭设备	
		}
	}	
	
}

void COem4pci_demoDlg::OnCancel() //退出系统
{
	
	KillTimer(1);//关闭定时中断1
	KillTimer(2);//关闭定时中断2

	CDialog::OnCancel();//退出对话框
	exit_pcicard();  //关闭设备		
}
	
void COem4pci_demoDlg::OnTimer(UINT nIDEvent) 
{
	UCHAR k,i,*p;
	CString strtemp;

	p=da_buf;
	switch(nIDEvent)//发送数据
	{
		case 1:					
		for(k=0;k<48;k++)
		send_buf[k]=temp+k;
		k=send_pbdata(send_buf,48);//发送PROFIBUS数据
		temp++;
		if(k==0x10)
		trig_int();
		break;

		case 2://查询接收						
		k=receive_pbdata(da_buf,48,prm_buf,48);
		if(k==0x10)
		{
			if(rev_pause_en==0)
			{
				display_cnt++;
			    if(display_cnt==100)
				{
				  display_cnt=0;
				  m_revda.Empty();
				  UpdateData(FALSE);			//更新编辑框内容
				}
		
			    for(i=0;i<48;i++)
				{
				  strtemp.Format("%02X \x0",*p);
				  m_revda+=strtemp;
				  p++;
				}
			    m_revda+="\r\n";
			}
			m_count++;
			if(m_count==2147483647)
			m_count=0;
			UpdateData(FALSE);								//更新编辑框内容
		}
		break;
	}
	CDialog::OnTimer(nIDEvent);
}

void COem4pci_demoDlg::OnClrCnt() //计数器清零
{
	rev_cnt=0;
	m_count=0;
	UpdateData(FALSE);			//更新编辑框内容
}

void COem4pci_demoDlg::OnPauseRev() //暂停显示
{	
	if(rev_pause_en==0)
	{
		rev_pause_en=1;	
		SetDlgItemText(ID_PAUSE_REV,"开始");
	}
	else 
	{
		rev_pause_en=0;
		SetDlgItemText(ID_PAUSE_REV,"暂停");
	}
	UpdateData(TRUE);			//更新编辑框内容
//	UpdateData(FALSE);
}

void COem4pci_demoDlg::OnClrRev() //清接收区显示
{
	m_revda.Empty();
	UpdateData(FALSE);			//更新编辑框内容
}

⌨️ 快捷键说明

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