?? readcarddlg.cpp
字號:
// 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 + -