Esempio n. 1
0
        /// <summary>
        /// 在指定块区连续写数据
        /// </summary>
        /// <param name="sector">扇区</param>
        /// <param name="block">块号</param>
        /// <param name="strData">写入的内容</param>
        /// <returns></returns>
        public bool WriteSectorData(int sector, int block, string strData)
        {
            int ret = -1;

            byte[] resultBuff = null;
            //_mutex.WaitOne();
            byte[] buff = Encoding.Default.GetBytes(strData);
            if (buff.Length > 16)
            {
                return(false);
            }
            for (int i = 0; i < _buff16.Length; i++)
            {
                if (i < buff.Length)
                {
                    _buff16[i] = buff[i];
                }
                else
                {
                    _buff16[i] = 0x00;
                }
            }

            switch (_readerType)
            {
            case ReaderType.COM:
                ret = MwRfSDK.rf_write(_icdev, sector * 4 + block, _buff16);
                break;

            case ReaderType.NET:
                //ret = MwRfSDKNet.rf_write(_icdev, sector * 4 + block, _buff16);
                resultBuff = Write(4 * sector + block, _buff16);
                datalen    = resultBuff.Length;
                if (datalen > 0)
                {
                    if ((resultBuff[0] == 0xbb) & (resultBuff[1] == 0xff))
                    {
                        switch (resultBuff[2])
                        {
                        case 0xAF:
                            ret = 0;
                            break;

                        default:
                            ret = 1;
                            break;
                        }
                    }
                }
                break;
            }
            //_mutex.ReleaseMutex();
            if (ret != 0)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 连续写整个扇区
        /// </summary>
        /// <param name="sector">扇区</param>
        /// <param name="strData">写入的内容</param>
        /// <returns></returns>
        public bool WriteData(int sector, string data)
        {
            int datalen;

            byte[] resultBuff = null;
            byte[] bytesData = Encoding.Default.GetBytes(data);
            byte[] buff = new byte[16];
            int    i = 0, ret = -1;
            bool   completed = false;

            //InitCard();
            if (!Auth(sector))
            {
                return(false);
            }
            switch (_readerType)
            {
            case ReaderType.COM:
                for (int block = 0; block < 3; block++)
                {
                    for (int j = 0; j < buff.Length; j++)
                    {
                        i = block * 16 + j;
                        if (i < bytesData.Length)
                        {
                            buff[j] = bytesData[i];
                        }
                        else
                        {
                            buff[j]   = 0x00;
                            completed = true;
                        }
                    }
                    ret = MwRfSDK.rf_write(_icdev, 4 * sector + block, buff);
                    if (ret != 0)
                    {
                        return(false);
                    }
                    if (completed)
                    {
                        break;
                    }
                }
                break;

            case ReaderType.NET:
                for (int block = 0; block < 3; block++)
                {
                    for (int j = 0; j < buff.Length; j++)
                    {
                        i = block * 16 + j;
                        if (i < bytesData.Length)
                        {
                            buff[j] = bytesData[i];
                        }
                        else
                        {
                            buff[j]   = 0x00;
                            completed = true;
                        }
                    }

                    //ret = MwRfSDKNet.rf_write(_icdev, 4 * sector + block, buff);
                    resultBuff = Write(4 * sector + block, buff);
                    datalen    = resultBuff.Length;
                    if (datalen > 0)
                    {
                        if ((resultBuff[0] == 0xbb) & (resultBuff[1] == 0xff))
                        {
                            switch (resultBuff[2])
                            {
                            case 0xAF:
                                ret = 0;
                                break;

                            default:
                                ret = 1;
                                break;
                            }
                        }
                    }
                    if (ret != 0)
                    {
                        return(false);
                    }
                    if (completed)
                    {
                        break;
                    }
                }
                break;
            }

            return(true);
        }