C#操作-LX3600非接触式IC卡读写器

C操作-LX3600非接触式IC卡读写器 - chy2z - 黑暗行动

 

首先的引用RC500_232.dll和zlg500B.dll这两个dll功能是一样,只是zlg500B.dll的功能有多一些!

如果是应用程序,将这两个dll放到bin文件夹中,如果是asp.net 程序 则将dll 放到系统盘中的system32文件夹中

(using System.Runtime.InteropServices; 一定要添加这个命名空间!!!!!!)

添加API申明如下:

        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_init(int mport, int mbaud);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_config();
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_exit();
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_request(byte mmode, ref UInt16 mtagtype);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_anticoll(byte mbcnt, ref UInt32 msnr);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_select(UInt32 msnr, ref byte msize);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_authkey(byte mmode, byte msecnr, byte[] mkey);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_read(byte maddr, byte[] mdata);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_write(byte maddr, byte[] mdata);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_readval(byte maddr, ref Int32 value);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_writeval(byte maddr, Int32 value);
        [DllImport("zlg500B.dll")]
        private static extern byte zlg500B_increment(byte maddr, ref Int32 value);
        [DllImport("zlg500B.dll")]
        private static extern byte zlg500B_decrement(byte maddr, ref Int32 value);

具体功能看代码:(这些函数都有说明的,自己看下吧!)

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="mport">端口</param>
        /// <param name="mbaud">平率</param>
        /// <returns></returns>
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_init(int mport, int mbaud);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_config();
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_exit();
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_request(byte mmode, ref UInt16 mtagtype);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_anticoll(byte mbcnt, ref UInt32 msnr);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_select(UInt32 msnr, ref byte msize);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_authkey(byte mmode, byte msecnr, byte[] mkey);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_read(byte maddr, byte[] mdata);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_write(byte maddr, byte[] mdata);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_readval(byte maddr, ref Int32 value);
        [DllImport("RC500_232.dll")]
        private static extern byte RC500_232_writeval(byte maddr, Int32 value);
        [DllImport("zlg500B.dll")]
        private static extern byte zlg500B_increment(byte maddr, ref Int32 value);
        [DllImport("zlg500B.dll")]
        private static extern byte zlg500B_decrement(byte maddr, ref Int32 value);

        ushort tagtype;
        UInt32 snr;
        byte size;
        byte[] key = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
        byte[] data = new byte[16];

        byte byteDataArea;  //选择的数据块
     
        byte byteSQ;  //选择扇区

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }

        private void but_connect_m_Click(object sender, EventArgs e)
        {

            if (RC500_232_init(Convert.ToInt32(box_com.Text.Replace("com","")), Convert.ToInt32(box_hz.Text)) != 0)
                 MessageBox.Show("端口打开失败!");
             else if (RC500_232_config() != 0)
                 MessageBox.Show("初始化失败!");
             else txt_log.Text="端口打开成功,读卡器初始化成功!\r\n";
        }

        private void but_close_m_Click(object sender, EventArgs e)
        {
              long i=1;
              int j = 0;
              while(i!=0){
                  i = RC500_232_exit();
                  j++;
                  if (j > 10)
                         break;
              }
             Log("关闭串口成功");
        }

        public void Log(string mess) {
            txt_log.Text += mess+"\r\n";
        }

        private void but_exti_m_Click(object sender, EventArgs e)
        {
            but_close_m_Click(null,null);
            Application.Exit();
        }


        Regex rgKey = new Regex(" ");
        private void but_readData_Click(object sender, EventArgs e)
        {

            if (Init())
            {

                if (RC500_232_read(byteDataArea, data) != 0)// 将0扇区数据读入byte类型数组data中,//data为16字节长   
                {
                    MessageBox.Show("数据读取失败");
                }
                else
                {
                    txt_code.Text = Convert.ToString(snr, 16);
                    Log("数据读取成功: " + ReadData(data));
                }
            }
           
        }


        public string ReadData(byte[] data)
        {

            string tempt = "";

            foreach (byte r in data)
            {
                tempt += Convert.ToString(r,16)+ " ";
            }
            return tempt;
        }

       
        private void but_readValue_Click(object sender, EventArgs e)
        {

            if (Init())
            {
                Int32 intValue = 0x500;

                if (RC500_232_readval(byteDataArea, ref intValue) != 0)// 将0扇区数据读入byte类型数组data中,//data为16字节长   
                {
                    MessageBox.Show("数值读取失败");
                }
                else
                {
                    txt_code.Text = Convert.ToString(snr, 16);
                    Log("数值读取成功: " +Convert.ToString(intValue,16));
                }
            }
        }


        private void box_SQ_SelectedIndexChanged(object sender, EventArgs e)
        {
            int intSQ = Convert.ToInt32(box_SQ.Text);
            int temp = intSQ * 4;
            box_dataArea.Items.Clear();
            box_dataArea.Items.Add(temp);
            box_dataArea.Items.Add(temp+1);
            box_dataArea.Items.Add(temp+2);
            box_dataArea.Items.Add(temp+3);
            box_dataArea.SelectedIndex = 0;

            box_dataArea2.Items.Clear();
            if (intSQ != 0)
            {
                box_dataArea2.Items.Add(temp);
            }
            box_dataArea2.Items.Add(temp + 1);
            box_dataArea2.Items.Add(temp + 2);
            box_dataArea2.Items.Add(temp + 3);
            box_dataArea2.SelectedIndex = 0;
        }

        private void but_writeDate_Click(object sender, EventArgs e)
        {
            string value = txt_writeDate.Text.Trim();

            if (!string.IsNullOrEmpty(value))
            {
                int index = 0;

                foreach (string pt in rgKey.Split(value))
                {
                    if (string.IsNullOrEmpty(pt))
                    {
                        MessageBox.Show("写入数据时有多余的空格!"); return;
                    }

                    int intValue = 0;

                    try
                    {
                       intValue = Convert.ToInt32(pt, 16);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("写入数据含有非法字符!");
                        return;
                    }
                   
                    if (intValue > 255 || intValue < 0)
                    {
                        MessageBox.Show("写入数据范围错误(00-ff)!");
                        return;
                    }

                    byte bt = (byte)intValue;

                    data[index] = bt;
                    index++;
                }

                if (index != 16)
                {
                    MessageBox.Show("写入数据错误,长度不为16个!"); return;
                }


                if (Init())
                {
                    if (RC500_232_write(byteDataArea, data) != 0)// 将0扇区数据读入byte类型数组data中,//data为16字节长   
                    {
                        MessageBox.Show("数据写入失败");
                    }
                    else
                    {
                        txt_code.Text = Convert.ToString(snr, 16);
                        Log("数据写入成功: " + ReadData(data));
                    }
                }
            }
            else {
                MessageBox.Show("写入数据不能为空!");
            }
        }
       

        public bool Init()
        {

            byteDataArea = (byte)Convert.ToInt32(box_dataArea.Text); //选择的数据块

             byteSQ = (byte)Convert.ToInt32(box_SQ.Text); //选择扇区
       
            string strKey = txt_key.Text;

            if (!string.IsNullOrEmpty(strKey))
            {
                int index = 0;

                foreach (string pt in rgKey.Split(strKey))
                {

                    byte bt = (byte)Convert.ToInt32(pt, 16);
                    key[index] = bt;
                    index++;

                }
            }
            else
            {
                key = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
            }

            bool flag = false;
            if (RC500_232_request(0, ref tagtype) != 0) //检查有效范围是否有卡   
            {

                if (RC500_232_request(0, ref tagtype) != 0) //检查有效范围是否有卡   
                {
                    MessageBox.Show("没有卡!"); return false;
                }
                else
                    flag = true;
            }
            else
                flag = true;


            if (flag)
            {

                if (RC500_232_anticoll(0, ref snr) != 0) //防碰撞控制,snr返回卡的序列号                                    
                {
                    MessageBox.Show("防碰撞错误!"); return false;
                }
                else if (RC500_232_select(snr, ref size) != 0) //选择某一序号的卡,//size返回卡的容量大小            
                {
                    MessageBox.Show("选择错误!"); return false;
                }
                else if (RC500_232_authkey((byte)box_keyMode.SelectedIndex, byteSQ, key) != 0) //密码验证,密码存放在 key数组中    
                {
                    MessageBox.Show("密码验证错误!"); return false;
                }

               
            }

            return true;
        }

        private void but_writeValue_Click(object sender, EventArgs e)
        {
            if (Init())
            {
                string value = txt_writeValue.Text.Trim();
                if(string.IsNullOrEmpty(value)){
                    MessageBox.Show("写入数值不能为空");
                    return;
                }

                Int32 intValue = Convert.ToInt32(value,16);

                if (RC500_232_writeval(byteDataArea, intValue) != 0)// 将0扇区数据读入byte类型数组data中,//data为16字节长   
                {
                    MessageBox.Show("数值写入失败");
                }
                else
                {
                    txt_code.Text = Convert.ToString(snr, 16);
                    Log("数值写入成功: " + value);
                }
            }
        }

        private void but_add_Click(object sender, EventArgs e)
        {
           
            if (Init())
            {
              
                string value = txt_addValue.Text.Trim();
                if(string.IsNullOrEmpty(value)){
                    MessageBox.Show("写入数值不能为空");
                    return;
                }

                Int32 temp = 1; //Convert.ToInt32(txt_addValue.Text.Trim(),16);

                if (zlg500B_increment(byteDataArea, ref temp) != 0)// 将0扇区数据读入byte类型数组data中,//data为16字节长   
                {           
                    MessageBox.Show("数值写入失败");
                }
                else
                {
                    txt_code.Text = Convert.ToString(snr, 16);
                    Log("数值写入成功: " + value);
                }
            }
        }

        private void but_decrement_Click(object sender, EventArgs e)
        {
            if (Init())
            {

                string value = txt_dec.Text.Trim();
                if (string.IsNullOrEmpty(value))
                {
                    MessageBox.Show("写入数值不能为空");
                    return;
                }

                Int32 temp = 1; //Convert.ToInt32(txt_addValue.Text.Trim(), 16);


                if (zlg500B_decrement(byteDataArea, ref temp) != 0)// 将0扇区数据读入byte类型数组data中,//data为16字节长   
                {
                    MessageBox.Show("数值写入失败");
                }
                else
                {
                    txt_code.Text = Convert.ToString(snr, 16);
                    Log("数值写入成功: " + value);
                }
            }
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值