private static void Test2() { do { try { Console.WriteLine("输入:串口ID 地址 命令 [数据 接收次数]"); string[] s = Console.ReadLine().Split(',', ' '); if (s[0] == "") { break; } int id = Convert.ToByte(s[0]); byte addr = Convert.ToByte(s[1], 16); byte cmd = Convert.ToByte(s[2], 16); int data = (s.Length > 3) ? Convert.ToInt32(s[3], 16) : 0; int cnt = (s.Length > 4) ? Convert.ToInt32(s[4]) : 1; TCardRecv recv = new TCardRecv(); CARD.SendCMD(id, addr, cmd, data, 100); for (int i = 0; i < cnt; i++) { DateTime d1 = DateTime.Now; bool bl = CARD.ReadData(id, recv, 200); TimeSpan t = new TimeSpan(DateTime.Now.Ticks - d1.Ticks); Console.Write(t.TotalMilliseconds.ToString("0ms:").PadRight(8, ' ')); if (bl) { Console.Write(recv.ToString()); string js = System.Text.Encoding.Default.GetString(recv.Data); Console.WriteLine(" " + js); } else { Console.WriteLine("读取超时"); } } Console.WriteLine(); } catch (Exception e) { Console.WriteLine(e.Message); } } while (true); }
/// <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="addr">指令地址</param> /// <param name="func">指令码</param> /// <param name="data">指令参数</param> /// <param name="recv">基于TCardRecv的对象,用于接收返回数据</param> /// <param name="TimeOut">超时的毫秒数</param> /// <returns>执行成功返回True,否则返回False</returns> public static bool Excute(byte addr, byte func, int data, TCardRecv recv, int TimeOut) { if (Port.Length < 1) { throw (new Exception("串口未指定!")); } else { return(Excute(1, addr, func, data, recv, TimeOut)); } }
private static void Test1() { do { try { Console.WriteLine("输入:串口ID 地址 命令 [数据 重复次数]"); string[] s = Console.ReadLine().Split(',', ' '); if (s[0] == "") break; int id = Convert.ToByte(s[0]); byte addr = Convert.ToByte(s[1], 16); byte cmd = Convert.ToByte(s[2], 16); int data = (s.Length > 3) ? Convert.ToInt32(s[3], 16) : 0; int cnt = (s.Length > 4) ? Convert.ToInt32(s[4]) : 1; TCardRecv recv = new TCardRecv(); for (int i = 0; i < cnt; i++) { DateTime d1 = DateTime.Now; if (CARD.Excute(id, addr, cmd, data, recv, 100)) { TimeSpan t = new TimeSpan(DateTime.Now.Ticks - d1.Ticks); Console.Write(t.TotalMilliseconds.ToString("0ms:").PadRight(8, ' ')); Console.Write(recv.ToString()); string js = System.Text.Encoding.Default.GetString(recv.Data); Console.WriteLine(" " + js); } } Console.WriteLine(); } catch (Exception e) { Console.WriteLine(e.Message); } } while (true); }
/// <summary> /// 执行卡指令,并读取返回数据 /// </summary> /// <param name="id">串口ID</param> /// <param name="addr">指令地址</param> /// <param name="func">指令码</param> /// <param name="data">指令参数</param> /// <param name="recv">基于TCardRecv的对象,用于接收返回数据</param> /// <returns>执行成功返回True,否则返回False</returns> protected bool ExcuteFunc(int id, byte addr, byte func, int data, TCardRecv recv) { try { SendCMD(id, addr, func, data); Thread.Sleep(30); recv.Addr = addr; recv.Func = func; recv.FuncData = data; return(ReadData(id, recv)); } catch (TimeoutException) { return(false); } }
/// <summary> /// 执行卡指令,并读取返回数据 /// </summary> /// <param name="addr">指令地址</param> /// <param name="func">指令码</param> /// <param name="data">指令参数</param> /// <param name="recv">基于TCardRecv的对象,用于接收返回数据</param> /// <param name="TimeOut">超时的毫秒数</param> /// <returns>执行成功返回True,否则返回False</returns> public static bool Excute(int id, byte addr, byte func, int data, TCardRecv recv, int TimeOut) { try { CARD.SendCMD(id, addr, func, data, TimeOut); if (recv != null) { Thread.Sleep(ExcuteDelay); return(CARD.ReadData(id, recv, TimeOut)); } else { return(true); } } catch (TimeoutException) { return(false); } }
/// <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); }
/// <summary> /// 执行卡指令,并读取返回数据 /// </summary> /// <param name="id">串口ID</param> /// <param name="addr">指令地址</param> /// <param name="func">指令码</param> /// <param name="data">指令参数</param> /// <param name="recv">基于TCardRecv的对象,用于接收返回数据</param> /// <returns>执行成功返回True,否则返回False</returns> protected bool ExcuteFunc(int id,byte addr, byte func, int data, TCardRecv recv) { try { SendCMD(id,addr, func, data); Thread.Sleep(30); recv.Addr = addr; recv.Func = func; recv.FuncData = data; return ReadData(id, recv); } catch (TimeoutException) { return false; } }
/// <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; }