/// <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); }
/// <summary> /// 打开设备 /// </summary> /// <returns></returns> public bool Open() { byte[] status = new byte[30]; byte[] ver = new byte[30]; int ret = -1; //string hexKey = "ffffffffffff"; //byte[] bytesKey = new byte[17]; switch (_readerType) { case ReaderType.COM: _icdev = MwRfSDK.rf_init(_port, _baud); ret = _icdev; if (ret < 0) { return(false); } // 加载Key for (int sector = 0; sector < 16; sector++) { ret = MwRfSDK.rf_load_key(_icdev, (byte)Mode.IDLE, sector, _key); if (ret != 0) { return(false); } } break; case ReaderType.NET: //MwRfSDKNet.rf_exit(_icdev); //_icdev = MwRfSDKNet.rf_init(Encoding.ASCII.GetBytes(_ip), 13); //ret = _icdev; //Int16 st = MwRfSDKNet.rf_get_status(_icdev, status); //if (_icdev >= 0 && st == 0) //{ // st = MwRfSDKNet.rf_lib_ver(ver); // // 加载Key // for (int sector = 0; sector < 16; sector++) // { // ret = MwRfSDKNet.rf_load_key(_icdev, (byte)Mode.IDLE, sector, _key); // if (ret != 0) return false; // } //} //else // return false; opennetreadcard(); break; } Status = DeviceStatus.OPEN; return(true); }
/// <summary> /// 连续读取该扇区的数据 /// </summary> /// <param name="sector">扇区</param> /// <param name="block">块号</param> /// <returns>读到的内容</returns> public string ReadSectorData(int sector, int block) { byte[] resultBuff = null; byte[] buff = new byte[16]; int ret = -1; switch (_readerType) { case ReaderType.COM: ret = MwRfSDK.rf_read(_icdev, 4 * sector + block, buff); if (ret != 0) { return(""); } break; case ReaderType.NET: //ret = MwRfSDKNet.rf_read(_icdev, 4 * sector + block, buff); resultBuff = Read(4 * sector + block); datalen = resultBuff.Length; if (datalen > 0) { if ((resultBuff[0] == 0xbb) & (resultBuff[1] == 0xff)) { switch (resultBuff[2]) { case 0x10: ret = 0; for (int j = 3; j < datalen; j++) { buff[j - 3] = resultBuff[j]; } break; default: ret = 1; break; } } } if (ret != 0) { return(""); } break; } string data = Encoding.Default.GetString(buff); return(data); }
public void Beep(int msec) { switch (_readerType) { case ReaderType.COM: MwRfSDK.rf_beep(_icdev, msec); break; case ReaderType.NET: //MwRfSDKNet.rf_beep(_icdev, msec); Beep(); break; } }
public bool StopCard() { int ret = -1; switch (_readerType) { case ReaderType.COM: ret = MwRfSDK.rf_halt(_icdev); if (ret != 0) { return(false); } break; case ReaderType.NET: //ret = MwRfSDKNet.rf_halt(_icdev); //if (ret != 0) return false; break; } return(true); }
public bool SelectCard() { int ret = -1; byte size = 0x00; switch (_readerType) { case ReaderType.COM: ret = MwRfSDK.rf_select(_icdev, _snr, out size); if (ret != 0) { return(false); } break; case ReaderType.NET: //ret = MwRfSDKNet.rf_select(_icdev, _snr, out size); //if (ret != 0) return false; break; } return(true); }
/// <summary> /// 关闭通讯口 /// </summary> /// <returns></returns> public bool Close() { int ret = -1; switch (_readerType) { case ReaderType.COM: ret = MwRfSDK.rf_exit(_icdev); _icdev = 0; if (ret < 0) { return(false); } break; case ReaderType.NET: //ret = MwRfSDKNet.rf_exit(_icdev); //_icdev = 0; //if (ret < 0) return false; //if (SendClient != null) { SendClient[this.m_iSelectedPound].Close(); SendClient = null; } if (SendClient != null) { CloseCard(); SendClient[this.m_iSelectedPound].Close(); SendClient[this.m_iSelectedPound] = null; } //if (SendClient != null) { SendClient[this.m_iSelectedPound].Close(); } break; } Status = DeviceStatus.CLOSE; return(true); }
public bool Auth(int sector) { int ret = -1; switch (_readerType) { case ReaderType.COM: ret = MwRfSDK.rf_authentication(_icdev, (byte)Mode.IDLE, sector); if (ret != 0) { return(false); } break; case ReaderType.NET: //ret = MwRfSDKNet.rf_authentication(_icdev, (byte)Mode.IDLE, sector); //if (ret != 0) //{ // return false; //} break; } return(true); }
/// <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); }
/// <summary> /// 读IC卡数据,此方法适用于不启用线程时读卡用, /// </summary> /// <param name="sector">扇区</param> /// <returns>读到的内容</returns> public string ReadData(int sector) { byte[] resultBuff = null; string data = ""; int ret = -1; byte[] buff = new byte[16]; //InitCard(sector); if (!Auth(sector)) { return(""); } switch (_readerType) { case ReaderType.COM: for (int block = 0; block < 3; block++) { for (int j = 0; j < buff.Length; j++) { buff[j] = 0x00; } ret = MwRfSDK.rf_read(_icdev, 4 * sector + block, buff); data += Encoding.Default.GetString(buff); if (buff[15] == 0x00) { break; } } break; case ReaderType.NET: for (int block = 0; block < 3; block++) { for (int j = 0; j < buff.Length; j++) { buff[j] = 0x00; } //ret = MwRfSDKNet.rf_read(_icdev, 4 * sector + block, buff); resultBuff = Read(4 * sector + block); datalen = resultBuff.Length; if (datalen > 0) { if ((resultBuff[0] == 0xbb) & (resultBuff[1] == 0xff)) { switch (resultBuff[2]) { case 0x10: ret = 0; for (int j = 3; j < datalen; j++) { buff[j - 3] = resultBuff[j]; } break; default: ret = 1; break; } } } data += Encoding.Default.GetString(buff); if (buff[15] == 0x00) { break; } } break; } return(data); }
public string InitCard() { _snr = 0; _guid = ""; int ret = -1; //string strTemp = ""; switch (_readerType) { case ReaderType.COM: ret = MwRfSDK.rf_reset(_icdev, 3); if (ret != 0) { _snr = 0; _guid = ""; return(""); } ret = MwRfSDK.rf_card(_icdev, (byte)Mode.IDLE, ref _snr); if (ret != 0) { _snr = 0; _guid = ""; return(""); } _guid = _snr.ToString("X"); break; case ReaderType.NET: //ret = MwRfSDKNet.rf_reset(_icdev, 3); //if (ret != 0) //{ // _snr = 0; // _guid = ""; // return ""; //} //ret = MwRfSDKNet.rf_card(_icdev, (byte)Mode.IDLE, ref _snr); //if (ret != 0) //{ // _snr = 0; // _guid = ""; // return ""; //} byte[] tempbuff = null; int datalen = 0; try { cardOfTesting(); Thread.Sleep(100); ep = null; tempbuff = SendClient[this.m_iSelectedPound].Receive(ref ep); Thread.Sleep(100); //Thread.Sleep(50); //cardOfTesting(); //Thread.Sleep(50); // tempbuff = this.ReciveMessage(); Thread.Sleep(50); this.CloseCard(); ep = null; byte[] buff = SendClient[this.m_iSelectedPound].Receive(ref ep); Thread.Sleep(50); // CloseCard(); } catch (Exception ex) { // CloseCard(); } if (tempbuff != null) { datalen = tempbuff.Length; } if (datalen > 0) { if ((tempbuff[0] == 0xbb) & (tempbuff[1] == 0xff)) { switch (tempbuff[2]) { case 0x70: ret = 0; for (int j = 3; j < datalen; j++) { tempbuff[j - 3] = tempbuff[j]; } //_guid = Encoding.Default.GetString(tempbuff); //_snr = Convert.ToUInt32(_guid); for (int i = 0; i < tempbuff.Length; i++) { _guid += funBtoHex(tempbuff[i]); } for (int i = 0; i < tempbuff.Length; i++) { _snr += tempbuff[i]; } //_snr = Convert.ToUInt32(_guid); break; default: ret = 1; if (ret != 0) { _snr = 0; _guid = ""; return(""); } break; } } } break; } return(_guid); }