using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.IO;
using RogerMfReaderExample.Mifare;

namespace RogerMfReaderExample
{

   
    public partial class Form1 : Form
    {
        private loadRogerLibrary laodLib = new loadRogerLibrary();
        private Reader MyReader = new Reader();
        private Boolean _mfTabActive = true;

        enum eRUD_Type
        {
            Non,
            RUD_2_v2,
            RUD_3_v1,
            RUD_3_v2,
            RUD_4_v1
        };
        private eRUD_Type _rudType =eRUD_Type.Non;
        
        public Form1()
        {
            InitializeComponent();
        }

        //library version
        private void usbButtonGetVersion_Click(object sender, EventArgs e)
        {
            usbTextGetVersion.Text = MyReader.GetlibVersion();
        }


        //all rud3
        private void usbButtonGetDevice_Click(object sender, EventArgs e)
        {
            List<String[]> allRuds = new List<string[]>();

            allRuds = MyReader.FindAll();

            usbDeviceList.RowCount = 0;

            foreach (String[] number in allRuds)
            {
                String[] row = new String[] { usbDeviceList.RowCount.ToString(), number[0], number[1], number[2], number[3] };         //serial number, description, port
                usbDeviceList.Rows.Add(row);

            }

            usbDeviceList.AutoSize = true;

        }

