Beispiel #1
0
        private static void Reader_CardReceived(object sender, CardDataEventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine($"Facility: {e.FacilityId}");
            sb.AppendLine($"Card:     {e.CardId}");
            sb.AppendLine($"{DateTime.Now}");

            Console.WriteLine(sb.ToString());
        }
 protected virtual void OnCardReceived(CardDataEventArgs args) => CardReceived?.Invoke(this, args);
        private async void ReadData()
        {
            try
            {
                if (serialPort == null)
                {
                    return;
                }

                if (!serialPort.IsOpen)
                {
                    throw new Exception($"serialPort {ComPort} Not Open!");
                }

                if (IsReading)
                {
                    return;
                }

                IsReading = true;

                while (true)
                {
                    int b = serialPort.ReadByte();
                    if (b == 6) // Do Nothing - Unit is jusr responding to a keep alive request.
                    {
                        continue;
                    }
                    else if (b == 2)
                    {
                        InputBuffer.Clear();
                        bool ContinueRead = true;
                        while (ContinueRead)
                        {
                            //bitCount = await reader.LoadAsync(1); //.AsTask();
                            b = serialPort.ReadByte();

                            if (System.Diagnostics.Debugger.IsAttached)
                            {
                                System.Diagnostics.Debug.Write(b);
                            }

                            if (b != 3)
                            {
                                InputBuffer.Add((byte)b);
                            }
                            else
                            {
                                ContinueRead = false;
                            }
                        }
                        string            result = System.Text.Encoding.UTF8.GetString(InputBuffer.ToArray());
                        CardDataEventArgs args   = new CardDataEventArgs(result);
                        OnCardReceived(args);
                    }
                }
            }
            catch (Exception ex)
            {
                Disconnect();
            }
            finally
            {
                IsReading = false;
            }
        }