回复 2楼 hellovfp
这是完整的MFC的程序
程序代码:
#include "stdafx.h"
#include "document transmission.h"
#include "document transmissionDlg.h"
#include "cifXTest_Console.h"
#include "cifXErrors.h"
#include "cifXUser.h"
#include "rcX_Public.h"
#include "rcX_User.h"
#include "TLR_Diag.h"
#include "TLR_Types.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define IO_WAIT_TIMEOUT 10
DWORD dwFileLen;
//unsigned char *abReadIOBuffer;
//unsigned char *abWriteIOBuffer;
unsigned char abReadIOBuffer[];
unsigned char abWriteIOBuffer[];
//char *abReadIOBuffer;
//char *abWriteIOBuffer;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDocumenttransmissionDlg dialog
CDocumenttransmissionDlg::CDocumenttransmissionDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDocumenttransmissionDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDocumenttransmissionDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDocumenttransmissionDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDocumenttransmissionDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDocumenttransmissionDlg, CDialog)
//{{AFX_MSG_MAP(CDocumenttransmissionDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_selectfile, Onselectfile)
ON_BN_CLICKED(IDC_receivefile, Onreceivefile)
ON_BN_CLICKED(IDC_sendfile, Onsendfile)
ON_BN_CLICKED(IDC_storefile, Onstorefile)
ON_BN_CLICKED(IDC_driveropen, Ondriveropen)
ON_BN_CLICKED(IDC_enumchannel, Onenumchannel)
ON_BN_CLICKED(IDC_enumboard, Onenumboard)
ON_BN_CLICKED(IDC_openchannel, Onopenchannel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDocumenttransmissionDlg message handlers
BOOL CDocumenttransmissionDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CDocumenttransmissionDlg::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 CDocumenttransmissionDlg::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 CDocumenttransmissionDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/************************************************************************/
/* 选择要传输的文件 */
/************************************************************************/
void CDocumenttransmissionDlg::Onselectfile()
{
// TODO: Add your control notification handler code here
CFileDialog fileDlg(TRUE);
fileDlg.m_ofn.lpstrFilter="Text Files(*.doc)\0*.doc\0picture(*.jpg)\0*.jpg\0All File(*.*)\0*.*\0\0";
fileDlg.m_ofn.lpstrDefExt="doc";
if (IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeRead|CFile::typeBinary);
dwFileLen=(DWORD)file.GetLength();
abWriteIOBuffer=new unsigned char [dwFileLen+1];
unsigned char abWriteIOBuffer[] = {0};
abWriteIOBuffer[dwFileLen]=0;
file.Read(abWriteIOBuffer,dwFileLen);
file.Close();
}
}
/************************************************************************/
/* 发送已经存到abWriteIOBuffer的文件 */
/************************************************************************/
void CDocumenttransmissionDlg::Onsendfile()
{
// TODO: Add your control notification handler code here
long lRet=CIFX_NO_ERROR;
HANDLE hDriver = NULL;
uint32_t ulBoard = 0;
BOARD_INFORMATION tBoardInfo = {0};
uint32_t ulChannel = 0;
CHANNEL_INFORMATION tChannelInfo = {0};
HANDLE hSys = NULL;
HANDLE hChannel = NULL;
uint32_t kbhit= 0;
lRet = xDriverOpen(&hDriver);
if(CIFX_NO_ERROR == lRet)
{
while(lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo))
{
while(lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo))
{
++ulChannel;
}
++ulBoard;
}
uint32_t ulChannel=0;
lRet = xSysdeviceOpen(hDriver,"cifX0", &hSys);
if(CIFX_NO_ERROR == lRet)
{
lRet = xChannelOpen(hDriver,"cifX0", ulChannel, &hChannel);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("cifX0通道0打开失败!");
} else
{
// Cyclic IO data transfer
// Write Output to all areas
lRet = xChannelIOWrite( hChannel,0, 0,dwFileLen,abWriteIOBuffer, IO_WAIT_TIMEOUT);
if( lRet== CIFX_NO_ERROR)
{
// Error writing Output data
MessageBox("发送数据完成");
}else
{
MessageBox("发送数据失败");
}
}
}
}
}
/************************************************************************/
/* 接收文件并放到abReadIOBuffer */
/************************************************************************/
void CDocumenttransmissionDlg::Onreceivefile()
{
// TODO: Add your control notification handler code here
long lRet=CIFX_NO_ERROR;
HANDLE hDriver = NULL;
uint32_t ulBoard = 0;
BOARD_INFORMATION tBoardInfo = {0};
uint32_t ulChannel = 0;
CHANNEL_INFORMATION tChannelInfo = {0};
HANDLE hSys = NULL;
HANDLE hChannel = NULL;
lRet = xDriverOpen(&hDriver);
if(CIFX_NO_ERROR == lRet)
{
while(lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo))
{
while(lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo))
{
++ulChannel;
}
++ulBoard;
}
uint32_t ulChannel=0;
lRet = xSysdeviceOpen(hDriver,"cifX0", &hSys);
if(CIFX_NO_ERROR == lRet)
{
lRet = xChannelOpen(hDriver,"cifX0", ulChannel, &hChannel);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("打开失败!");
} else
{
lRet = xChannelIORead( hChannel,0, 0,100 ,abReadIOBuffer, IO_WAIT_TIMEOUT);
if(lRet==CIFX_NO_ERROR) //经过这一步可以把数据从装置读取到"abReadIOBuffer",以供显示程序使用
{
MessageBox("存入abReadbuffer!");
}
}
}
}
}
/************************************************************************/
/* 把接收到的文件另存到指定的位置 */
/************************************************************************/
void CDocumenttransmissionDlg::Onstorefile()
{
// TODO: Add your control notification handler code here
CFileDialog fileDlg(FALSE);
fileDlg.m_ofn.lpstrFilter="Text Files(*.doc)\0*.doc\0*.jpg\0All File(*.*)\0*.*\0\0";
fileDlg.m_ofn.lpstrDefExt="doc";
if (IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);
file.Write(abReadIOBuffer,dwFileLen);
file.Close();
}
}
/************************************************************************/
/* 测试用的一些按钮 */
/************************************************************************/
void CDocumenttransmissionDlg::Ondriveropen()
{
// TODO: Add your control notification handler code here
long lRet;
HANDLE hDriver = NULL;
lRet = xDriverOpen(&hDriver);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("打开失败!");
}
else
{
MessageBox("打开成功!");
}
}
void CDocumenttransmissionDlg::Onenumchannel()
{
// TODO: Add your control notification handler code here
/************************************************************************/
/* 先打开驱动 */
/************************************************************************/
/************************************************************************/
/* 列举board */
/************************************************************************/
long lRet;
HANDLE hDriver = NULL;
uint32_t ulBoard = 0;
BOARD_INFORMATION tBoardInfo = {0};
uint32_t ulChannel = 0;
CHANNEL_INFORMATION tChannelInfo = {0};
lRet = xDriverOpen(&hDriver);
if(CIFX_NO_ERROR == lRet)
{
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
/*************************************************************************/
/* iterate over all channels on the current board */
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board0失败!");
}
else
{
lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board0通道0失败!!");
}
else
{
MessageBox("枚举board0通道0成功!");
++ulChannel;
lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board0通道1失败!");
}
else
{
MessageBox("枚举board0通道1成功!");
}
}
++ulBoard;
uint32_t ulChannel=0;
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board1失败!");
}
else
{
lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board1通道0失败!");
}
else
{
MessageBox("枚举board1通道0成功!");
++ulChannel;
lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board1通道1失败!");
}
else
{
MessageBox("枚举board1通道1成功!");
}
++ulBoard;
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("枚举board2失败!");
}
else
{
MessageBox("枚举board2成功!");
++ulBoard;
}
}
}
}
}
}
void CDocumenttransmissionDlg::Onenumboard()
{
// TODO: Add your control notification handler code here
long lRet;
HANDLE hDriver = NULL;
uint32_t ulBoard = 0;
BOARD_INFORMATION tBoardInfo = {0};
/* Iterate over all boards */
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("打开失败!");
}
else
{
MessageBox("打开成功0!");
++ulBoard;
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("打开失败!");
}
else
{
MessageBox("打开成功1!");
++ulBoard;
lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("打开失败!");
}
else
{
MessageBox("打开成功1");
++ulBoard;
}
}
}
}
void CDocumenttransmissionDlg::Onopenchannel()
{
// TODO: Add your control notification handler code here
/************************************************************************/
/* 先打开驱动 */
/************************************************************************/
/************************************************************************/
/* 列举 board */
/************************************************************************/
/************************************************************************/
/* 列举 channel */
/************************************************************************/
/************************************************************************/
/* SysdeviceOpen */
/************************************************************************/
long lRet;
HANDLE hDriver = NULL;
uint32_t ulBoard = 0;
BOARD_INFORMATION tBoardInfo = {0};
uint32_t ulChannel = 0;
CHANNEL_INFORMATION tChannelInfo = {0};
//char* szBoard;
HANDLE hSys = NULL;
HANDLE hChannel = NULL;
lRet = xDriverOpen(&hDriver);
if(CIFX_NO_ERROR == lRet)
{
while(lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo))
{
while(lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo))
{
++ulChannel;
}
++ulBoard;
}
uint32_t ulChannel= 0;
lRet = xSysdeviceOpen(hDriver,"cifX0", &hSys);
if(CIFX_NO_ERROR == lRet)
{
lRet = xChannelOpen(hDriver,"cifX0", ulChannel, &hChannel);
if(CIFX_NO_ERROR != lRet)
{
MessageBox("cifX0通道0打开失败!");
}
else
{
MessageBox("cifX0通道0打开成功!");
}
}
}
}