Example #1
0
        //private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
        //{
        //    lock (_locker)
        //    {
        //        try
        //        {

        //            lastCommandResponce = DateTime.Now;
        //            // If the com port has been closed, do nothing
        //            if (!COMPort.IsOpen) return;
        //            // Obtain the number of bytes waiting in the port's buffer
        //            int bytes = COMPort.BytesToRead;
        //            // Create a byte array buffer to hold the incoming data
        //            IList<byte> buffer = new List<byte>();
        //            // Read the data from the port and store it in our buffer
        //            var buffertmp = new byte[bytes];
        //            COMPort.Read(buffertmp, 0, bytes);
        //            foreach (var b in buffertmp)
        //            {
        //                buffer.Add(b);
        //            }
        //            while (buffer.Count < LastCommand.ResponceLength)
        //            {
        //                buffertmp = new byte[bytes];
        //                COMPort.Read(buffertmp, 0, bytes);
        //                foreach (var b in buffertmp)
        //                {
        //                    buffer.Add(b);
        //                }
        //            }
        //            var res = new byte[buffer.Count];
        //            for (int i = 0; i < buffer.Count; i++)
        //            {
        //                res[i] = buffer[i];
        //            }

        //            SendResponse(res);
        //        }
        //        catch (Exception ex)
        //        {

        //            throw;
        //        }

        //        Monitor.Pulse(_locker);

        //    }
        //}

        /// <summary> Send the user's data currently entered in the 'send' box.</summary>
        public ReaderEvent Send(Command command)
        {
            ReaderEvent e1 = new ReaderEvent();

            LastCommand = command;

            try
            {
                SendCommand(command.Data);
                Thread.Sleep(250);

                lastCommandResponce = DateTime.Now;
                // If the com port has been closed, do nothing
                if (!COMPort.IsOpen)
                {
                    COMPort.Open();
                }
                // Obtain the number of bytes waiting in the port's buffer
                int bytes = 1;
                // Create a byte array buffer to hold the incoming data
                IList <byte> buffer = new List <byte>();
                // Read the data from the port and store it in our buffer
                var buffertmp = new byte[bytes];
                COMPort.Read(buffertmp, 0, bytes);
                foreach (var b in buffertmp)
                {
                    buffer.Add(b);
                }
                while (COMPort.BytesToRead > 0)
                {
                    if (COMPort.BytesToRead < 1)
                    {
                        break;
                    }
                    buffertmp = new byte[bytes];
                    COMPort.Read(buffertmp, 0, bytes);
                    foreach (var b in buffertmp)
                    {
                        buffer.Add(b);
                    }
                }
                var res = new byte[buffer.Count];
                for (int i = 0; i < buffer.Count; i++)
                {
                    res[i] = buffer[i];
                }

                e1.Data = Encoding.ASCII.GetString(res);
                return(e1);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #2
0
        //private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(CommandParser));

        public static void Parse(Command command, ReaderEvent response)
        {
            //Console.WriteLine(response.Data);
            switch (command.Operation)
            {
            case Operation.TransferData:
            {
                if (response.Data.Length < 5)
                {
                    Link862Reader.COMPort.Close();
                    Link862Reader.COMPort.Open();
                    break;
                }

                Log.Debug(response.Data);
                if (response.Data.Contains("!!"))
                {
                    Link862Reader.COMPort.DiscardOutBuffer();
                    Link862Reader.COMPort.DiscardInBuffer();
                    Link862Reader.COMPort.Dispose();
                    Link862Reader.COMPort.Close();
                    Link862Reader.COMPort.Open();
                    Link862Reader.CardNumber = "";
                    Link862Reader.IsNew      = false;
                    break;
                }
                string cardNumber = response.Data.Substring(4);
                cardNumber = cardNumber.Replace("\0", "");
                var cardNumberNoquestins = cardNumber;
                cardNumber = cardNumber.Replace("?", "");
                Link862Reader.CardNumber = cardNumber;
                Link862Reader.IsNew      = cardNumberNoquestins.Contains("?????") && cardNumber.Length < 2;
                if (Link862Reader.IsNew)
                {
                    Link862Reader.CardNumber = "?????????????????????????";
                }

                Log.Debug("Card number:" + cardNumber);
            }
            break;

            case Operation.ActivateChip:
                break;

            case Operation.SaveData:
                Link862Reader.COMPort.Close();
                Link862Reader.COMPort.Open();
                break;

            case Operation.CheckPosition:
                if (response.Data.Contains("ss"))
                {
                    Link862Reader.State = 1;
                }
                if (response.Data.Contains("pp"))
                {
                    Link862Reader.State = 0;
                }

                break;

            case Operation.CheckDescriptors:
                try
                {
                    if (response.Data.Length > 5)
                    {
                        int len = response.Data.Length - 5;
                        Link862Reader.IdReaderHW = response.Data.Substring(4, len);
                    }
                    else
                    {
                        Link862Reader.IdReaderHW = null;
                    }
                }
                catch
                {
                    Link862Reader.IdReaderHW = null;
                }
                break;

            case Operation.CheckSN:
                try
                {
                    if (response.Data.Length > 5)
                    {
                        int len = response.Data.Length - 5;
                        Link862Reader.IdReaderSN = response.Data.Substring(4, len);
                    }
                    else
                    {
                        Link862Reader.IdReaderSN = null;
                    }
                }
                catch
                {
                    Link862Reader.IdReaderSN = null;
                }
                break;
            }
        }