C# read and write T5557 chip card copy ID access control card source code

        T5557 card is a multi-functional non-contact radio frequency chip card produced by Atmel Corporation of the United States. It is a 125KHz low-frequency card and has a large application market in China. For example, many hotel access control cards use T5557 cards. The chip has a total of 330bit (bit) EPROM (distributed into 10 blocks, each block is 33bit). Block 0 of page 0 is a parameter configuration block reserved for setting the T5557 operation mode. Block 7 on page 0 can be used as a user data block, or as a password to protect all data (if the password function is enabled in the configuration block), to prevent illegal rewriting of data. Blocks 1 and 2 on page 1 store the manufacturer's information and unique factory ID, which can only be read and cannot be changed. T5567 and T5577 are upgraded versions of T5557.

        By modifying the parameter configuration block of the T5557 card, the t5557 card can be simulated as an ID card and HID card, so it is widely used in the copying industry of access control cards.

Introduction to the card issuer used in this example: T5557 T5567 T5577 Low Frequency RFID Reader EM4100 HID Card Duplicator Hotel Key Card - Taobao.com (taobao.com)

  1. Function declaration

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;  //调用动态库一定要加入这个引用


namespace idcardreader
{
    public partial class Form1 : Form
    {
        CheckBox[] myCheckBox = new CheckBox[14];
        TextBox[] myTextBox = new TextBox[14]; 

        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //外部函数声明:让设备发出声响
        [DllImport("OUR_IDR.dll", EntryPoint = "idr_beep", CallingConvention = CallingConvention.StdCall)]
        static extern byte idr_beep(UInt32 xms);//xms单位为毫秒 
	   //------------------------------------------------------------------------------------------------------------------------------------------------------    
        //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限
        [DllImport("OUR_IDR.dll", EntryPoint = "pcdgetdevicenumber", CallingConvention = CallingConvention.StdCall)]
        static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber用于返回编号 
        //------------------------------------------------------------------------------------------------------------------------------------------------------    
        //只读EM4100卡号
        [DllImport("OUR_IDR.dll", EntryPoint = "idr_read", CallingConvention = CallingConvention.StdCall)]
        public static extern byte idr_read(byte[] serial);//serial返回卡号        
	    //------------------------------------------------------------------------------------------------------------------------------------------------------    
        //只读卡号,只读一次EM4100卡,必须拿开卡才能再读到
        [DllImport("OUR_IDR.dll", EntryPoint = "idr_read_once", CallingConvention = CallingConvention.StdCall)]
        public static extern byte idr_read_once(byte[] serial);//serial返回卡号
        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //T5557卡写配置块
        [DllImport("OUR_IDR.dll", EntryPoint = "t5557_init", CallingConvention = CallingConvention.StdCall)]
        public static extern byte  t5557_init(byte ctrlword,byte[] seria,byte[] key,byte[] configdata,byte[] newkey);
        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //T5557卡读卡
        [DllImport("OUR_IDR.dll", EntryPoint = "t5557_read", CallingConvention = CallingConvention.StdCall)]
        public static extern byte  t5557_read(byte ctrlword,byte[] seria,byte[] key,byte[] blockflag,byte[] readdata);
        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //T5557卡写卡
        [DllImport("OUR_IDR.dll", EntryPoint = "t5557_write", CallingConvention = CallingConvention.StdCall)]
        public static extern byte  t5557_write(byte ctrlword,byte[] seria,byte[] key,byte[] blockflag,byte[] writedata);
        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //T5557卡改密码
        [DllImport("OUR_IDR.dll", EntryPoint = "t5557_changekey", CallingConvention = CallingConvention.StdCall)]
        public static extern byte  t5557_changekey(byte ctrlword,byte[] seria,byte[] oldkey,byte[] newkey);
        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //用T5557卡制作ID卡(也就是EM4100及兼容卡)
        [DllImport("OUR_IDR.dll", EntryPoint = "t5557_to4100", CallingConvention = CallingConvention.StdCall)]
        public static extern byte  t5557_to4100(byte ctrlword,byte[] seria,byte[] oldkey,byte[] newkey,byte[] myuidbuf);
        //-----------------------------------------------------------------------------------------------------------------------------------------------------        
        //只读HID卡号
        [DllImport("OUR_IDR.dll", EntryPoint = "hid_read", CallingConvention = CallingConvention.StdCall)]
        public static extern byte hid_read(byte[] serial);//serial返回卡号 
        //------------------------------------------------------------------------------------------------------------------------------------------------------
        //用T5557卡制作HID卡
        [DllImport("OUR_IDR.dll", EntryPoint = "t5557_tohid", CallingConvention = CallingConvention.StdCall)]
        public static extern byte t5557_tohid(byte ctrlword, byte[] seria, byte[] oldkey, byte[] newkey, byte[] myuidbuf);


