/// <summary> /// 向串口发送指令 /// </summary> /// <param name="id">COM口序号</param> /// <param name="mac">无线终端MAC</param> /// <param name="addr">卡地址</param> /// <param name="func">功能号</param> /// <param name="data">参数数组,高字节在前</param> /// <param name="TimeOut">等待超时的毫秒数</param> public static void SendCMD(int id, short mac, byte addr, byte func, int[] data, int TimeOut) { byte[] cmd = new byte[8 + data.Length * 4]; FUNC.GetBytes(mac, out cmd[0], out cmd[1]); cmd[2] = Convert.ToByte('W'); cmd[3] = Convert.ToByte('A'); cmd[4] = Convert.ToByte('T'); cmd[5] = addr; cmd[6] = func; for (int i = 0; i < data.Length; i++) { byte[] d = FUNC.GetBytes(data[i]); cmd[7 + i * 4] = d[0]; cmd[8 + i * 4] = d[1]; cmd[9 + i * 4] = d[2]; cmd[10 + i * 4] = d[3]; } cmd[cmd.Length - 1] = FUNC.CRC8(cmd, 2, cmd.Length - 3); for (int i = 0; i < cmd.Length; i++) { Debug.Write(cmd[i].ToString("X2") + " "); } SendBytes(id, cmd, TimeOut); }
/// <summary> /// 读取卡的返回数据 /// </summary> /// <param name="data">基于TCardRecv的子类,用于保存返回数据</param> /// <param name="TimeOut">超时的毫秒数</param> /// <returns>读取成功返回True,否则返回False</returns> public static bool ReadData(int id, TCardRecv data, int TimeOut) { byte[] buf = new byte[15]; string Head = ""; int size = 0; while (size < buf.Length) { //if (size > 0) FUNC.Sleep(10); int i = ReadBytes(id, buf, size, buf.Length - size, TimeOut); if (i == 0) { return(false); //读超时 } size = size + i; Debug.WriteLine(string.Format("Card.ReadData:({0}) {1} ", size, FUNC.BytesToString(buf))); if (size >= 3) //超过三字节,判断wat起始标记 { char[] ch = new char[] { Convert.ToChar(buf[0]), Convert.ToChar(buf[1]), Convert.ToChar(buf[2]) }; Head = new string(ch, 0, 3); if (Head.ToLower() != "wat") //起始字节不是wat,去掉一个字节,继续读取 { for (int j = 0; j < size - 1; j++) { buf[j] = buf[j + 1]; //前移一个字节 } size = size - 1; } } } ; byte c = FUNC.CRC8(buf, 0, 14); if (c != buf[14]) { return(false); } data.Head = Head; data.Addr = buf[3]; for (int i = 0; i < 10; i++) { data[i] = buf[i + 4]; } return(true); }
/// <summary> /// 向串口发送指令 /// </summary> /// <param name="id">COM口序号</param> /// <param name="addr">卡地址</param> /// <param name="func">功能号</param> /// <param name="data">参数,高字节在前</param> /// <param name="TimeOut">等待超时的毫秒数</param> public static void SendCMD(int id, byte addr, byte func, int data, int TimeOut) { byte[] cmd = new byte[10]; byte[] d = FUNC.GetBytes(data); cmd[0] = Convert.ToByte('W'); cmd[1] = Convert.ToByte('A'); cmd[2] = Convert.ToByte('T'); cmd[3] = addr; cmd[4] = func; cmd[5] = d[0]; cmd[6] = d[1]; cmd[7] = d[2]; cmd[8] = d[3]; cmd[9] = FUNC.CRC8(cmd, 0, 9); SendBytes(id, cmd, TimeOut); }
/// <summary> /// 向串口发送指令 /// </summary> /// <param name="id">COM口序号</param> /// <param name="addr">卡地址</param> /// <param name="func">功能号</param> /// <param name="data">参数数组,高字节在前</param> /// <param name="TimeOut">等待超时的毫秒数</param> public static void SendCMD(int id, byte addr, byte func, int[] data, int TimeOut) { byte[] cmd = new byte[6 + data.Length * 4]; cmd[0] = Convert.ToByte('W'); cmd[1] = Convert.ToByte('A'); cmd[2] = Convert.ToByte('T'); cmd[3] = addr; cmd[4] = func; for (int i = 0; i < data.Length; i++) { byte[] d = FUNC.GetBytes(data[i]); cmd[5 + i * 4] = d[0]; cmd[6 + i * 4] = d[1]; cmd[7 + i * 4] = d[2]; cmd[8 + i * 4] = d[3]; } cmd[cmd.Length - 1] = FUNC.CRC8(cmd, 0, cmd.Length - 1); SendBytes(id, cmd, TimeOut); }
/// <summary> /// 读取卡的返回数据 /// </summary> /// <param name="id">串口ID</param> /// <param name="data">基于TCardRecv的子类,用于保存返回数据</param> /// <returns>读取成功返回True,否则返回False</returns> protected bool ReadData(int id, TCardRecv data) { byte[] buf = new byte[15]; int i = ReadBytes(id, buf, 15); if (i != 15) { return(false); } byte c = FUNC.CRC8(buf, 0, 14); if (c != buf[14]) { return(false); } char[] ch = new char[] { Convert.ToChar(buf[0]), Convert.ToChar(buf[1]), Convert.ToChar(buf[2]) }; data.Head = new string(ch, 0, 3); if (data.Head.ToLower() != "wat") { return(false); } for (i = 0; i < 10; i++) { data[i] = buf[i + 4]; } data.Addr = buf[3]; return(true); }