public Invoker(Link862Reader reader) { this.Reader = reader; //this.Reader.Response += OnResponse; }
//fire event when card inserted or removed public void FireCardStateChanged() { int exception_time_counter = 0; const int MAX_EXCEPTION_TIME = 2; bool show_msg_only_once = false; Console.WriteLine("Thread started"); while (true) { try { while (true) { Command checkForCard = new Command(Operation.CheckPosition, "010000013838"); invoker.InvokeCommand(checkForCard); if (Link862Reader.PrevState != Link862Reader.State) { Link862Reader.PrevState = Link862Reader.State; break; } if (_readDeviceID == true) { //Command checkDescriptors = new Command(Operation.checkDescriptors, "010000013737"); Command checkDescriptors = new Command(Operation.CheckDescriptors, "01000002370135"); invoker.InvokeCommand(checkDescriptors); _readDeviceID = false; } else if (_readDeviceSN == true) { Command checkSN = new Command(Operation.CheckSN, "01000002370034"); invoker.InvokeCommand(checkSN); _readDeviceSN = false; } if (show_msg_only_once) { show_msg_only_once = false; } exception_time_counter = 0; } if (Link862Reader.State == 1) { if (StartedCardReading != null) { StartedCardReading(null, null); } Log.Debug("card inserted, read card number"); Link862Reader.CardNumber = ""; if (!ReadCardNumber()) { CardError(this, null); writeData = null; continue; } if (!string.IsNullOrEmpty(writeData)) { if (string.IsNullOrEmpty(writeData)) { throw new ArgumentNullException(); } writeData = writeData.Trim(); var numbertowrite = Link862Reader.ByteArrayToHexString(Encoding.ASCII.GetBytes(writeData)); var lengthHex = String.Format("{0:X}", writeData.Length); var array = Link862Reader.HexStringToByteArray("0100001F4200D00000" + lengthHex + "" + numbertowrite); byte xorValue = array[0]; for (int i = 1; i < array.Length; i++) { xorValue ^= array[i]; } string xor = Link862Reader.ByteArrayToHexString(new byte[] { xorValue }); Log.Debug("write card number command"); //invoker.InvokeCommand(new Command(Operation.SaveData, "0100001F4200D00000193F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3FAA")); invoker.InvokeCommand(new Command(Operation.SaveData, "0100001F4200D00000" + lengthHex + "" + numbertowrite + "" + xor)); Log.DebugFormat("write card number:{0}", writeData); writeData = null; if (!ReadCardNumber()) { CardError(this, null); continue; } } for (int i = 0; i < 3; i++) { if (Link862Reader.CardNumber.Length > 25) { Link862Reader.CardNumber = Link862Reader.CardNumber.Substring(0, 25); } if (Link862Reader.CardNumber.Length != 25 && !Link862Reader.IsNew) { if (!ReadCardNumber()) { CardError(this, null); continue; } } } if (Link862Reader.CardNumber.Length != 25 && !Link862Reader.IsNew) { if (CardError != null) { Link862Reader.COMPort.Close(); Link862Reader.COMPort.Open(); CardError(this, null); continue; } } if (InsertCard != null) { InsertCard(this, new CardEventArgs <string>(Link862Reader.CardNumber)); } } else { Log.Debug("card ejected"); Link862Reader.CardNumber = ""; if (EjectCard != null) { EjectCard(this, null); } } } catch (Exception ex) { /* if (ex is ArgumentNullException) * { * throw; * }*/ if (ShowMsg) { if (exception_time_counter++ > MAX_EXCEPTION_TIME) { if (show_msg_only_once == false) { CardError(this, null); show_msg_only_once = true; } exception_time_counter = 0; } } Thread.Sleep(1000); Link862Reader.TryReconnect(); } } }