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

📄 ic_designdlg.cpp

📁 使用c++编程语言实现考勤卡的基本功能
💻 CPP
字号:
// IC_DesignDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IC_Design.h"
#include "IC_DesignDlg.h"
#include "mwrf32.h"
#include "string.h"
#include "time.h"


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

//CTime tm;

unsigned char Num[17];
unsigned char Name[9];

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

/////////////////////////////////////////////////////////////////////////////
// CIC_DesignDlg dialog

CIC_DesignDlg::CIC_DesignDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIC_DesignDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIC_DesignDlg)
	m_Name = _T("");
	m_Num = _T("");
	m_IC = _T("");
	m_Duty = _T("");
	m_Key = _T("");
	m_Time = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	icdev = NULL;
}

void CIC_DesignDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIC_DesignDlg)
	DDX_Control(pDX, IDC_KEY_MODE, m_KeyModelCtrl);
	DDX_Text(pDX, IDC_Name, m_Name);
	DDV_MaxChars(pDX, m_Name, 50);
	DDX_Text(pDX, IDC_Num, m_Num);
	DDV_MaxChars(pDX, m_Num, 50);
	DDX_Text(pDX, IDC_IC, m_IC);
	DDV_MaxChars(pDX, m_IC, 32);
	DDX_Text(pDX, IDC_Duty, m_Duty);
	DDX_Text(pDX, IDC_EDIT2, m_Key);
	DDV_MaxChars(pDX, m_Key, 50);
	DDX_Text(pDX, IDC_Time, m_Time);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIC_DesignDlg, CDialog)
	//{{AFX_MSG_MAP(CIC_DesignDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
	ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBtnDisconnect)
	ON_BN_CLICKED(IDC_WRITEBLOCK, OnWriteblock)
	ON_BN_CLICKED(IDC_READBLOCK, OnReadblock)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIC_DesignDlg message handlers

BOOL CIC_DesignDlg::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
    m_Sector = 1;
	m_KeyModelCtrl.SetCurSel(0);
	m_Key = "FFFFFFFFFFFF";
	UpdateData(FALSE);
    
		try
	{
		m_pConnection.CreateInstance(__uuidof(Connection));
	    m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=IC.mdb","","",adModeUnknown);
    }
	catch (_com_error e)
	{
		//::CoUninitialize();
		AfxMessageBox("无法连接数据库!程序不能正常使用!");
	}
     
	//打开记录级
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	
	try
	{
          m_pRecordset->Open("SELECT * FROM Stor",                // 查询table表中所有字段
						 m_pConnection.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText);

	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());

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

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

void CIC_DesignDlg::OnBtnConnect() 
{
	// TODO: Add your control notification handler code here
	m_Port = 0;
	m_baund=9600;
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    __int16 st;
	unsigned char _Status[30];
	memset(_Status,0,30);//覆盖status的30个字符
	rf_exit(icdev);
	icdev = rf_init(m_Port,m_baund);//初始化串口成功返回串口表示符,否则返回负值
	st = rf_get_status(icdev,_Status);//取得读写器硬件版本号,成功返回0
	if(icdev<0 || st)
	    MessageBox("连接失败!");
	else
	{
		rf_beep(icdev,10);//蜂鸣声
		 MessageBox("连接成功!");
	}
	
	UpdateData(FALSE);
	
	
}

void CIC_DesignDlg::OnBtnDisconnect() 
{
	// TODO: Add your control notification handler code here
		__int16 st;
	
//	if(icdev == NULL || icdev <0)
    st = rf_exit(icdev);//释放窗口
	if(st)
		MessageBox("断开连接失败!");
	else
        MessageBox("断开连接成功!");
	UpdateData(FALSE);
}

