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

📄 ckzzdlg.cpp

📁 this a C++ example for read card
💻 CPP
字号:
//----------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "ckzzDLG.h"
#pragma resource "*.dfm"
TckDlg *ckDlg;

const char  STX = 0x02, ETX = 0x03;
const char  *SS1 = "%", *SS2 = ";", *SS3 = "+", *ES = "?";
const int   tk1MaxLen = 76, tk2MaxLen = 37, tk3MaxLen = 104;
const AnsiString DEF_CommPort = "COM1";
const int        DEF_BaudRate = 9600;
const AnsiString DEF_Parity = "O";

//////////////////////////////////////////////////////////////////////////////////
__fastcall TckDlg::TckDlg(TComponent *Owner, char *wStr2, char *wStr1,char *wStr3)
	: TForm(Owner)
{
    GetIniCfg();
    Result = false;

    int len1 = lstrlen(wStr1);
    int len2 = lstrlen(wStr2);
    int len3 = lstrlen(wStr3);
    if(len1 > tk1MaxLen-2)
      Application->MessageBox("第一轨字符串太长,无法写入!", "磁卡", MB_OK);
    else if(len2 > tk2MaxLen-2)
      Application->MessageBox("第二轨字符串太长,无法写入!", "磁卡", MB_OK);
    else if(len3 > tk3MaxLen-2)
      Application->MessageBox("第三轨字符串太长,无法写入!", "磁卡", MB_OK);
    else if(len1+len2+len3 > MAXSTRLEN-8)
      Application->MessageBox("字符串太长,无法写入!", "磁卡", MB_OK);

	readBuf[0] = 0;
    char tempBuf[2];
    tempBuf[0] = STX;
    tempBuf[1] = 0;
    lstrcpy(writeBuf, tempBuf);
    lstrcat(writeBuf, SS1);
    lstrcat(writeBuf, wStr1);
    lstrcat(writeBuf, ES);
    lstrcat(writeBuf, SS2);
    lstrcat(writeBuf, wStr2);
    lstrcat(writeBuf, ES);
    lstrcat(writeBuf, SS3);
    lstrcat(writeBuf, wStr3);
    lstrcat(writeBuf, ES);
	tempBuf[0] = ETX;
    tempBuf[1] = 0;
    lstrcat(writeBuf, tempBuf);
    wLen = strlen(writeBuf);

	IsWritten = false;

	hComId = CreateFile(CommPort.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL,
                        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
	if(hComId == INVALID_HANDLE_VALUE)
    {  Application->MessageBox("无法打开通讯口!", "磁卡制作", MB_OK);
       return;
    }
	if( !SetupComm(hComId, 1024, 256) )
    {  Application->MessageBox("无法建立通讯!", "磁卡制作", MB_OK);
       return;
    }
  	GetCommState(hComId,&comDcb);
  	comDcb.BaudRate = BaudRate;
  	comDcb.ByteSize = 7;
  	comDcb.Parity   = Parity;
  	comDcb.StopBits = 0;
	if(!SetCommState(hComId,&comDcb))
	   Application->MessageBox("通讯设置无效!","磁卡制作",MB_OK);

	PurgeComm(hComId, PURGE_TXCLEAR);
	PurgeComm(hComId, PURGE_RXCLEAR);

    //create I/O event used for overlapped read
    os.hEvent = CreateEvent( NULL,    // 无安全属性
                             FALSE,   // 自动复位事件
                             FALSE,   // 无信号
                             NULL );  // 无名字
    GetCommTimeouts(hComId, &comTimeout);
    comTimeout.ReadTotalTimeoutMultiplier = 20;
    SetCommTimeouts(hComId, &comTimeout);
    	bool write = WriteFile(hComId, writeBuf, wLen, &wLen, NULL);


    if( !write )
    {  Application->MessageBox("无法写入数据!","磁卡制作", MB_OK);
       Close();
    }
    else
      IsWritten = true;

    ClearCommError( hComId, &dwErrorFlags, &ComStat ) ;
}

//------------------------------------------------------------------------
void __fastcall TckDlg::GetIniCfg()
{
    TIniFile *cfgFile = new TIniFile(".\\ckzz.ini");

    CommPort = cfgFile->ReadString("Communication",  "Port", DEF_CommPort);
    if(CommPort.IsEmpty())
      CommPort = DEF_CommPort;
    BaudRate = cfgFile->ReadInteger("Communication", "BaudRate", DEF_BaudRate);
    AnsiString p = cfgFile->ReadString("Communication", "Parity",DEF_Parity );
    if(p.IsEmpty())
      p = DEF_Parity;
    if( p == "n" || p == "N")         // 0-4=no,odd,even,mark,space
       Parity = 0;
    else if( p == "o" || p == "O")
       Parity = 1;
    else if( p == "e" || p == "E")
       Parity = 2;
    else
       Parity = 0;

    delete cfgFile;
}

//------------------------------------------------------------------------
void __fastcall TckDlg::FormCreate(TObject *Sender)
{
    lbPromptText->Caption = "请将磁卡平行匀速地刷过滑槽";

}
//---------------------------------------------------------------------------

void __fastcall TckDlg::FormDestroy(TObject *Sender)
{
	CloseHandle(hComId);
}
//---------------------------------------------------------------------------


void __fastcall TckDlg::ckTimerTimer(TObject *Sender)
{
	if( !IsWritten )
       return;

	int n = ReadCom();
	if( n <= 0 )
	   return;
	else
    {  if( memicmp(readBuf, writeBuf, wLen) == 0)
        {
               
                Result = true;      //已成功的写入磁卡 ,作为向外部传送数据的标记.
                MessageBeep(20);

                Close();

        }
	   else
	   {  IsWritten = false;
       	  PurgeComm(hComId, PURGE_TXCLEAR);     //Retry
	      PurgeComm(hComId, PURGE_RXCLEAR);
          bool write = WriteFile(hComId, writeBuf, wLen, &wLen, NULL);
	      if( !write )
	      {  Application->MessageBox("无法写入数据!", "磁卡制作", MB_OK);
             Close();
          }
          IsWritten = true;
          lbPromptText->Caption = "磁卡写入错误,\n请再试一次!";
          ClearCommError( hComId, &dwErrorFlags, &ComStat );
       }
    }
}

int __fastcall TckDlg::ReadCom()
{
	char buf;
	int  num = 0;
	unsigned long rLen=0;

	while(1)
	{
       BOOL read = ReadFile(hComId, &buf, 1, &rLen, &os);
	   if( !read )
	   {  DCB dcb;
		  if( GetCommState(hComId, &dcb) != 0)
             return -1;
          else
		     return 0;
	   }
	   else
	   {  readBuf[num] = buf;
          if( buf == ETX)
             return num+1;
	      num += rLen;
		  if(num >= int(wLen))
             return num;
	   }
	}
}

void __fastcall TckDlg::BitBtn1Click(TObject *Sender)
{
        Close();        
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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