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

📄 readcarddlg.cpp

📁 不用控制器,对读卡头进行读写.可设置根据读卡器类型不同而进行长度的设置
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// readcardDlg.cpp : implementation file
//

#include "stdafx.h"
#include "readcard.h"
#include "readcardDlg.h"
#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CReadcardDlg dialog
double hextodec(char *str)
{
	char c,a[100];
	double sum=0,n=1;
	int i=0,j=0;
	do
	{
       c=str[i];
	   if(c=='a'||c=='A')      c=10+48;
	   else if(c=='b'||c=='B') c=11+48;
	   else if(c=='c'||c=='C') c=12+48;
	   else if(c=='d'||c=='D') c=13+48;
	   else if(c=='e'||c=='E') c=14+48;
	   else if(c=='f'||c=='F') c=15+48;
	   else if(c>'f'||c>'F' || c<'0'||c>'9')   
	   {
		return -1;
	   }
		a[i]=c-48;//将字符类型转换为 
		i++;		
	} while (strlen(str)>i);
	i--;
	for( j=0;j<=i;j++)
	{
	   n=pow(16,i-j);
	   sum=sum+a[j]*n;
	}
	return sum;
}



BOOL DebugTrace(char * lpszFormat,...)
{
	static HWND hwnd = ::FindWindowA(NULL,	"DbgView");
	if(!IsWindow(hwnd))
		hwnd = ::FindWindowA(NULL, "DbgView");
	if(hwnd)
	{
		static char szMsg[512];
		va_list argList;
		va_start(argList, lpszFormat);
		try
		{
			vsprintf(szMsg,lpszFormat, argList);
		}
		catch(...)
		{
			strcpy(szMsg ,"DebugHelper:Invalid string format!");
		}
		va_end(argList);
		DWORD dwId = GetCurrentProcessId();
		::SendMessage(hwnd,WM_SETTEXT,dwId,(LPARAM)(LPCTSTR)szMsg);
	}
	return TRUE;
}

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

void CReadcardDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReadcardDlg)
	DDX_Text(pDX, IDC_HELPER, m_helper);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReadcardDlg, CDialog)
	//{{AFX_MSG_MAP(CReadcardDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnSetId)
	ON_BN_CLICKED(IDC_BUTTON3, OnRRead)
	ON_BN_CLICKED(IDC_BTN, OnBtnID)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadcardDlg message handlers