void CIC_DesignDlg::OnWriteblock() 
{
   UpdateData(TRUE);
	unsigned char Sec = m_Sector;
    __int16 st,sn,sd;
    unsigned __int16 TagType;
	unsigned long Snr;
	unsigned char Size;
    unsigned char data[33];
	memset(data,0,33);
	
	st = rf_reset(icdev,5);
	
	st = rf_request(icdev,1,&TagType);
	if(st)
	{
			MessageBox("寻卡失败!");
		return;
	}
	st = rf_anticoll(icdev,0, &Snr);
	if(st)
	{
		MessageBox("防冲突失败!");
		return;
	}
	st = rf_select(icdev,Snr,&Size);
	if(st)
	{
		MessageBox("选卡失败!");
		return;
	}
	unsigned char key[7];
	memset(key,0,7);
	a_hex(m_Key.GetBuffer(12),key,12);
	m_Key.ReleaseBuffer();
	unsigned char m_keymode;
	if(m_KeyModelCtrl.GetCurSel()==0)
		m_keymode = 0;
	else
		m_keymode = 4;
	st = rf_load_key(icdev,m_keymode,Sec,key);
	if(st)
	{
		MessageBox("装载密码失败!");
		return;
	}
	st = rf_authentication(icdev,m_keymode,Sec);
    if(st)
	{
		MessageBox("认证失败!");
    	return;
	}

     	if(m_Num == "" )
		{
			MessageBox("请确认是否有输入三个块的数据!","读写测试",MB_OK|MB_ICONWARNING);
			return;
		}
	   	unsigned char temp[33];
		memset(temp,0,17);
		memset(data,0,17);
		a_hex(m_Num.GetBuffer(16),temp,16);
		m_Num.ReleaseBuffer();
		memcpy(data,temp,8);
		st = rf_write(icdev,Sec*4,data);
		memcpy(temp,m_Name.GetBuffer(16),16);   
        m_Name.ReleaseBuffer();
    	sn=rf_swr_eeprom(icdev,100,16,temp);
		memcpy(temp,m_Duty.GetBuffer(16),16);   
        m_Duty.ReleaseBuffer();
		sd=rf_swr_eeprom(icdev,200,16,temp);
		if(st&sn&sd)
		{
			MessageBox("写数据失败!");
			return;
		}
        
	  MessageBox("写数据成功!");

	rf_beep(icdev,10);
	
    UpdateData(FALSE);
	st = rf_halt(icdev);
	sn = rf_halt(icdev);

}

void CIC_DesignDlg::OnReadblock() 
{ 
	 UpdateData(TRUE);
    unsigned char Sec = 1;
    __int16 st,sN,sm,sd;
    unsigned __int16 TagType;
	unsigned long Snr;
	unsigned char Size;
    unsigned char data[33];
	memset(data,0,33);
	
	st = rf_reset(icdev,5);
	
	st = rf_request(icdev,1,&TagType);
	if(st)
	{
			MessageBox("寻卡失败!");
		return;
	}
	st = rf_anticoll(icdev,0, &Snr);
	if(st)
	{
		MessageBox("防冲突失败!");
		return;
	}
	st = rf_select(icdev,Snr,&Size);
	if(st)
	{
		MessageBox("选卡失败!");
		return;
	}
	unsigned char key[7];
	memset(key,0,7);
	a_hex(m_Key.GetBuffer(12),key,12);
	m_Key.ReleaseBuffer();
	unsigned char m_keymode;
	if(m_KeyModelCtrl.GetCurSel()==0)
		m_keymode = 0;
	else
		m_keymode = 4;
	st = rf_load_key(icdev,m_keymode,Sec,key);
	if(st)
	{
		MessageBox("装载密码失败!");
		return;
	}
	st = rf_authentication(icdev,m_keymode,Sec);
    if(st)
	{
		MessageBox("认证失败!");

		return;
	}

    unsigned char temp[16];
	memset(temp,0,16);
	sN= rf_read(icdev,Sec*4,Num);
	hex_a(Num,(char*)temp,4);
	m_Num.Format("%s",temp);
	m_IC.Format("%u",Snr);
	sm=rf_srd_eeprom(icdev,100,16,temp);
    m_Name.Format("%s",temp);
	sd=rf_srd_eeprom(icdev,200,16,temp);
    m_Duty.Format("%s",temp);
	SYSTEMTIME sy;
    CString strDate,strTime,str,str1;
	GetLocalTime(&sy);
    strDate.Format("%4d.%2d.%2d",sy.wYear,sy.wMonth,sy.wDay);
    strTime.Format("%2d:%2d:%2d",sy.wHour,sy.wMinute,sy.wSecond);
    m_Time=strTime;
	m_Date.Format("%s %s",strDate,strTime);
//	GetSystemTime(&systm); 
	if(sN )
	{
		   MessageBox("读数据失败!");
		   return;
	}   

	
		MessageBox("读数据成功!");
		UpdateData(FALSE); 
		try 
	{
		UpdateData();
		m_pRecordset->AddNew();
    	
    	m_pRecordset->PutCollect("IC卡序列号",_variant_t(m_IC));
		m_pRecordset->PutCollect("职工序号", _variant_t(m_Num));
		m_pRecordset->PutCollect("职工姓名",_variant_t(m_Name));
		m_pRecordset->PutCollect("职工职务",_variant_t(m_Duty));
		//m_pRecordset->PutCollect("刷卡时间",_variant_t(m_Time));
	    m_pRecordset->PutCollect("刷卡时间",_variant_t(m_Date));
		m_pRecordset->Update();
	}
	catch (_com_error e)
	{
    	AfxMessageBox("导入数据失败!");	
	}
		 



}


⌨️ 快捷键说明

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