        //Connect the selected device or disconect 
        private void usbButtonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (usbButtonConnect.Text.Equals("Disconnect") == false)                                //Connected
                {
                    //try connect
                    if (MyReader.connect(usbDeviceList.CurrentRow.Cells["Serial"].Value.ToString()))
                    {
                        usbConnectedDevice(true);

                        tabControl1.TabPages.Remove(tabPage2);
                        tabControl1.TabPages.Remove(tabPage3);
                        buttonMfClassicCardID.Enabled = false;

                        //Get rud type
                        string signature = MyReader.ReadSignature();
                        
                        _rudType =eRUD_Type.Non;
                        if (signature.Contains("RUD-2 v2."))
                        {
                            _rudType =eRUD_Type.RUD_2_v2;
                        }
                        else if (signature.Contains("RUD-3 v1.") || (signature.Equals("RUD-3-HK v1.")))
                        {
                            _rudType =eRUD_Type.RUD_3_v1;

                            //RUD3_v1 enable:
                            //Mifare Classic Card ID
                            //And Tab Mifare Classic
                            tabControl1.TabPages.Insert(1, tabPage2);
                            buttonMfClassicCardID.Enabled = true;
                        }
                        else if((signature.Contains("RUD-3 v2."))||(signature.Contains("RUD-3-DES v2.")))
                        { 
                            _rudType =eRUD_Type.RUD_3_v2;

                            //RUD3_v2 enable:
                            tabControl1.TabPages.Insert(1, tabPage3);
                        }
                        else if ((signature.Contains("RUD-4 v1."))||(signature.Contains("RUD-4-DES v1.")))
                        {
                            _rudType =eRUD_Type.RUD_4_v1;

                             //RUD4 enable:
                            tabControl1.TabPages.Insert(1, tabPage3);
                        }
                    }

                }
                else
                {
                    //try disconnect
                    if (MyReader.disconnect())
                    {
                        usbConnectedDevice(false);

                    }
                }
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }
        }


        //Change form when usb connect or disconect
        private void usbConnectedDevice(Boolean connectState)
        {
            usbButtonGetDevice.Enabled = !connectState;             //Block when connect 
            usbDeviceList.Enabled = !connectState;                  //Block when connect 


            if (connectState)
            {
                usbButtonConnect.Text = "Disconnect";
                InfoBuffer.messageTitle("connect - " + usbDeviceList.CurrentRow.Cells["Description"].Value.ToString() + " - " + usbDeviceList.CurrentRow.Cells["Serial"].Value.ToString());
            }
            else
            {
                usbButtonConnect.Text = "Connect the selected device";
                InfoBuffer.messageTitle("disconnect");
            }

        }

        //Set data in messageTextBox
        public void messageTextBox_AddText(String Text, Font TextFont, Color TextColor)
        {
            try
            {
                if (Text.Last().Equals(Environment.NewLine) == false)
                    Text += Environment.NewLine;


                while ((messeageTextBox.TextLength + Text.Length) > messeageTextBox.MaxLength)
                {

                    messeageTextBox.Select(0, messeageTextBox.Lines[0].Length + 1);
                    messeageTextBox.Cut();
                }

                messeageTextBox.Select(messeageTextBox.TextLength, 0);

                messeageTextBox.SelectionFont = TextFont;

                if (TextColor.IsKnownColor)
                    messeageTextBox.SelectionColor = TextColor;
                else
                    messeageTextBox.SelectionColor = Color.Red;

                messeageTextBox.AppendText(Text);
                messeageTextBox.ScrollToCaret();
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }

        }

        //Check is number
        private void textBoxMfClassicSector_TextChanged(object sender, EventArgs e)
        {
            try
            {
                TextBox testText = (TextBox)sender;

                if (testText.Text.Length > 0)
                    Convert.ToByte(testText.Text, 16);

            }
            catch (Exception exe)
            {

                InfoBuffer.error(exe.Message);
            }
        }

        //refresh tab data
        private void tabPage2_Enter(object sender, EventArgs e)
        {
            MfClassicSetSector();
            MfClassicSetBlock();
            MfClassicSetKey();
            MfClassicSetKeyType();

        }

        //past text - form 2
        private void buttonMfClassicPastText_Click(object sender, EventArgs e)
        {
            Form2 past = new Form2();

            past.ShowDialog();

            // Determine if the OK button was clicked on the dialog box.
            if (past.DialogResult == DialogResult.OK)
            {
                MfClassicSetData(past.Data);
                past.Dispose();
            }
            else
                past.Dispose();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
            string myPath = Environment.CurrentDirectory;
            DirectoryInfo test;
            do
            {
                 test  = Directory.GetParent(myPath);
                 myPath = test.ToString();
               messeageTextBox.AppendText(myPath);
            }
            while ((test.Name.Equals("SDK") == false) || (Directory.Exists(myPath) == false));

            if (Directory.Exists(myPath))
            {
                messeageTextBox.AppendText(Path.Combine(myPath, "Lib", "RogerMiafer"));
            }


            //string[] test2 = GetFileSystemEntries(Environment.CurrentDirectory, "SDK"); 

            messeageTextBox.ScrollToCaret();
        }

        //insert data to value box
        private void MfClassicSetData(Byte[] dataTab)
        {
            String[] strTab = new String[16];

            try
            {
                for (int i = 0; i < dataTab.Length; i++)
                {
                    strTab[i] = dataTab[i].ToString("X2");
                }

                MfClassicSetData(strTab);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }

        }

        private void MfClassicSetData(String data)
        {
            Char[] charTab = new Char[16];
            Byte[] byteTab = new Byte[16];

            try
            {
                if (data.Length > 16)
                    charTab = data.ToCharArray(0, 16);
                else
                    charTab = data.ToCharArray();

                for (int i = 0; i < charTab.Length; i++)
                    byteTab[i] = Convert.ToByte(charTab[i]);

                MfClassicSetData(byteTab);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }
        }

        private void MfClassicSetData(String[] dataTab)
        {
            String[] valueTab = new String[16];
            try
            {

                if (dataTab.Length > 16)
                    Array.Copy(dataTab, valueTab, 16);
                else
                    Array.Copy(dataTab, valueTab, dataTab.Length);

                textBoxMfClassicDataB0.Text = valueTab[0];
                textBoxMfClassicDataB1.Text = valueTab[1];
                textBoxMfClassicDataB2.Text = valueTab[2];
                textBoxMfClassicDataB3.Text = valueTab[3];
                textBoxMfClassicDataB4.Text = valueTab[4];
                textBoxMfClassicDataB5.Text = valueTab[5];
                textBoxMfClassicDataB6.Text = valueTab[6];
                textBoxMfClassicDataB7.Text = valueTab[7];
                textBoxMfClassicDataB8.Text = valueTab[8];
                textBoxMfClassicDataB9.Text = valueTab[9];
                textBoxMfClassicDataB10.Text = valueTab[10];
                textBoxMfClassicDataB11.Text = valueTab[11];
                textBoxMfClassicDataB12.Text = valueTab[12];
                textBoxMfClassicDataB13.Text = valueTab[13];
                textBoxMfClassicDataB14.Text = valueTab[14];
                textBoxMfClassicDataB15.Text = valueTab[15];

            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }


        }

        //get data from value box
        private Byte[] MfClassicGetValue()
        {
            Byte[] byteTab = new Byte[16];

            try
            {
                if (!textBoxMfClassicDataB0.Text.Equals(String.Empty))
                    byteTab[0] = Convert.ToByte(textBoxMfClassicDataB0.Text, 16);
                else
                    byteTab[0] = 0x00;

                if (!textBoxMfClassicDataB1.Text.Equals(String.Empty))
                    byteTab[1] = Convert.ToByte(textBoxMfClassicDataB1.Text, 16);
                else
                    byteTab[1] = 0x00;

                if (!textBoxMfClassicDataB2.Text.Equals(String.Empty))
                    byteTab[2] = Convert.ToByte(textBoxMfClassicDataB2.Text, 16);
                else
                    byteTab[2] = 0x00;

                if (!textBoxMfClassicDataB3.Text.Equals(String.Empty))
                    byteTab[3] = Convert.ToByte(textBoxMfClassicDataB3.Text, 16);
                else
                    byteTab[3] = 0x00;

                if (!textBoxMfClassicDataB4.Text.Equals(String.Empty))
                    byteTab[4] = Convert.ToByte(textBoxMfClassicDataB4.Text, 16);
                else
                    byteTab[4] = 0x00;

                if (!textBoxMfClassicDataB5.Text.Equals(String.Empty))
                    byteTab[5] = Convert.ToByte(textBoxMfClassicDataB5.Text, 16);
                else
                    byteTab[5] = 0x00;

                if (!textBoxMfClassicDataB6.Text.Equals(String.Empty))
                    byteTab[6] = Convert.ToByte(textBoxMfClassicDataB6.Text, 16);
                else
                    byteTab[6] = 0x00;

                if (!textBoxMfClassicDataB7.Text.Equals(String.Empty))
                    byteTab[7] = Convert.ToByte(textBoxMfClassicDataB7.Text, 16);
                else
                    byteTab[7] = 0x00;

                if (!textBoxMfClassicDataB8.Text.Equals(String.Empty))
                    byteTab[8] = Convert.ToByte(textBoxMfClassicDataB8.Text, 16);
                else
                    byteTab[8] = 0x00;

                if (!textBoxMfClassicDataB9.Text.Equals(String.Empty))
                    byteTab[9] = Convert.ToByte(textBoxMfClassicDataB9.Text, 16);
                else
                    byteTab[9] = 0x00;

                if (!textBoxMfClassicDataB10.Text.Equals(String.Empty))
                    byteTab[10] = Convert.ToByte(textBoxMfClassicDataB10.Text, 16);
                else
                    byteTab[10] = 0x00;

                if (!textBoxMfClassicDataB11.Text.Equals(String.Empty))
                    byteTab[11] = Convert.ToByte(textBoxMfClassicDataB11.Text, 16);
                else
                    byteTab[11] = 0x00;

                if (!textBoxMfClassicDataB12.Text.Equals(String.Empty))
                    byteTab[12] = Convert.ToByte(textBoxMfClassicDataB12.Text, 16);
                else
                    byteTab[12] = 0x00;

                if (!textBoxMfClassicDataB13.Text.Equals(String.Empty))
                    byteTab[13] = Convert.ToByte(textBoxMfClassicDataB13.Text, 16);
                else
                    byteTab[13] = 0x00;

                if (!textBoxMfClassicDataB14.Text.Equals(String.Empty))
                    byteTab[14] = Convert.ToByte(textBoxMfClassicDataB14.Text, 16);
                else
                    byteTab[14] = 0x00;

                if (!textBoxMfClassicDataB15.Text.Equals(String.Empty))
                    byteTab[15] = Convert.ToByte(textBoxMfClassicDataB15.Text, 16);
                else
                    byteTab[15] = 0x00;
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
                Array.Clear(byteTab, 0, byteTab.Length);
            }

            return byteTab;

        }

        //get setor
        private Byte MfClassicGetSector()
        {
            try
            {
                return Convert.ToByte(textBoxMfClassicSector.Text, 16);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
                return Byte.MinValue;
            }
        }

        //set setor
        private void MfClassicSetSector()
        {
            try
            {
                textBoxMfClassicSector.Text = MyReader.CardMifareClassic.sector.ToString("X2");
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);

            }
        }

        //get block
        private Byte MfClassicGetBlock()
        {
            try
            {
                return Convert.ToByte(textBoxMfClassicBlock.Text, 16);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
                return Byte.MinValue;
            }

        }

        //set block
        private void MfClassicSetBlock()
        {
            try
            {
                textBoxMfClassicBlock.Text = MyReader.CardMifareClassic.block.ToString("X2");
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);

            }

        }

        //get key
        private Byte[] MfClassicGetKey()
        {
            Byte[] dataKey = new Byte[6];

            try
            {
                dataKey[0] = Convert.ToByte(textBoxMfClassicKeyB0.Text, 16);
                dataKey[1] = Convert.ToByte(textBoxMfClassicKeyB1.Text, 16);
                dataKey[2] = Convert.ToByte(textBoxMfClassicKeyB2.Text, 16);
                dataKey[3] = Convert.ToByte(textBoxMfClassicKeyB3.Text, 16);
                dataKey[4] = Convert.ToByte(textBoxMfClassicKeyB4.Text, 16);
                dataKey[5] = Convert.ToByte(textBoxMfClassicKeyB5.Text, 16);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
                Array.Clear(dataKey, 0, dataKey.Length);
            }

            return dataKey;
        }

        //set key
        private void MfClassicSetKey()
        {
            try
            {
                textBoxMfClassicKeyB0.Text = MyReader.CardMifareClassic.key[0].ToString("X2");
                textBoxMfClassicKeyB1.Text = MyReader.CardMifareClassic.key[1].ToString("X2");
                textBoxMfClassicKeyB2.Text = MyReader.CardMifareClassic.key[2].ToString("X2");
                textBoxMfClassicKeyB3.Text = MyReader.CardMifareClassic.key[3].ToString("X2");
                textBoxMfClassicKeyB4.Text = MyReader.CardMifareClassic.key[4].ToString("X2");
                textBoxMfClassicKeyB5.Text = MyReader.CardMifareClassic.key[5].ToString("X2");
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }
        }

        //get keytype
        private Classic.KeyTypeBlockAccess MfClassicGetKeyType()
        {
            try
            {
                if (comboBoxMfClassicKeyType.SelectedIndex == 0)
                    return Classic.KeyTypeBlockAccess.TypeA;
                else
                    return Classic.KeyTypeBlockAccess.TypeB;
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
                return Classic.KeyTypeBlockAccess.TypeA;
            }
        }

        //set keytype
        private void MfClassicSetKeyType()
        {
            try
            {
                if (MyReader.CardMifareClassic.keyType == Classic.KeyTypeBlockAccess.TypeA)
                    comboBoxMfClassicKeyType.SelectedIndex = 0;
                else
                    comboBoxMfClassicKeyType.SelectedIndex = 1;

            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);
            }
        }

        //update value data
        private void textBoxMfClassicDataB0_TextChanged(object sender, EventArgs e)
        {
            Byte[] valueTab;
            String name = String.Empty;

            try
            {

                valueTab = MfClassicGetValue();

                foreach (Byte str in valueTab)
                    name += Char.ConvertFromUtf32(str);

            }
            catch (Exception exe)
            {

                name = String.Empty;
                InfoBuffer.error(exe.Message);
            }

            textBoxMfClassicDataText.Text = name;

        }

        //get default key, calculate sector and block
        private void buttonMfClassicGetDefault_Click(object sender, EventArgs e)
        {

            MyReader.CardMifareClassic.sector = MfClassicGetSector();       //Set sector
            MyReader.CardMifareClassic.block = MfClassicGetBlock();         //Set block
            MyReader.CardMifareClassic.key = MyReader.CardMifareClassic.defaultKey;     //Set default key for sector

            MfClassicSetKey();                                              //Get key 


        }

        //Mifare Read data
        private void buttonMfClassicRead_Click(object sender, EventArgs e)
        {
            //Set data in mifare card
            MyReader.CardMifareClassic.sector = MfClassicGetSector();
            MyReader.CardMifareClassic.block = MfClassicGetBlock();

            MyReader.CardMifareClassic.key = MfClassicGetKey();
            MyReader.CardMifareClassic.keyType = MfClassicGetKeyType();

            //Read data from card
            Byte[] MyTab = MyReader.ReadCard();

            //Update data value
            MfClassicSetData(MyTab);

        }

        //Mifare Write data
        private void buttonMfClassicWrite_Click(object sender, EventArgs e)
        {
            //Set data in mifare card
            MyReader.CardMifareClassic.sector = MfClassicGetSector();
            MyReader.CardMifareClassic.block = MfClassicGetBlock();

            MyReader.CardMifareClassic.key = MfClassicGetKey();
            MyReader.CardMifareClassic.keyType = MfClassicGetKeyType();

            //check Mad sector
            if ((MyReader.CardMifareClassic.sector == 0x00) || (MyReader.CardMifareClassic.sector == 0x10))
            {
                // Determine if the OK button was clicked on the dialog box.
                if (MessageBox.Show("Modification MAD sector - MIFARE Application Directory.\nContinue?", "Write mifare classic", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
            }

            //check sector trailer 
            if ((MyReader.CardMifareClassic.block == 0x03) || (MyReader.CardMifareClassic.block == 0x0F))
            {
                // Determine if the OK button was clicked on the dialog box.
                if (MessageBox.Show("Modification sector trailer.\nContinue?", "Write mifare classic", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
            }

            //Read data from card
            MyReader.WriteCard(MfClassicGetValue());

        }

        //Mifare Read card Buffer
        private void buttonMfClassicCardBuffer_Click(object sender, EventArgs e)
        {
            string myCardBuff;

            myCardBuff = MyReader.ReadCardCode();
            //myCardBuff = MyReader.ReadCardBuffer();

            try
            {
                textBoxMfClassicCardCode.Text = myCardBuff;//new String(myCardBuff);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);

            }
        }

        //Mifare Read card Serial Number
        private void buttonMfClassicCardID_Click(object sender, EventArgs e)
        {
            Byte[] myCardID;

            myCardID = MyReader.ReadCardId();

            try
            {
                String cardIdStr = String.Empty;

                foreach (Byte element in myCardID)
                    cardIdStr += element.ToString("X2");

                textBoxMfClassicCardID.Text = cardIdStr;
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);

            }

        }

        #region RUD3_v2

        private void buttonRUD3_v2_PastText_Click(object sender, EventArgs e)
        {
            Form2 pastText = new Form2();

            pastText.ShowDialog();

            // Determine if the OK button was clicked on the dialog box.
            if (pastText.DialogResult == DialogResult.OK)
            {
                string data = pastText.Data;
                pastText.Dispose();
                try
                {
                    //Convert to hex value
                    if (data.Length <= 32)
                    {
                        byte[] byteTab = new byte[data.Length / 2];

                        for (int i = 0; i < data.Length; i += 2)
                        {
                            byteTab[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);
                        }

                        //Update hex value
                        RUD3_v2_HexValue = byteTab;
                    }
                }
                catch (Exception exe)
                {
                    InfoBuffer.error(exe.Message);
                }
 
            }
            else
                pastText.Dispose();
        }

        private void textBoxRUD3_v2_DataB0_TextChanged(object sender, EventArgs e)
        {
            byte[] valueHex;
            string name = string.Empty;

            try
            {
                //Get value hex
                valueHex = RUD3_v2_HexValue;
                string valueHexName = string.Empty;
                //Convert to string
                valueHexName = BitConverter.ToString(valueHex);
                name = valueHexName.Replace("-", "");

            }
            catch (Exception exe)
            {

                name = string.Empty;
                InfoBuffer.error(exe.Message);
            }
            //Update text
            textBoxRUD3_v2_DataText.Text = name;
        }

        private Byte[] RUD3_v2_HexValue
        {
            get
            {
                Byte[] byteTab = new Byte[16];

                try
                {
                    //Convert to byte
                    if (!textBoxRUD3_v2_DataB0.Text.Equals(String.Empty))
                        byteTab[0] = Convert.ToByte(textBoxRUD3_v2_DataB0.Text, 16);
                    else
                        byteTab[0] = 0x00;

                    if (!textBoxRUD3_v2_DataB1.Text.Equals(String.Empty))
                        byteTab[1] = Convert.ToByte(textBoxRUD3_v2_DataB1.Text, 16);
                    else
                        byteTab[1] = 0x00;

                    if (!textBoxRUD3_v2_DataB2.Text.Equals(String.Empty))
                        byteTab[2] = Convert.ToByte(textBoxRUD3_v2_DataB2.Text, 16);
                    else
                        byteTab[2] = 0x00;

                    if (!textBoxRUD3_v2_DataB3.Text.Equals(String.Empty))
                        byteTab[3] = Convert.ToByte(textBoxRUD3_v2_DataB3.Text, 16);
                    else
                        byteTab[3] = 0x00;

                    if (!textBoxRUD3_v2_DataB4.Text.Equals(String.Empty))
                        byteTab[4] = Convert.ToByte(textBoxRUD3_v2_DataB4.Text, 16);
                    else
                        byteTab[4] = 0x00;

                    if (!textBoxRUD3_v2_DataB5.Text.Equals(String.Empty))
                        byteTab[5] = Convert.ToByte(textBoxRUD3_v2_DataB5.Text, 16);
                    else
                        byteTab[5] = 0x00;

                    if (!textBoxRUD3_v2_DataB6.Text.Equals(String.Empty))
                        byteTab[6] = Convert.ToByte(textBoxRUD3_v2_DataB6.Text, 16);
                    else
                        byteTab[6] = 0x00;

                    if (!textBoxRUD3_v2_DataB7.Text.Equals(String.Empty))
                        byteTab[7] = Convert.ToByte(textBoxRUD3_v2_DataB7.Text, 16);
                    else
                        byteTab[7] = 0x00;

                    if (!textBoxRUD3_v2_DataB8.Text.Equals(String.Empty))
                        byteTab[8] = Convert.ToByte(textBoxRUD3_v2_DataB8.Text, 16);
                    else
                        byteTab[8] = 0x00;

                    if (!textBoxRUD3_v2_DataB9.Text.Equals(String.Empty))
                        byteTab[9] = Convert.ToByte(textBoxRUD3_v2_DataB9.Text, 16);
                    else
                        byteTab[9] = 0x00;

                    if (!textBoxRUD3_v2_DataB10.Text.Equals(String.Empty))
                        byteTab[10] = Convert.ToByte(textBoxRUD3_v2_DataB10.Text, 16);
                    else
                        byteTab[10] = 0x00;

                    if (!textBoxRUD3_v2_DataB11.Text.Equals(String.Empty))
                        byteTab[11] = Convert.ToByte(textBoxRUD3_v2_DataB11.Text, 16);
                    else
                        byteTab[11] = 0x00;

                    if (!textBoxRUD3_v2_DataB12.Text.Equals(String.Empty))
                        byteTab[12] = Convert.ToByte(textBoxRUD3_v2_DataB12.Text, 16);
                    else
                        byteTab[12] = 0x00;

                    if (!textBoxRUD3_v2_DataB13.Text.Equals(String.Empty))
                        byteTab[13] = Convert.ToByte(textBoxRUD3_v2_DataB13.Text, 16);
                    else
                        byteTab[13] = 0x00;

                    if (!textBoxRUD3_v2_DataB14.Text.Equals(String.Empty))
                        byteTab[14] = Convert.ToByte(textBoxRUD3_v2_DataB14.Text, 16);
                    else
                        byteTab[14] = 0x00;

                    if (!textBoxRUD3_v2_DataB15.Text.Equals(String.Empty))
                        byteTab[15] = Convert.ToByte(textBoxRUD3_v2_DataB15.Text, 16);
                    else
                        byteTab[15] = 0x00;
                }
                catch (Exception exe)
                {
                    InfoBuffer.error(exe.Message);
                    Array.Clear(byteTab, 0, byteTab.Length);
                }
                finally
                {
                    
                }

                return byteTab;
            }
            set
            {
                String[] strTab = new String[16];

                try
                {
                    //Convert to hex string
                    for (int i = 0; i < value.Length; i++)
                    {
                        strTab[i] = value[i].ToString("X2");
                    }

                    //Past 
                    textBoxRUD3_v2_DataB0.Text = strTab[0];
                    textBoxRUD3_v2_DataB1.Text = strTab[1];
                    textBoxRUD3_v2_DataB2.Text = strTab[2];
                    textBoxRUD3_v2_DataB3.Text = strTab[3];
                    textBoxRUD3_v2_DataB4.Text = strTab[4];
                    textBoxRUD3_v2_DataB5.Text = strTab[5];
                    textBoxRUD3_v2_DataB6.Text = strTab[6];
                    textBoxRUD3_v2_DataB7.Text = strTab[7];
                    textBoxRUD3_v2_DataB8.Text = strTab[8];
                    textBoxRUD3_v2_DataB9.Text = strTab[9];
                    textBoxRUD3_v2_DataB10.Text = strTab[10];
                    textBoxRUD3_v2_DataB11.Text = strTab[11];
                    textBoxRUD3_v2_DataB12.Text = strTab[12];
                    textBoxRUD3_v2_DataB13.Text = strTab[13];
                    textBoxRUD3_v2_DataB14.Text = strTab[14];
                    textBoxRUD3_v2_DataB15.Text = strTab[15];
                    
                }
                catch (Exception exe)
                {
                    InfoBuffer.error(exe.Message);
                }
            }
        }

        #endregion

        private void buttonRUD3_v2_Read_Click(object sender, EventArgs e)
        {
            try
            {
                string cardBufFullStr = MyReader.ReadCardCode();

                //trim end width chars
                string cardBufTmpStr = cardBufFullStr.TrimEnd('\0');
                string cardBufStr;

                //Check start char 'R' or 'L'
                if ((cardBufTmpStr.StartsWith("R") == true) || (cardBufTmpStr.StartsWith("L") == true))
                {
                    cardBufStr = cardBufTmpStr.TrimStart('R', 'L');
                }
                else
                {
                    cardBufStr = cardBufTmpStr;
                }

                //Check lenght
                if((cardBufStr != null) && (cardBufStr.Length>0))
                {
                    //Convert ASCII HEX STRING to byte array
                    byte[] byteTab = new byte[cardBufStr.Length/2];

                    for(int i = 0; i< cardBufStr.Length; i+=2)
                    {
                        byteTab[i/2] = Convert.ToByte(cardBufStr.Substring(i, 2), 16);
                    }
                   
                    if (byteTab.Length <= 16)
                    {
                        //Update hex value
                        RUD3_v2_HexValue = byteTab;
                    }
                }
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);

            }
        }

        private void buttonRUD3_v2_Write_Click(object sender, EventArgs e)
        {
            try
            {
                char[] cardCodeAsciHex = new char[RUD3_v2_HexValue.Length * 2];
              
                int b;
                for (int i = 0; i < RUD3_v2_HexValue.Length; i++)
                {
                    b = RUD3_v2_HexValue[i] >> 4;
                    cardCodeAsciHex[i * 2] = (char)(b>9?(b-10)+'A':b+'0');
                    b = RUD3_v2_HexValue[i] & 0xF;
                    cardCodeAsciHex[i * 2 + 1] = (char)(b >9 ? (b - 10) + 'A' : b + '0');
                }
                MyReader.WriteCardCode(cardCodeAsciHex);
            }
            catch (Exception exe)
            {
                InfoBuffer.error(exe.Message);

            }
        }
    }

}