char ConvertHexChar(char ch)
{
    if((ch>='0')&&(ch<='9'))
        return ch-0x30;
	else if((ch>='A')&&(ch<='F'))
		return ch-'A'+10;
	else if((ch>='a')&&(ch<='f'))
	    return ch-'a'+10;
	else return(-1);
}
int String2Hex(CString str,CByteArray &senddata)
{
   int hexdata,lowhexdata;
   int hexdatalen=0;
   int len=str.GetLength();
   senddata.SetSize(len/2);
   for(int i=0;i<len;) 
   {
	    char lstr,hstr=str[i];
		if(hstr==' ')
		{
			i++;
			continue;
		}
		i++;
		if(i>=len)
	        break;
	    lstr=str[i];
	    hexdata=ConvertHexChar(hstr);
	    lowhexdata=ConvertHexChar(lstr);
	    if((hexdata==16)||(lowhexdata==16))
			break;
		else
			hexdata=hexdata*16+lowhexdata;
		i++;
		senddata[hexdatalen]=(char)hexdata;
		hexdatalen++;
   }
   senddata.SetSize(hexdatalen);
   return hexdatalen;
}
BOOL CReadcardDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
/*	comno    = GetPrivateProfileInt("COM","com",1, CONFIGFINE);
    baudrate = GetPrivateProfileInt("COM","baudrate",19200, CONFIGFINE);
	conno    = GetPrivateProfileInt("COM","controlno",1, CONFIGFINE);
	cardlength = GetPrivateProfileInt("COM","cardlength",6, CONFIGFINE);*/
	((CButton *)GetDlgItem(IDC_PR210))->SetCheck(1);
	((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COM))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_CARDLEN))->SetCurSel(1);
	CString str="操作说明:";
	str+='\r';
	str+="1、设地址编号:输入模块编号,选择ID号 ";
	str+='\r';
	str+="2、读卡、重读:选择ID号,点击读卡或重读";
	str+='\r';
	str+="3、读控制器ID: 选择读卡器编号,再读取读卡器ID号";
	SetDlgItemText(IDC_HELPER,str);
	SetWindowText("读卡器操作工具");
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CReadcardDlg::OnButton1() 
{
	CString port,OutBuffer;
	LONG Ret=0,length=0,whil=0,datalength=0;
	char bcc=0,tmp[50],*point;
	BOOL mmt=((CButton *)GetDlgItem(IDC_MMT))->GetCheck();
	CSerial serial;
	memset(tmp,0x00,sizeof(tmp));
	GetDlgItemText(IDC_COM,port);
	Ret=serial.Open(_T(port));
	if(Ret == ERROR_SUCCESS)
	{
	bcc=tmp[0]=9;
	if (!mmt)//普通头
	   tmp[1]='A';
	else    //密码头
       tmp[1]='H';
	GetDlgItemText(IDC_COMBO1,OutBuffer); //读卡器编号ID
	tmp[2]=OutBuffer[0];
	tmp[3]='F';
	for(int i=1;i<=3;i++)
		bcc^=tmp[i];
    if (((tmp[4] = ((bcc&0xf0)>>4)|0x30)) > '9')
		tmp[4]+=7;
    if ((tmp[5] = (bcc&0x0f)|0x30) > '9')
		tmp[5]+=7;
	tmp[6]=0x0d;
	tmp[7]=0x00;
    OutBuffer.Format("09 %02x %02x %02x %02x %02x 0D",tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]);
    serial.Setup(CSerial::EBaud19200,CSerial::EData8,CSerial::EParEven,CSerial::EStop1);
	SetDlgItemText(IDC_SNDDATA,OutBuffer);
    datalength=strlen(tmp);
	Ret=serial.Write(tmp,datalength,0,0,3);
	if (Ret!=ERROR_SUCCESS)
	{
		serial.Close();
		CString errcode;
		errcode.Format("写数据到串口失败.errcode=%d",Ret);		
		MessageBox(errcode);
		return;
	} 
	whil=serial.WaitEvent (0,180);
	if (whil==0)
	 {
        char lencard[2];
		GetDlgItemText(IDC_CARDLEN,OutBuffer);
		strcpy(lencard,OutBuffer);
		memset(tmp,0x00,sizeof(tmp));
		Sleep(50);
		Ret=serial.Read(tmp,30,0,0,10);   //取返回信息
		Sleep(50);
		length=0;
		while (tmp[length]!=0)
		{
			if(tmp[length]==13)
				break;
			else
			{
				if(tmp[length]>9)
					length++;
				else
					tmp[length]=0x00;
			}
		}
		if (Ret!=ERROR_SUCCESS)
		{
			CString errcode;
			errcode.Format("读取串口数据失败.errcode=%d",Ret);		
			MessageBox(errcode);
			return;
		}
		if (length > 8 && tmp[4]!='3')
		{
			char card[18];
			memset(card,0,sizeof(card));
			length=length-4-3;
		    memcpy(card,&tmp[5],length);
			////////////////////////////////////////////////////////////////////////// 去前面为0的部份
			int sit=0;
			while (card[sit]=='0')
				sit++;
			memset(tmp,0x00,sizeof(tmp));
			strcpy(tmp,&card[sit]);
			strcpy(card,tmp);
			////////////////////////////////////////////////////////////////////////// 去前面为0的部份
			if((((CButton *)GetDlgItem(IDC_DCHK))->GetCheck())==true)
			{
				memset(tmp,0x00,sizeof(tmp));
				sprintf(tmp,"%lf",hextodec(card));
				point=strchr(tmp,'.');
				sit=point-tmp;
				tmp[sit]=0x00;
				memcpy(card,tmp,sit+1);
			}
    		SetDlgItemText(IDC_CARDINFO,card);//取要设置的读卡器ID号 
		}
		else
		{
			memset(tmp,0x00,sizeof(tmp));
			SetDlgItemText(IDC_CARDINFO,tmp);
		}
	 }
	else
	{
			CString errcode;
			errcode.Format("等待串口数据失败.errcode=%d",whil);		
			MessageBox(errcode);
	}
	serial.Close();
	}
	else
	{
			CString errcode;
			errcode.Format("打开串口失败.errcode=%d",Ret);		
			MessageBox(errcode);
			return;
	}
}

void CReadcardDlg::OnSetId() 
{
	CString port,OutBuffer;
	LONG Ret=0,length=0,whil=0;
	BOOL readcard=TRUE,mmt=((CButton *)GetDlgItem(IDC_MMT))->GetCheck();
    char bcc=0,tmp[100];
	CSerial serial;
	memset(tmp,0x00,sizeof(tmp));
	GetDlgItemText(IDC_COM,port);
    Ret=serial.Open(_T(port));
	if (Ret == ERROR_SUCCESS)
    {
    bcc=tmp[0]=9;
	if (!mmt)//普通头
	   tmp[1]='A';
	else    //密码头
       tmp[1]='H';
	GetDlgItemText(IDC_COMBO1,OutBuffer);
	tmp[2]=OutBuffer[0];
	tmp[3]='B';
	for(int i=1;i<=3;i++)
		bcc^=tmp[i];
	//BCD to ASC
    if ((tmp[4] = (bcc&0xf0)>>4|0x30) > '9')
		tmp[4]+=7;
    if ((tmp[5] = (bcc&0x0f)|0x30) > '9')
		tmp[5]+=7;
	tmp[6]=0x0d;
	tmp[7]=0;
    OutBuffer.Format("09 %02x %02x %02x %02x %02x 0D",tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]);

⌨️ 快捷键说明

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