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

#include <vcl.h>
#pragma hdrstop

#include <stdio.h>
#include "Unit1.h"
#include "MagAPI.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int cmdtype = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    MemoTK1->Text = "";
    MemoTK2->Text = "";
    MemoTK3->Text = "";
    ComboBoxCOMSelect(this);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBoxCOMSelect(TObject *Sender)
{
    char ver[256];
    unsigned char bpi;
    unsigned char CoStu;
    char port[32];
    unsigned int tick;

    String str;
    char temp[256];

    if ((ComboBoxCOM->ItemIndex >= 0)&&(ComboBoxCOM->ItemIndex < 9))
    {
        sprintf(port, "%s",ComboBoxCOM->Text.c_str());
    }
    else
    {
        sprintf(port, "\\\\.\\%s",ComboBoxCOM->Text.c_str());
    }

    MSR_ExitComm();
    if(MSR_InitComm(port,9600)==0)
    {
    	sprintf(temp, " %s 9600,N,8,1", port);
        str = ""; str+= temp;
        Form1->StatusBar1->Panels->Items[0]->Text = str;

   	    MSR_DoCancel();
    	tick = GetTickCount();
    	while ((unsigned int)GetTickCount()-tick <5){};

		if (MSR_Get_Firmware_Version(ver)=='0')
        {
        	sprintf(temp, " Firmware: %s", ver);
        	str = ""; str+= temp;
        	Form1->StatusBar1->Panels->Items[1]->Text = str;

            if (Form1->RadioHiLoCo->ItemIndex) MSR_Set_HiCo();
    		else MSR_Set_LoCo();

            Form1->Button_Write->Enabled = true;
            Form1->Button_Read->Enabled = true;
            Form1->RadioHiLoCo->Enabled = true;
        }
        else
        {
            Form1->Button_Write->Enabled = false;
            Form1->Button_Read->Enabled = false;
            Form1->RadioHiLoCo->Enabled = false;

        	sprintf(temp, " Not Find Reader/Writer", ver);
        	str = ""; str+= temp;
        	Form1->StatusBar1->Panels->Items[1]->Text = str;
			MessageDlg("Not Find Reader/Writer!!", mtInformation, TMsgDlgButtons()<<mbOK, 0);
        }
    }
    else
    {
        Form1->Button_Write->Enabled = false;
        Form1->Button_Read->Enabled = false;
        Form1->RadioHiLoCo->Enabled = false;

    	Form1->StatusBar1->Panels->Items[0]->Text = "";
        Form1->StatusBar1->Panels->Items[1]->Text = "";
        MessageDlg("Init COM port Error!!", mtInformation, TMsgDlgButtons()<<mbOK, 0);
    }
    Form1->Enabled = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_ReadClick(TObject *Sender)
{
    if (MSR_Read() != '0') return;
    cmdtype = 1;
        Form1->Button_Write->Enabled = false;
        Form1->Button_Read->Enabled = false;
        Form1->RadioHiLoCo->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button_WriteClick(TObject *Sender)
{
    if (MSR_Write(MemoTK1->Text.c_str(), MemoTK2->Text.c_str(), MemoTK3->Text.c_str()) != '0') return;
    cmdtype = 2;
        Form1->Button_Write->Enabled = false;
        Form1->Button_Read->Enabled = false;
        Form1->RadioHiLoCo->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ButtonDoCancelClick(TObject *Sender)
{
    MSR_DoCancel();
    cmdtype = 0;
        Form1->Button_Write->Enabled = true;
        Form1->Button_Read->Enabled = true;
        Form1->RadioHiLoCo->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonDoReadClick(TObject *Sender)
{
    int stu, len;
    char TK1Dat[256], TK2Dat[256], TK3Dat[256];
    String str;
    char code[256];

	stu = MSR_GetTrackData(TK1Dat, TK2Dat, TK3Dat);
	if (stu <= 0) return;
    cmdtype = 0;
            Form1->Button_Write->Enabled = true;
            Form1->Button_Read->Enabled = true;
            Form1->RadioHiLoCo->Enabled = true;

        // read card successful , track data is return by TK1Dat, TK2Dat, TK3Dat
        memset(code, 0, 256);
        len = strlen(TK1Dat);
        if (len>=2)
        {
            memcpy(code, &TK1Dat[1], len-2);    // 去掉起始和结束符
        }
        str = "";
        str += code;
        MemoTK1->Text = str;

        memset(code, 0, 256);
        len = strlen(TK2Dat);
        if (len>=2)
        {
            memcpy(code, &TK2Dat[1], len-2);    // 去掉起始和结束符
        }
        str = "";
        str += code;
        MemoTK2->Text = str;

        memset(code, 0, 256);
        len = strlen(TK3Dat);
        if (len>=2)
        {
            memcpy(code, &TK3Dat[1], len-2);    // 去掉起始和结束符
        }
        str = "";
        str += code;
        MemoTK3->Text = str;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonDoWriteClick(TObject *Sender)
{
    int stu;

    stu = MSR_Get_Write_Erase_Status();
    if (stu <= 0) return;
    cmdtype = 0;
            Form1->Button_Write->Enabled = true;
            Form1->Button_Read->Enabled = true;
            Form1->RadioHiLoCo->Enabled = true;

    if (stu != '0')
    {
		MessageDlg("Write Error!!", mtInformation, TMsgDlgButtons()<<mbOK, 0);
    }
    else
    {
		MessageDlg("Write OK!!", mtInformation, TMsgDlgButtons()<<mbOK, 0);
	}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    switch (cmdtype)
    {
    case 1:
        ButtonDoReadClick(this);
        break;

    case 2:
        ButtonDoWriteClick(this);
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::RadioHiLoCoClick(TObject *Sender)
{
    if (RadioHiLoCo->ItemIndex)
    	MSR_Set_HiCo();
    else
    	MSR_Set_LoCo();
}
//---------------------------------------------------------------------------

