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

📄 vcsampledlg.cpp

📁 深圳远望谷804读写器DEMO 深圳远望谷804读写器DEMO
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// VCsampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VCsample.h"
#include "VCsampleDlg.h"
#include "XCRFAPI.h"
#include "WriteEPC.h"
#include "ReadData.h"
#include "WriteData.h"
#include "SetIP.h"
#include "SetPower.h"
#include <math.h>   

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

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

/////////////////////////////////////////////////////////////////////////////
// CVCsampleDlg dialog

CVCsampleDlg::CVCsampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVCsampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVCsampleDlg)
	m_strComMode = _T("");
	m_strAntenna = _T("");
	m_strAffirmCode = _T("");
	m_uiScanTimesEPC = 1;
	m_IdTimes = _T("0");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVCsampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVCsampleDlg)
	DDX_Control(pDX, IDC_STATIC_SULV, m_slStatic);
	DDX_Control(pDX, IDC_STATIC_STATUS3, m_Status3Ctl);
	DDX_Control(pDX, IDC_LIST_MARKID, m_IdList);
	DDX_Control(pDX, IDC_STATIC_STATUS2, m_Status2Ctl);
	DDX_Control(pDX, IDC_STATIC_STATUS1, m_Status1Ctl);
	DDX_Control(pDX, IDC_EDIT_NUM, m_EditScansTimesEPC);
	DDX_Control(pDX, IDC_COMBO_Affirm, m_AffirmCombCtl);
	DDX_Control(pDX, IDC_COMBO_Antenna, m_AntennaCombCtl);
	DDX_Control(pDX, IDC_IPADDRESS_IP, m_IpCtl);
	DDX_Control(pDX, IDC_COMBO_MODE, m_ComModelCtl);
	DDX_CBString(pDX, IDC_COMBO_MODE, m_strComMode);
	DDX_CBString(pDX, IDC_COMBO_Antenna, m_strAntenna);
	DDX_CBString(pDX, IDC_COMBO_Affirm, m_strAffirmCode);
	DDX_Text(pDX, IDC_EDIT_NUM, m_uiScanTimesEPC);
	DDX_Text(pDX, IDC_EDIT_IDTIMES, m_IdTimes);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVCsampleDlg, CDialog)
	//{{AFX_MSG_MAP(CVCsampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_MENU_QUERY_IP, OnMenuQueryIp)
	ON_COMMAND(ID_MENU_QUERY_POWER, OnMenuQueryPower)
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
	ON_COMMAND(ID_MENU_CONNECT, OnMenuConnect)
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_COMBO_MODE, OnSelchangeComboMode)
	ON_CBN_SELCHANGE(IDC_COMBO_Antenna, OnSelchangeCOMBOAntenna)
	ON_COMMAND(ID_MENU_DISCONNECT, OnMenuDisconnect)
	ON_COMMAND(ID_MENU_READ_EPC, OnMenuReadEpc)
	ON_COMMAND(ID_MENU_READ_TID, OnMenuReadTid)
	ON_COMMAND(ID_MENU_STOP_READ, OnMenuStopRead)
	ON_COMMAND(ID_MENU_WRITE_EPC, OnMenuWriteEpc)
	ON_COMMAND(ID_MENU_READ_USERDATA, OnMenuReadUserdata)
	ON_COMMAND(ID_MENU_WRITE_USERDATA, OnMenuWriteUserdata)
	ON_COMMAND(ID_MENU_IP_SET, OnMenuIpSet)
	ON_COMMAND(ID_MENU_POWER_SET, OnMenuPowerSet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVCsampleDlg message handlers

BOOL CVCsampleDlg::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
	IsHaveOpened = FALSE;
	hCom = INVALID_HANDLE_VALUE;

	FindCurrentDirectory();	

	InitCombString ( );
	InitComb();
	InitIpPara();
	m_AffirmCombCtl.ResetContent();
	m_AffirmCombCtl.AddString ( "Single" );
	m_AffirmCombCtl.AddString ( "Loop" );
	m_AffirmCombCtl.SetCurSel ( 1 );

	m_uiScanTimesEPC = 1;

	InitListHead();
	ResetArray();
	InitRectTimesArray();

	m_AntennaCombCtl.EnableWindow(false);
	m_AffirmCombCtl.EnableWindow(false);
	m_EditScansTimesEPC.EnableWindow ( false ); 
	
	IsReadId = FALSE;

	CTime t;
	int		nHour;
	int		nMinute;
	int		nSecond;

	t = CTime::GetCurrentTime ( );	
	CString strTime;
	nSecond = t.GetSecond ( );
	nMinute = t.GetMinute ( );
	nHour = t.GetHour ( );
	strTime.Format ( "%02d:%02d:%02d", nHour, nMinute, nSecond );
	m_Status3Ctl.SetWindowText ( strTime ); 
	SetTimer ( 1, 1000, NULL );

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

void CVCsampleDlg::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 CVCsampleDlg::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 CVCsampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//Query IP
void CVCsampleDlg::OnMenuQueryIp() 
{
	// TODO: Add your command handler code here
	int i;
	
	if ( IsHaveOpened == TRUE )
	{
		unsigned char ucRecBuf [ 256 ];
		CString strSysPara, strTemp;
		memset ( ucRecBuf, 0, 256 );
		
		int Result = XCSysQuery ( hCom, 0x06, 0x0C, ucRecBuf );
		if ( 1 == Result )
		{
			CString szTemp = "";
			strSysPara = "IP address:";
			CString szIp = "";
			for ( i = 0; i < 3; i++ )
			{
				strTemp.Format ( "%01d.", ucRecBuf [ i ] );
				strSysPara += strTemp;
				szIp += strTemp;
				szTemp	+= strTemp;
			}
			strTemp.Format ( "%01d \n", ucRecBuf [ i ] );
			strSysPara += strTemp;
			
			strSysPara += "Subnet Mask:";
			szTemp = "";
			
			for ( i = 4; i < 7; i++ )
			{
				strTemp.Format ( "%01d.", ucRecBuf [ i ] );
				strSysPara += strTemp;
				szTemp	+= strTemp;
			}
			strTemp.Format ( "%01d \n", ucRecBuf [ i ] );
			strSysPara += strTemp;
		
			strSysPara += "GateWay :";
			szTemp = "";
			for ( i = 8; i < 11; i++ )
			{
				strTemp.Format ( "%01d.", ucRecBuf [ i ] );
				strSysPara += strTemp;
				szTemp	+= strTemp;
			}
			strTemp.Format ( "%01d \n", ucRecBuf [ i ] );
			strSysPara += strTemp;

			MessageBox ( strSysPara, "Network parameter", MB_OK | MB_ICONINFORMATION);
		}
		else
		{
			strStatusDemo="Query reader IP address failed!";
			AfxMessageBox ( strStatusDemo);
		}
	}
	else
		AfxMessageBox ( "Please connect reader successfully first!" );
}
//Query antenna power
void CVCsampleDlg::OnMenuQueryPower() 
{
	// TODO: Add your command handler code here
	if ( IsHaveOpened == TRUE )
	{
		unsigned char ucRecBuf [ 256 ];
		memset ( ucRecBuf, 0, 256 );
		CString strTemp="";

		int Result = XCPowerQuery ( hCom, 0X03, 4, ucRecBuf );
		if ( 1 == Result )
		{
			strTemp.Format("1#:%1.1f dBm,2#:%1.1f dBm\n3#:%1.1f dBm,4#:%1.1f dBm"
				, (20+ucRecBuf [ 0 ]*0.5) ,(20+ucRecBuf [ 1 ]*0.5) ,(20+ucRecBuf [ 2 ]*0.5) ,(20+ucRecBuf [ 3 ]*0.5));
			MessageBox ( strTemp, "Antenna power query"	, MB_OK | MB_ICONINFORMATION );
		}
		else
		{
			AfxMessageBox ( "Query antenna power failed!");			
		}		
		
	}
	else
		AfxMessageBox ( "Please connect reader successfully first!" );

}
//Exit
void CVCsampleDlg::OnMenuExit() 
{
	// TODO: Add your command handler code here
	CString csTitle = "Exit";	
//	char Msg[256];

//	strcpy(Msg,"You will close the application,please confirm the choice!\r\n");
	
//	if(MessageBox(Msg, csTitle, MB_YESNO | MB_ICONQUESTION)
//		== IDYES)
//	{
		PostMessage(WM_CLOSE,0,0);	
//	}
}
//Connect the reader
void CVCsampleDlg::OnMenuConnect() 
{
	// TODO: Add your command handler code here
	strStatusDemo = "";
	int Result,result;
	Result=0;
	result=0; 
	
	if ( IsHaveOpened == FALSE )
	{		
		if ( !strCfgFileName.IsEmpty ( ) )
		{
			UpdateData ( TRUE );
			if(m_IpCtl.IsBlank())  
			{
				AfxMessageBox("IP error!");				
				return;
			}
			MemSet ( strCfgFileName, "XCRF-800 Series Reader"
				, "XC_PORT_TYPE", m_strComMode.Left(3) );
			Result = XCOpen ( hCom, strCfgFileName
				, "XCRF-800 Series Reader" );
			if ( Result != 1 )
			{	
				m_AntennaCombCtl.EnableWindow(true);
				m_AffirmCombCtl.EnableWindow(true);
				m_EditScansTimesEPC.EnableWindow(false);
				if(strcmp(m_strComMode,"TCP")==0)
				{
					m_IpCtl.EnableWindow(true);
				}
				else
				{
					m_IpCtl.EnableWindow(false);
				}		
				strStatusDemo = "Connect failed!";
				CString sz;
				sz = theApp.Error_800 ( Result );
				if ( sz != "" )
				{
					strStatusDemo += "(" + sz + ")";
				}
				m_Status1Ctl.SetWindowText ( strStatusDemo );				
				return ;
			}
			
			IsHaveOpened  = TRUE;
			
			unsigned char ucRecIP [ 256 ];
			CString strIpTemp="";
			int i = 0;
			result = XCSysQuery ( hCom, 0x06, 0x0C, ucRecIP);
			if( 1 == result )
			{
				CString szIp = "";
				for ( i = 0; i < 3; i++ )
				{
					strIpTemp.Format ( "%01d.", ucRecIP [ i ] );
					szIp += strIpTemp; 			
				}						
				strIpTemp.Format ( "%01d", ucRecIP [ i ] );	
				szIp += strIpTemp; 	
				m_IpCtl.SetWindowText ( szIp );
				MemSet ( strCfgFileName
					, "XCRF-800 Series Reader"
					, "XC_HOST_NAME"
					, szIp );
			}
			m_Status1Ctl.SetWindowText ( "Connect successfully!" );	
			
			m_AntennaCombCtl.EnableWindow(true);
			m_AffirmCombCtl.EnableWindow(true);
			m_EditScansTimesEPC.EnableWindow ( true );
			m_IpCtl.EnableWindow(false);		
			m_ComModelCtl.EnableWindow ( false );
			
			UpdateData(false);
		}
	}
	else
	{
		::AfxMessageBox("Connection has been established!");
	}
}
//Find current directory
BOOL CVCsampleDlg::FindCurrentDirectory()
{
	CString strTemp;
	TCHAR steFullPath[MAX_PATH];	
	
	GetModuleFileName(NULL,(LPTSTR)steFullPath,_MAX_PATH);
	strTemp = steFullPath;
	int pos=strTemp.ReverseFind(L'\\');
	strTemp=strTemp.Mid(0,pos);	
	theApp.m_szRunPath = strTemp + "\\";
	m_strSysit =strTemp +_T("\\SYSIT.CFG");

	return TRUE;
}

void CVCsampleDlg::InitCombString()
{
	strTianXian[0] = _T("1#");
	strTianXian[1] = _T("2#");
	strTianXian[2] = _T("3#");
	strTianXian[3] = _T("4#");
	strTianXian[4] = _T("1#-2#");  
	strTianXian[5] = _T("1#-3#");
	strTianXian[6] = _T("1#-4#");

	strCom[0] = _T("TCP");
	strCom[1] = _T("USB");

}

void CVCsampleDlg::InitComb()
{
	m_AntennaCombCtl.ResetContent ();
	m_ComModelCtl.ResetContent();

	for(int i=0;i<8;i++)   
	{
		if(i<=1)                         
			m_ComModelCtl.AddString (strCom[i]);
		if(i<=6)
			m_AntennaCombCtl.AddString (strTianXian[i]);
					
	}

	for(int k=1;k<9;k++)
	{
		CString sPort;
		sPort.Format(_T("\\\\.\\COM%d"),k);
		bool bSuccess =FALSE;
		HANDLE hPort = ::CreateFile(sPort,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING ,0,0);
		if(hPort == INVALID_HANDLE_VALUE)
		{
		  DWORD dwError = GetLastError();
		  if(dwError==ERROR_ACCESS_DENIED)
			  bSuccess = TRUE;
		}
		else
		{
			  bSuccess = TRUE;
			  CloseHandle(hPort);
		}
		if(bSuccess)
		{
			CString str;
			str.Format("COM%d",k);
			m_ComModelCtl.AddString(str);	
		}
	}	

⌨️ 快捷键说明

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