public string readNFC_data() { // 读取设备 // 读取设备信息 OnLotusCardExtendReadWriteCallBackFunc pt = null; long nHandle = LotusCardOpenDevice("", 0, 0, 0, 0, pt); if (nHandle == -1) //MessageBox.Show("驱动读取失败!"); { return("驱动读取失败!"); } // Request寻卡 int nRequestType = CLotusCardDriver.RT_NOT_HALT; LotusCardParamStruct lotusCardParam = new LotusCardParamStruct();// 结构体 lotusCardParam.arrCardNo = new byte[8]; lotusCardParam.arrBuffer = new byte[64]; lotusCardParam.arrKeys = new byte[64]; lotusCardParam.arrCosResultBuffer = new byte[256]; int bResult = CLotusCardDriver.LotusCardRequest(nHandle, nRequestType, ref lotusCardParam); if (bResult == 0) //MessageBox.Show("寻卡失败"); { return("寻卡失败"); } // Anticoll读卡 bResult = Anticoll(nHandle, ref lotusCardParam); if (bResult == 0) //MessageBox.Show("读卡失败"); { return("读卡失败"); } // 读卡成功蜂鸣一下 bResult = LotusCardBeep(nHandle, 10); string nfc_id = string.Empty; //for (int i = 0; i < lotusCardParam.arrCardNo.Length - 1; i++) //{ // nfc_id += Convert.ToString(lotusCardParam.arrCardNo[i], 16).PadLeft(2, '0').ToUpper();// 解决左边输出编号为0,丢失0问题 //} // 读取data信息 int rd = Read(nHandle, 8, ref lotusCardParam); uint geterror = LotusCardGetErrorCode(nHandle); // MessageBox.Show(Convert.ToString(geterror), "geterror"); //if (rd == 0) //{ // MessageBox.Show("ok"); // for (int i = 0; i < lotusCardParam.arrBuffer.Length-1; i++) // { // nfc_id += Convert.ToString(lotusCardParam.arrBuffer[i], 16).PadLeft(2, '*').ToUpper();// 解决左边输出编号为0,丢失0问题 // } //} // 关闭设备 LotusCardCloseDevice(nHandle); return(nfc_id); }//getNFC_id()
public string getNFC_id(Boolean sound) { // 读取设备 // 读取设备信息 OnLotusCardExtendReadWriteCallBackFunc pt = null; long nHandle = LotusCardOpenDevice("", 0, 0, 0, 0, pt); if (nHandle == -1) { //MessageBox.Show("驱动读取失败!"); return(Convert.ToString(-1)); // "驱动读取失败!"; } // Request寻卡 int nRequestType = CLotusCardDriver.RT_NOT_HALT; LotusCardParamStruct lotusCardParam = new LotusCardParamStruct();// 结构体 lotusCardParam.arrCardNo = new byte[8]; lotusCardParam.arrBuffer = new byte[64]; lotusCardParam.arrKeys = new byte[64]; lotusCardParam.arrCosResultBuffer = new byte[256]; int bResult; bResult = CLotusCardDriver.LotusCardRequest(nHandle, nRequestType, ref lotusCardParam); if (bResult == 0) { //MessageBox.Show("寻卡失败"); return(Convert.ToString(-2)); //"寻卡失败"; } // Anticoll读卡 bResult = Anticoll(nHandle, ref lotusCardParam); if (bResult == 0) { //MessageBox.Show("读卡失败"); return(Convert.ToString(-3)); //"读卡失败"; } // 读卡成功蜂鸣一下 if (sound) { bResult = LotusCardBeep(nHandle, 10); } string nfc_id = string.Empty; //foreach (var i in lotusCardParam.arrCardNo) //{ // nfc_id +=Convert.ToString(i,16);// 可能会多取 //} for (int i = 0; i < lotusCardParam.arrCardNo.Length - 1; i++) { nfc_id += Convert.ToString(lotusCardParam.arrCardNo[i], 16).PadLeft(2, '0').ToUpper();// 解决左边输出编号为0,丢失0问题 } // 关闭设备 LotusCardCloseDevice(nHandle); return(nfc_id); }//getNFC_id()
// 重载函数有参 public string readNFC_data(Boolean sound) { bool innersound = true; innersound = sound; // 读取设备 // 读取设备信息 OnLotusCardExtendReadWriteCallBackFunc pt = null; int nHandle = LotusCardOpenDevice("", 0, 0, 0, 0, pt); if (nHandle == -1) { //MessageBox.Show("驱动读取失败!"); return("-1"); } // Request寻卡 int nRequestType = CLotusCardDriver.RT_NOT_HALT; LotusCardParamStruct lotusCardParam = new LotusCardParamStruct();// 结构体 lotusCardParam.nBufferSize = 0; lotusCardParam.arrCardNo = new byte[8]; lotusCardParam.arrBuffer = new byte[64]; lotusCardParam.arrKeys = new byte[64]; //lotusCardParam.arrCosResultBuffer = new byte[256]; lotusCardParam.arrCosSendBuffer = new byte[256]; int bResult = CLotusCardDriver.LotusCardRequest(nHandle, nRequestType, ref lotusCardParam); if (bResult == 0) { //MessageBox.Show("寻卡失败"); return("-2"); } // Anticoll读卡 bResult = Anticoll(nHandle, ref lotusCardParam); if (bResult == 0) { return("-3");// 读卡失败 } // 读卡成功蜂鸣一下 if (innersound) { bResult = LotusCardBeep(nHandle, 10); } // 重置卡 int reset = LotusCardResetCpuCard(nHandle, ref lotusCardParam); int sendCOSCommand = 2; string cosString = string.Empty; if (reset == 1) { // 有严格的顺序 // 指令一 cosString = "00A4040007D276000085010100"; mycopyCOSCommand(cosString, ref lotusCardParam); sendCOSCommand = LotusCardSendCOSCommand(nHandle, ref lotusCardParam);// 成功执行指令返回int 1 // 指令有误返回“-5” if (sendCOSCommand != 1) { return("-5"); } // 指令二 cosString = "00A4000C02E103"; mycopyCOSCommand(cosString, ref lotusCardParam); sendCOSCommand = LotusCardSendCOSCommand(nHandle, ref lotusCardParam);// 成功执行指令返回int 1 if (sendCOSCommand != 1) { return("-5"); } // 指令三 cosString = "00A4000C020001"; mycopyCOSCommand(cosString, ref lotusCardParam); sendCOSCommand = LotusCardSendCOSCommand(nHandle, ref lotusCardParam);// 成功执行指令返回int 1 if (sendCOSCommand != 1) { return("-5"); } // 指令四 cosString = " 00B0000008"; mycopyCOSCommand(cosString, ref lotusCardParam); sendCOSCommand = LotusCardSendCOSCommand(nHandle, ref lotusCardParam);// 成功执行指令返回int 1 if (sendCOSCommand != 1) { return("-5"); } } else { return("-4"); } // 重置卡失败 // 关闭设备 LotusCardCloseDevice(nHandle); string result = ""; for (int i = 0; i < lotusCardParam.unCosReultBufferLength; i++) { result += lotusCardParam.arrCosResultBuffer[i].ToString("X2"); } result = result.Replace("9000", ""); string new_result = string.Empty; for (int i = result.Length - 2; i >= 0; i -= 2) { new_result += result.Substring(i, 2); } return(new_result); }//getNFC_id()