        byte NEEDSERIAL = 0x01;   //需要只对指定系列号的卡操作
        byte NEEDKEY = 0x02;      //需要用密码认证
        byte LOCKBIT = 0x04;      //锁定配置块或数据块,仅对   t5557_init,t5557_write ,t5557_changekey函数有效
        byte KEYENABLE = 0x08;    //启用本卡的密码功能
        byte RESETCARD = 0x10;    //操作成功后重启卡片

2. Read the data in the T5557 card block 

        private void button1_Click(object sender, EventArgs e)
        {
            byte status;     //存放返回值
            int j;
            byte myctrlword = 0x00;  //控制字
            byte[] oldpicckey = new byte[4];  //密码
            byte[] mypiccserial = new byte[6];  //卡序列号
            byte[] mypiccdata = new byte[50];  //读卡数据缓冲:卡无线转输分频比、卡内容长度(字节数),及最多返回12个块的数据
            byte[] mypiccblockflag = new byte[2]; //指定读哪一块
            string PasswStr = textBox14.Text.Trim();
            string seriaStr = textBox15.Text.Trim();

            if (checkBox1.Checked)  //本次操作需要密码验证
            {
                myctrlword =(byte)(myctrlword+ NEEDKEY);
                try
                {
                    oldpicckey[0] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(0, 2), 16));
                    oldpicckey[1] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(2, 2), 16));
                    oldpicckey[2] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(4, 2), 16));
                    oldpicckey[3] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(6, 2), 16));
                }
                catch {
                    MessageBox.Show("认证密码输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox14.Select();
                    return;
                }
            }

            if (checkBox2.Checked)  //仅操作指定卡号的卡
            {
                try
                {
                    myctrlword = (byte)(myctrlword + NEEDSERIAL);
                    mypiccserial[0] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(0, 2), 16));
                    mypiccserial[1] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(2, 2), 16));
                    mypiccserial[2] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(4, 2), 16));
                    mypiccserial[3] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(6, 2), 16));
                    mypiccserial[4] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(8, 2), 16));
                    mypiccserial[5] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(10, 2), 16));
                }
                catch
                {
                    MessageBox.Show("卡号输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox15.Select();
                    return;
                }
            }

            j = 0;
            mypiccblockflag[0] = 0x00;
            for (int i = 0; i < 8; i++)
            {
                if (myCheckBox[i].Checked)
                {
                    mypiccblockflag[0] = (byte)(mypiccblockflag[0] + Math.Pow(2, i));
                    j++;
                }
            }

            mypiccblockflag[1] = 0x00;
            for (int i = 0; i < 4; i++)
            {
                if (myCheckBox[8+i].Checked)
                {
                    mypiccblockflag[1] = (byte)(mypiccblockflag[1] + Math.Pow(2, i+1));
                    j++;
                }
            }

            status = t5557_read(myctrlword,mypiccserial,oldpicckey,mypiccblockflag,mypiccdata);
            if (status == 0)
            {
                string blockdata = "";
                for (int i = 0; i < mypiccdata[1]; i++)
                {
                    blockdata = blockdata + mypiccdata[2+i].ToString("X2");
                }

                j = 0;
                for (int i = 0; i < 12; i++)
                {
                    if (myCheckBox[i].Checked)
                    {
                        myTextBox[i].Text = blockdata.Substring(j, 8);
                        j = j + 8;
                    }
                }

                seriaStr = "";
                for(int i=0 ;i<6;i++){
                    seriaStr = seriaStr + mypiccserial[i].ToString("X2"); 
                }
                idr_beep(30);
                MessageBox.Show("读卡成功,卡无线转输分频比:" + mypiccdata[0].ToString("D") + ",卡号:" + seriaStr, "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);


            }
            else { MessageErrInf(status); }

        }

