//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "SynPublic.h"
#include <stdio.h>
#include <FileCtrl.hpp>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
        char sMsg[1024];
        int nPort=0;
        unsigned int nCurbaud;
        nPort = Syn_FindReader();
        if(nPort ==0)
        {
                sprintf(sMsg,"%s  没有找到读卡器",Now().DateTimeString());
        }
        else
        {
                Beep(4000,200);
                if(nPort > 1000)
                {
                        sprintf(sMsg,"%s  读卡器连接在USB %d 上",Now().DateTimeString(),nPort );
                        Edit1->Text = nPort;
                        Edit3->Enabled = false;
                        Button3->Enabled = false;
                }
                else
                {
                        Syn_GetCOMBaud(nPort,&nCurbaud);
                        sprintf(sMsg,"%s  读卡器连接在串口 %d 上",Now().DateTimeString(),nPort);
                        Edit1->Text = nPort;
                        Edit3->Enabled = true;
                        Edit3->Text = nCurbaud;
                        Button3->Enabled = true;
                }
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button19Click(TObject *Sender)
{
        Application->Terminate();        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        int nMaxByte,nPort;
        nPort = StrToInt(Edit1->Text);
        nMaxByte = StrToInt(Edit2->Text);
        nRet = Syn_OpenPort(nPort);
        if(nRet == 0)
        {
                if(Syn_GetSAMStatus(nPort,0) == 0)
                {
                        if(Syn_SetMaxRFByte(nPort,nMaxByte,0) == 0)
                        {
                                sprintf(sMsg,"%s  设置最大通讯字节数成功,最大通讯字节数为 %d!",Now().DateTimeString(),nMaxByte);
                        }
                        else
                        {
                                sprintf(sMsg,"%s  设置最大通讯字节数失败!",Now().DateTimeString());
                        }
                }
                else
                {
                        sprintf(sMsg,"%s  端口上没有连接读卡器!",Now().DateTimeString());
                }
        }
        else
        {
                sprintf(sMsg,"%s  打开端口失败!",Now().DateTimeString());
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
        char sMsg[1024];
        int nPort,nRet;
        unsigned int nCurbaud,nSetbaud;
        nPort = StrToInt(Edit1->Text);
        nRet = Syn_GetCOMBaud(nPort,&nCurbaud);
        if(nRet != 0)
        {
                sprintf(sMsg,"%s  获得当前读卡器波特率失败",Now().DateTimeString());
                ListBox1->Items->Add(sMsg);
                return;
        }
        nSetbaud = StrToInt(Edit3->Text);
        if(Syn_OpenPort(nPort) == 0)
        {
                if(Syn_SetCOMBaud(nPort,nCurbaud,nSetbaud) == 0)
                {
                        sprintf(sMsg,"%s  设置读卡器波特率成功,当前读卡器波特率为 %d",Now().DateTimeString(),nSetbaud);
                }
                else
                {
                        sprintf(sMsg,"%s  设置读卡器波特率失败",Now().DateTimeString());
                }
        }
        else
        {
                sprintf(sMsg,"%s  打开端口失败",Now().DateTimeString());
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button18Click(TObject *Sender)
{
        ListBox1->Items->Clear();        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
        char sMsg[1024];
        char cPath[256]={0};
        int nRet;
        nRet = Syn_SetPhotoPath(0,cPath);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存路径设置为C:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存路径失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
        char sMsg[1024];
        char cPath[256]={0};
        int nRet;
        nRet = Syn_SetPhotoPath(1,cPath);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存路径设置当前路径:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存路径失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);

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

void __fastcall TForm1::Button6Click(TObject *Sender)
{
        char sMsg[1024];
        AnsiString Path;
        char cPath[256];
        int nRet;
        if(SelectDirectory("请选择目录","",Path))
        {
                sprintf(cPath,"%s",Path);
        }
        else
        {
                sprintf(cPath,"C:\\");
        }
        nRet = Syn_SetPhotoPath(2,cPath);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存路径设置为:%s,nRet = %d",Now().DateTimeString(),cPath,nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存路径失败,nRet = %d",Now().DateTimeString(),nRet);
        }

        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoType(0);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存格式设置为Bmp:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存格式失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button8Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoType(1);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存格式设置为Jpeg:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存格式失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button9Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoType(2);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存格式设置为base64:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存格式失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button10Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoName(0);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存文件名设置为tmp:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存文件名失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button11Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoName(1);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存文件名设置为姓名:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存文件名失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button12Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoName(2);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存文件名设置为身份证号:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存文件名失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);
        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button13Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        nRet = Syn_SetPhotoName(3);
        if(nRet == 0)
        {
                sprintf(sMsg,"%s  照片保存文件名设置为姓名_身份证号:,nRet = %d",Now().DateTimeString(),nRet);
        }
        else
        {
                sprintf(sMsg,"%s  设置照片保存文件名失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        ListBox1->Items->Add(sMsg);

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

void __fastcall TForm1::Button14Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        int nPort;
        nPort = StrToInt(Edit1->Text);
        nRet = Syn_OpenPort(nPort);
        if(nRet == 0)
        {
                nRet = Syn_ResetSAM(nPort,0);
                if(nRet == 0)
                {
                        sprintf(sMsg,"%s  复位SAM模块成功,nRet = %d",Now().DateTimeString(),nRet);
                }
                else
                {
                        sprintf(sMsg,"%s  复位SAM模块失败,nRet = %d",Now().DateTimeString(),nRet);
                }
        }
        else
        {
                sprintf(sMsg,"%s  打开端口失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        Syn_ClosePort(nPort);
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button15Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        int nPort;
        nPort = StrToInt(Edit1->Text);
        nRet = Syn_OpenPort(nPort);
        if(nRet == 0)
        {
                nRet = Syn_GetSAMStatus(nPort,0);
                if(nRet == 0)
                {
                        sprintf(sMsg,"%s  SAM模块状态正常,nRet = %d",Now().DateTimeString(),nRet);
                }
                else
                {
                        sprintf(sMsg,"%s  SAM模块状态不正常,nRet = %d",Now().DateTimeString(),nRet);
                }
        }
        else
        {
                sprintf(sMsg,"%s  打开端口失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        Syn_ClosePort(nPort);
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button16Click(TObject *Sender)
{
        char sMsg[1024];
        int nRet;
        int nPort;
	unsigned char cSAMID[16];
	unsigned int uSAMID[4];
	memset(cSAMID,0,16);
        nPort = StrToInt(Edit1->Text);
        nRet = Syn_OpenPort(nPort);
        if(nRet == 0)
        {
                nRet = Syn_GetSAMID( nPort , cSAMID,0 );
                if(nRet == 0)
                {
                        memcpy(uSAMID,cSAMID,16);
                        sprintf(sMsg,"%s  读取SAM模块ID成功,SAM模块ID为: %X%X-%X%X-%u-%u-%u",Now().DateTimeString(),cSAMID[1],cSAMID[0],cSAMID[3],cSAMID[2],uSAMID[1],uSAMID[2],uSAMID[3]);
                }
                else
                {
                        sprintf(sMsg,"%s  读取SAM模块ID失败,nRet = %d",Now().DateTimeString(),nRet);
                }
        }
        else
        {
                sprintf(sMsg,"%s  打开端口失败,nRet = %d",Now().DateTimeString(),nRet);
        }
        Syn_ClosePort(nPort);
        ListBox1->Items->Add(sMsg);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button17Click(TObject *Sender)
{
	int nRet,nPort;
	char sMsg[1024];
	unsigned char pucIIN[4];
	unsigned char pucSN[8];
	IDCardData idcardData;
	Syn_SetSexType(1);
	Syn_SetNationType(1);
	Syn_SetBornType(2);
	Syn_SetUserLifeBType(3);
	Syn_SetUserLifeEType(4,1);

	nPort = StrToInt(Edit1->Text);
	nRet = Syn_OpenPort(nPort);
	if (nRet == 0)
	{
		if ( Syn_SetMaxRFByte( nPort ,80 , 0) == 0)
		{
			Syn_StartFindIDCard( nPort , pucIIN , 0 );
			Syn_SelectIDCard( nPort , pucSN , 0 );
			if (Syn_ReadMsg( nPort , 0 , &idcardData) == 0)
			{
				sprintf(sMsg, "%s  读取身份证信息成功!",Now().DateTimeString());
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  姓名:%s",Now().DateTimeString(),idcardData.Name);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  性别:%s",Now().DateTimeString(),idcardData.Sex);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  民族:%s",Now().DateTimeString(),idcardData.Nation);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  出生:%s",Now().DateTimeString(),idcardData.Born);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  住址:%s",Now().DateTimeString(),idcardData.Address);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  身份证号:%s",Now().DateTimeString(),idcardData.IDCardNo);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  发证机关:%s",Now().DateTimeString(),idcardData.GrantDept);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  有效期开始:%s",Now().DateTimeString(),idcardData.UserLifeBegin);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  有效期结束:%s",Now().DateTimeString(),idcardData.UserLifeEnd);
				ListBox1->Items->Add(sMsg);
				sprintf(sMsg,"%s  照片:%s",Now().DateTimeString(),idcardData.PhotoFileName);
				ListBox1->Items->Add(sMsg);
			}
			else
			{
				sprintf(sMsg,"%s  读取身份证信息错误!",Now().DateTimeString());
				ListBox1->Items->Add(sMsg);
			}
		}
	}
	else
	{
		sprintf(sMsg , "%s  打开端口错误",Now().DateTimeString());
		ListBox1->Items->Add(sMsg);
	}
	Syn_ClosePort( nPort );
}
//---------------------------------------------------------------------------

