winfrom读取IC卡的数据(串口通信)

版权声明:本文为博主原创文章,未经博主允许也能转载。 https://blog.csdn.net/qq_34090937/article/details/81357335
    /// <summary>
    /// 串口设备读写封装类
    /// </summary>
    public class ComDevice
    {
        public delegate void EventHandle(ArrayList dataList);
        public event EventHandle DataReceived;

        public SerialPort serialPort;
        Thread thread;
        volatile bool _keepReading;

        public ComDevice()
        {
            serialPort = new SerialPort();
            thread = null;
            _keepReading = false;
        }

        public bool IsOpen
        {
            get
            {
                return serialPort.IsOpen;
            }
        }

        private void StartReading()
        {
            if (!_keepReading)
            {
                _keepReading = true;
                thread = new Thread(new ThreadStart(ReadPort));
                thread.Start();
            }
        }

        private void StopReading()
        {
            if (_keepReading)
            {
                _keepReading = false;
                thread.Join();
                thread = null;
            }
        }

        private void ReadPort()
        {
            while (_keepReading)
            {
                try
                {
                    if (serialPort.IsOpen)
                    {
                        ArrayList data = new ArrayList();
                    READAGAIN://goto标记
                        while (serialPort.BytesToRead > 0)
                        {
                            data.Add((byte)serialPort.ReadByte());
                        }
                        Thread.Sleep(50);

                        if (serialPort.BytesToRead > 0) goto READAGAIN;//goto标记,数据未接收完继续等待接收完成

                        if (data.Count > 0)
                            DataReceived(data);
                    }
                }
                catch (Exception)
                {
                    //throw new ArgumentException("【ComDevice.ReadPort】" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                Close();
                serialPort.Open();
                if (serialPort.IsOpen)
                {
                    StartReading();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                //new AggregateException(ex);
            }

            return false;
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void Close()
        {
            StopReading();
            serialPort.Close();
        }

        /// <summary>
        /// 写数据到卡
        /// </summary>
        /// <param name="send"></param>
        /// <param name="offSet"></param>
        /// <param name="count"></param>
        public void WritePort(byte[] send, int offSet, int count)
        {
            if (IsOpen)
            {
                serialPort.Write(send, offSet, count);
            }
        }
    }

这是读取数据的帮助类。类的开头声明了一个委托事件,这个委托就是当IC卡头读取到数据触发返回数据的。

有了帮助类,我们在使用的时候给委托添加事件就行了

public ComDevice comm = new ComDevice();
comm.DataReceived += new ComDevice.EventHandle(GetCardNo);

        /// <summary>
        /// 读卡
        /// </summary>
        /// <param name="dataList"></param>
        public void GetCardNo(ArrayList dataList)
        {
            if (dataList.Count > 0 && dataList.Count == 9)
            {
                dataList.RemoveRange(0, 3);//去除前面3位
                dataList.RemoveRange(4, 2);//去除最后面2为

                //获取真实卡号
                string str = "";
                for (int i = 0; i < dataList.Count; i++)
                {
                    byte bData = (byte)dataList[i];
                    string tmpData = bData.ToString("X");
                    str += (tmpData.Length == 2 ? tmpData : "0" + tmpData);
                }

                //如果卡号不为空,那就输出看看
                if (!string.IsNullOrEmpty(str))
                {
                    try
                    {
                        //获取到卡号后,就可以进行业务处理
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

如果有不足的地方,欢迎批评指正。

展开阅读全文

没有更多推荐了,返回首页