3. Rewrite the data in the T5557 card block

private void button3_Click(object sender, EventArgs e)
        {
            byte status;     //存放返回值
            int j;
            byte myctrlword = 0x00;               //控制字
            byte[] oldpicckey = new byte[4];      //密码
            byte[] mypiccserial = new byte[6];    //卡序列号
            byte[] mypiccdata = new byte[50];     //写卡数据缓冲
            byte[] mypiccblockflag = new byte[2]; //指定读哪一块
            string PasswStr = textBox14.Text.Trim();
            string seriaStr = textBox15.Text.Trim();

            int chtxt=Form1_chedkdata();
            if (chtxt != 100)
            {
                MessageBox.Show("写卡数据输入错误,请重新输入!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                myTextBox[chtxt].Select(); 
                return;
            }

            if (checkBox1.Checked)  //本次操作需要密码验证
            {
                myctrlword = (byte)(myctrlword + NEEDKEY);
                try
                {
                    oldpicckey[0] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(0, 2), 16));
                    oldpicckey[1] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(2, 2), 16));
                    oldpicckey[2] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(4, 2), 16));
                    oldpicckey[3] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(6, 2), 16));
                }
                catch
                {
                    MessageBox.Show("认证密码输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox14.Select();
                    return;
                }
            }

            if (checkBox2.Checked)  //仅操作指定卡号的卡
            {
                try
                {
                    myctrlword = (byte)(myctrlword + NEEDSERIAL);
                    mypiccserial[0] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(0, 2), 16));
                    mypiccserial[1] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(2, 2), 16));
                    mypiccserial[2] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(4, 2), 16));
                    mypiccserial[3] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(6, 2), 16));
                    mypiccserial[4] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(8, 2), 16));
                    mypiccserial[5] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(10, 2), 16));
                }
                catch
                {
                    MessageBox.Show("卡号输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox15.Select();
                    return;
                }
            }

            j = 0;
            mypiccblockflag[0] = 0x00;
            for (int i = 0; i < 8; i++)
            {
                if (myCheckBox[i].Checked)
                {
                    mypiccblockflag[0] = (byte)(mypiccblockflag[0] + Math.Pow(2, i));
                    j++;
                }
            }

            mypiccblockflag[1] = 0x00;
            for (int i = 0; i < 4; i++)
            {
                if (myCheckBox[8 + i].Checked)
                {
                    mypiccblockflag[1] = (byte)(mypiccblockflag[1] + Math.Pow(2, i+1));
                    j++;
                }
            }

            string WriteStr = "";
            for (int i = 0; i < 12; i++)
            {               
                if (myCheckBox[i].Checked)
                {
                    WriteStr = WriteStr+myTextBox[i].Text;                    
                }
            }
            for (int i = 0; i < WriteStr.Length/2;i++ )
            {
                mypiccdata[i] = Convert.ToByte(Convert.ToInt32(WriteStr.Substring(i * 2, 2), 16));
            }

            status = t5557_write(myctrlword, mypiccserial, oldpicckey, mypiccblockflag, mypiccdata);
            if (status == 0)
            {                
                seriaStr = "";
                for (int i = 0; i < 6; i++)
                {
                    seriaStr = seriaStr + mypiccserial[i].ToString("X2");
                }
                idr_beep(30);
                MessageBox.Show("卡号:" + seriaStr+"写卡成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else { MessageErrInf(status); }
        }

 4. Configure T5557 card as ID card and HID card

private void button8_Click(object sender, EventArgs e)
        {
            byte status;     //存放返回值
            byte myctrlword = 0x00;  //控制字
            byte[] oldpicckey = new byte[4];  //密码
            byte[] newpicckey = new byte[4];  //新密码
            byte[] mypiccserial = new byte[6];  //卡序列号
            
            byte[] mypiccblockflag = new byte[2]; //指定读哪一块
            string PasswStr = textBox14.Text.Trim();
            string seriaStr = textBox15.Text.Trim();
            string newpassw = textBox20.Text.Trim();
            string newuidstr = textBox17.Text.Trim();

            if (checkBox1.Checked)  //本次操作需要密码验证
            {
                myctrlword = (byte)(myctrlword + NEEDKEY);
                try
                {
                    oldpicckey[0] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(0, 2), 16));
                    oldpicckey[1] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(2, 2), 16));
                    oldpicckey[2] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(4, 2), 16));
                    oldpicckey[3] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(6, 2), 16));
                }
                catch
                {
                    MessageBox.Show("认证密码输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox14.Select();
                    return;
                }
            }

            if (checkBox2.Checked)  //仅操作指定卡号的卡
            {
                try
                {
                    myctrlword = (byte)(myctrlword + NEEDSERIAL);
                    mypiccserial[0] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(0, 2), 16));
                    mypiccserial[1] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(2, 2), 16));
                    mypiccserial[2] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(4, 2), 16));
                    mypiccserial[3] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(6, 2), 16));
                    mypiccserial[4] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(8, 2), 16));
                    mypiccserial[5] = Convert.ToByte(Convert.ToInt32(seriaStr.Substring(10, 2), 16));
                }
                catch
                {
                    MessageBox.Show("卡号输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox15.Select();
                    return;
                }
            }

            if (checkBox11.Checked)  //修改卡片密码
            {
                try
                {
                    myctrlword = (byte)(myctrlword + KEYENABLE);
                    newpicckey[0] = Convert.ToByte(Convert.ToInt32(newpassw.Substring(0, 2), 16));
                    newpicckey[1] = Convert.ToByte(Convert.ToInt32(newpassw.Substring(2, 2), 16));
                    newpicckey[2] = Convert.ToByte(Convert.ToInt32(newpassw.Substring(4, 2), 16));
                    newpicckey[3] = Convert.ToByte(Convert.ToInt32(newpassw.Substring(6, 2), 16));
                }
                catch
                {
                    MessageBox.Show("密码输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox20.Select();
                    return;
                }
            }

            myctrlword = (byte)(myctrlword + RESETCARD); //操作后重启卡片,否则在制卡后,需要拿开卡片重放才能成功读ID卡

            if (radioButton3.Checked)
            {
                byte[] myuid = new byte[5];        //写入的新卡号
                try
                {
                    myuid[0] = Convert.ToByte(Convert.ToInt32(textBox16.Text.Substring(0, 2), 16));
                }
                catch
                {
                    MessageBox.Show("产家标识输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox16.Select();
                    return;
                }

                try
                {
                    myuid[1] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(0, 2), 16));
                    myuid[2] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(2, 2), 16));
                    myuid[3] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(4, 2), 16));
                    myuid[4] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(6, 2), 16));
                }
                catch
                {
                    MessageBox.Show("新卡号输入错误,请输入8位16进制数的卡号!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox17.Select();
                    return;
                }

                status = t5557_to4100(myctrlword, mypiccserial, oldpicckey, newpicckey, myuid);
                if (status == 0)
                {
                    idr_beep(50);
                    MessageBox.Show("卡号:" + System.Convert.ToString(myuid[1] * 256 * 256 * 256 + myuid[2] * 256 * 256 + myuid[3] * 256 + myuid[4]) + " 写卡成功变成ID卡!不能再用t5557的指令读写此卡,可重新设置配置块恢复t5557卡功能。", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else { MessageErrInf(status); }
            }
            else
            {
                byte[] myuid = new byte[7];        //写入的新卡号
                try
                {
                    myuid[0] = Convert.ToByte(Convert.ToInt32(textBox16.Text.Substring(0, 2), 16));
                }
                catch
                {
                    MessageBox.Show("HID卡号位数错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox16.Select();
                    return;
                }

                try
                {
                    myuid[1] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(0, 2), 16));
                    myuid[2] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(2, 2), 16));
                    myuid[3] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(4, 2), 16));
                    myuid[4] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(6, 2), 16));
                    myuid[5] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(8, 2), 16));
                    myuid[6] = Convert.ToByte(Convert.ToInt32(newuidstr.Substring(10, 2), 16));
                }
                catch
                {
                    MessageBox.Show("新卡号输入错误,请输入12位16进制数的卡号!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox17.Select();
                    return;
                }

                status = t5557_tohid(myctrlword, mypiccserial, oldpicckey, newpicckey, myuid);
                if (status == 0)
                {
                    idr_beep(50);
                    MessageBox.Show("卡号:" + System.Convert.ToString(myuid[3] * 256 * 256 * 256 + myuid[4] * 256 * 256 + myuid[5] * 256 + myuid[6]) + " 写卡成功变成HID卡!不能再用t5557的指令读写此卡,可重新设置配置块恢复t5557卡功能。", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else { MessageErrInf(status); }
            }
        }

5. T5557 card initialization

private void button9_Click(object sender, EventArgs e)
        {             byte status; //store return value             byte[] oldpicckey = new byte[4]; //old password             byte[] newpicckey = new byte[4]; //enable new Password             byte[] mypiccserial = new byte[6]; //card serial number             byte[] mypiccdata = new byte[4]; //configuration value             string PasswStr = textBox14.Text.Trim();             string NewPass = textBox21.Text. Trim();             string ConfStr = textBox18. Text. Trim();







            byte myctrlword = 0x00; //control word

            if (checkBox1.Checked)  //本次操作需要密码验证
            {
                myctrlword = (byte)(myctrlword + NEEDKEY);
                try
                {
                    oldpicckey[0] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(0, 2), 16));
                    oldpicckey[1] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(2, 2), 16));
                    oldpicckey[2] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(4, 2), 16));
                    oldpicckey[3] = Convert.ToByte(Convert.ToInt32(PasswStr.Substring(6, 2), 16));
                }
                catch
                {
                    MessageBox.Show("Authentication password input error!", "Example prompt", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox14.Select();
                    return;
                }
            }

            if (checkBox13.Checked)  //卡片启用新密码
            {
                myctrlword = (byte)(myctrlword + KEYENABLE);
                try
                {
                    newpicckey[0] = Convert.ToByte(Convert.ToInt32(NewPass.Substring(0, 2), 16));
                    newpicckey[1] = Convert.ToByte(Convert.ToInt32(NewPass.Substring(2, 2), 16));
                    newpicckey[2] = Convert.ToByte(Convert.ToInt32(NewPass.Substring(4, 2), 16));
                    newpicckey[3] = Convert.ToByte(Convert.ToInt32(NewPass.Substring(6, 2), 16));
                }
                catch
                {
                    MessageBox.Show("Authentication password input error!", "Example prompt", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox21.Select();
                    return;
                }
            }

            try
            {
                mypiccdata[0] = Convert.ToByte(Convert.ToInt32(ConfStr.Substring(0, 2), 16));
                mypiccdata[1] = Convert.ToByte(Convert.ToInt32(ConfStr.Substring(2, 2), 16));
                mypiccdata[2] = Convert.ToByte(Convert.ToInt32(ConfStr.Substring(4, 2), 16));
                mypiccdata[3] = Convert.ToByte(Convert.ToInt32(ConfStr.Substring(6, 2), 16));
            }
            catch
            {
                MessageBox.Show("配置值输入错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox18.Select();
                return;
            }

            status = t5557_init(myctrlword,mypiccserial,oldpicckey,mypiccdata,newpicckey);
            if (status == 0)
            {
                string seriaStr = "";
                for (int i = 0; i < 6; i++)
                {
                    seriaStr = seriaStr + mypiccserial[i].ToString("X2");
                }
                idr_beep(30);
                MessageBox.Show("卡号:" + seriaStr + "配置成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else { MessageErrInf(status); }
        }

        private void GetConfigNUm()
        {
            byte byte_i;
            byte[] Configdata = new byte[4];

            if (radioButton1.Checked) //e5550 compatibility mode (X-Mode bit: bit15) is 0
            {                 if (comboBox1.SelectedIndex == 0) //Master key                 {                     Configdata[0] = 0x00;                 }                 else { Configdata[0 ] = 0x60; }





                if (comboBox2.SelectedIndex < 9) //调制方式
                {
                    byte_i = (byte)comboBox2.SelectedIndex;
                }
                else
                {
                    if (comboBox2.SelectedIndex == 9)
                    {
                        byte_i = 0x10;
                    }
                    else { byte_i = 0x18; }
                }

                Configdata[1] = (byte)((2 * 4) | ((byte_i / 16) & 0x01)); //baud rate
                Configdata[2] = (byte)((byte_i % 16) * 16);
                Configdata [2] = (byte)(Configdata[2] | ((comboBox3.SelectedIndex % 4) * 4)); //Phase keying modulation

                if (checkBox12.Checked) //AOR request response mode
                {                     Configdata[2] = (byte)(Configdata[2] | 0x02);                 }

                Configdata[3] = (byte)((comboBox4.SelectedIndex % 8) * 32); //The largest block

                if (checkBox13.Checked) //Enable the encryption function
                {                     if (Configdata[3] > (6 * 32)) //After the encryption function is enabled, the maximum block cannot be 7                     {                         Configdata[3] = 6 * 32;                     }                     Configdata[ 3] = (byte)(Configdata[3] | 0x10);                 }                 else { Configdata[2] = (byte)(Configdata[2] & 0xfd); } //Cancel AOR request response mode






                if (checkBox14.Checked) //The frame terminator is valid
                {                     Configdata[3] = (byte)(Configdata[3] | 0x08);                 }

                if (checkBox19.Checked) //The card reset delay is 67ms
                {                     Configdata[3] = (byte)(Configdata[3] | 0x01);                 }             }             else //Extended mode (X-Mode bit: bit15) is 1             {                 Configdata [1] = 0x02;





                if (comboBox1.SelectedIndex == 0) // master key
                {                     Configdata[0] = 0x60;                 }                 else { Configdata[0] = 0x90; }


                byte_i = (byte)((int)(btlnum.Value) % 64); //波特率BIT9-14
                Configdata[1] = (byte)(Configdata[1] | (byte_i * 4));

                if (comboBox2.SelectedIndex < 6) //调制方式
                {
                    byte_i = (byte)comboBox2.SelectedIndex;
                }
                else
                {
                    if (comboBox2.SelectedIndex == 7)
                    {
                        byte_i = 0x10;
                    }
                    else
                    {
                        if (comboBox2.SelectedIndex == 8)
                        {
                            byte_i = 0x18;
                        }
                        else { byte_i = 0x08; }
                    }
                }

                Configdata[1] = (byte)(Configdata[1] | ((byte_i/16) & 0x01)); //baud rate
                Configdata[2] = (byte)((byte_i % 16) * 16);
                Configdata[ 2] = (byte)(Configdata[2] | ((comboBox3.SelectedIndex % 4) * 4)); //Phase keying modulation

                if (checkBox12.Checked) //AOR request response mode
                {                     Configdata[2] = (byte)(Configdata[2] | 0x02);                 }

                if (checkBox23.Checked) //OTP
                {
                    Configdata[2] = (byte)(Configdata[2] | 0x01);
                }

                Configdata[3] = (byte)((comboBox4.SelectedIndex % 8) * 32); //The largest block

                if (checkBox13.Checked) //Enable the encryption function
                {                     if (Configdata[3] > (6 * 32)) //After the encryption function is enabled, the maximum block cannot be 7                     {                         Configdata[3] = 6 * 32;                     }                     Configdata[ 3] = (byte)(Configdata[3] | 0x10);                 }                 else { Configdata[2] = (byte)(Configdata[2] & 0xfd); } //Cancel AOR request response mode






                if (checkBox22.Checked) //The frame start character is valid
                {                     Configdata[3] = (byte)(Configdata[3] | 0x08);                 }

                if (checkBox24.Checked) //Quick write: 0 is 12TC, 1 is 27TC
                {                     Configdata[3] = (byte)(Configdata[3] | 0x04);                 }

                if (checkBox25.Checked) // data inversion output
                {                     Configdata[3] = (byte)(Configdata[3] | 0x02);                 }

                if (checkBox19.Checked) // Card reset delay 67ms
                {                     Configdata[3] = (byte)(Configdata[3] | 0x01);                 }             }


            textBox18.Text = Configdata[0].ToString("X2") + Configdata[1].ToString("X2") + Configdata[2].ToString("X2") + Configdata[3].ToString("X2"); 
        }

Guess you like

Origin blog.csdn.net/zhangjin7422/article/details/128826303