/// <summary> /// 写卡 /// </summary> /// <param name="hadler">打开的串口句柄</param> /// <param name="sqadd">扇区地址</param> /// /// <param name="blockAddr">扇区块地址 0——2 第三块地址为密码存储块</param> /// <param name="value">写入的值,一个扇区块值不能超过10个字节</param> /// <returns></returns> public bool WriteCard(IntPtr hadler, string value) { ASCIIEncoding AE1 = new ASCIIEncoding(); int rs = -1; if (value.Length > 16) { byte[] ByteArray1 = AE1.GetBytes(value.Substring(0, 16)); byte[] ByteArray2 = AE1.GetBytes(value.Substring(16)); rs = CRTCard.RF610_S50WriteBlock(hadler, Convert.ToByte(2), Convert.ToByte(0), ByteArray1, ""); rs = CRTCard.RF610_S50WriteBlock(hadler, Convert.ToByte(2), Convert.ToByte(1), ByteArray2, ""); } else { byte[] ByteArray1 = AE1.GetBytes(value); rs = CRTCard.RF610_S50WriteBlock(hadler, Convert.ToByte(2), Convert.ToByte(0), ByteArray1, ""); } if (rs == 0) { return(true); } else { return(false); } }
/// <summary> /// 写卡 界面调用 /// </summary> /// <param name="cardNo"></param> /// <returns></returns> public int WriteCardFacade(string cardNo) { IntPtr ip = OpenPort(); if (ip.ToInt32() == 0) { return(1); } try { if (!FindCard(ip)) { return(2); } if (!ValidPass(ip)) { return(3); } if (!WriteCard(ip, cardNo)) { return(5); } return(0); } catch (Exception ex) { throw new Exception(ex.Message); } finally { CRTCard.RF610_CommClose(ip); } }
/// <summary> /// 得到发卡机状态 /// </summary> /// <param name="hadler">打开的串口句柄</param> /// <param name="add">机器地址:0——15</param> /// <returns>状态byte数组 具体含义参照发卡机文档</returns> public string getstatus(IntPtr hadler) { byte[] stateinfo = new byte[4]; int rs = CRTCard.D1000_SensorQuery(hadler, Convert.ToByte(0), stateinfo); if (rs == 0) { return(stateinfo[0].ToString() + stateinfo[1].ToString() + stateinfo[2].ToString() + stateinfo[3].ToString()); } return(string.Empty);; }
/// <summary> /// 卡密校验 /// </summary> /// <param name="sqadd">扇区地址 s50 三个扇区,第三扇区为密码存储扇区 占后6个字节,谨慎写操作</param> /// <param name="KeyType">密码类型</param> /// <param name="key">密码</param> /// <returns></returns> public bool ValidPass(IntPtr hadler) { long Key = 281474976710655; int rskey = CRTCard.RF610_S50LoadSecKey(hadler, Convert.ToByte(2), Convert.ToByte(48), ref Key, ""); if (rskey == 0) { return(true); } else { return(false); } }
/// <summary> /// 寻卡 /// </summary> /// <param name="hadler">打开的串口句柄</param> /// <returns>true 寻卡成功,false 寻卡失败</returns> public bool FindCard(IntPtr hadler) { StringBuilder temp = new StringBuilder(64); int rs = CRTCard.RF610_S50DetectCard(hadler, temp); if (rs == 0) { return(true); } else { return(false); } }
/// <summary> /// 发送指令 /// </summary> /// <param name="hadler">打开的串口句柄</param> /// <param name="add">机器地址 0——15</param> /// <param name="cmd">指令内容 具体参考发卡器文档</param> /// <returns></returns> public bool sendcmd(string cmd) { IntPtr hadler = Init(); int rs = 0; if (hadler.ToInt32() == 0) { MyMsg.MsgInfo("端口打开失败!"); return(false); } try { rs = CRTCard.D1000_SendCmd(hadler, Convert.ToByte(0), "RS", 2); if (rs == 0) { Thread.Sleep(2000); string rsmsg = checkD1000(getstatus(hadler)); if (rsmsg == string.Empty) { rs = CRTCard.D1000_SendCmd(hadler, Convert.ToByte(0), cmd, cmd.Length); if (rs == 0) { return(true); } } else { MyMsg.MsgInfo(rsmsg); return(false); } } return(false); } catch (Exception ex) { throw new Exception(ex.Message); } finally { ClosePort(hadler); } }
/// <summary> /// 关闭端口 /// </summary> /// <param name="hadler">打开的串口句柄</param> public void ClosePort(IntPtr hadler) { int rs = CRTCard.D1000_CommClose(hadler); }
/// <summary> /// 发卡器初始化 /// </summary> /// <param name="add">机器地址 0——15</param> /// <returns>返回串口句柄</returns> public IntPtr Init() { IntPtr hadler = CRTCard.CommOpenWithBaud("com2", 9600); return(hadler); }
/// <summary> /// 打开端口 /// </summary> /// <returns>打开串口句柄</returns> public IntPtr OpenPort() { IntPtr hadler = CRTCard.RF610_CommOpenWithBaud("com5", 9600); return(hadler); }