Example #1
0
        private void timerWrite_Tick(object sender, EventArgs e)
        {
            byte[] dataBytes = EMCardEncoder.ReadBytes(maskedTextBoxWrite.Text);
            if (dataBytes == null)
            {
                throw new ArgumentNullException("data");
            }
            if (dataBytes.Length == 0)
            {
                throw new ArgumentException("Data must be provided", "data");
            }
            if (dataBytes.Length != 5)
            {
                throw new ArgumentException("Provided data must be 5 bytes", "data");
            }

            // Check if there is a message pending
            byte[] ack = emCardEncoder.ReadMessage();
            if (ByteArrayCompare(ack, dataBytes))
            {
                maskedTextBoxRead.Text = (EMCardEncoder.FormatBytes(ack));
                timerRead.Enabled      = false;
            }
            else
            {
                emCardEncoder.SendWrite(dataBytes, writeProtect);
                Thread.Sleep(250);
                ack = emCardEncoder.ReadMessage();

                emCardEncoder.SendRead();
                Thread.Sleep(50);
                ack = emCardEncoder.ReadMessage();
            }
        }
Example #2
0
 private void timerPoll_Tick(object sender, EventArgs e)
 {
     // Check if there is a message pending
     byte[] ack = emCardEncoder.ReadMessage();
     if ((ack != null) && (ack.Length > 0))
     {
         maskedTextBoxRead.Text = (EMCardEncoder.FormatBytes(ack));
     }
     else
     {
         emCardEncoder.SendRead();
     }
 }
Example #3
0
 public static void Poll(string port)
 {
     using (var emCardEncoder = new EMCardEncoder())
     {
         emCardEncoder.Open(port);
         while (true)
         {
             emCardEncoder.SendRead();
             Thread.Sleep(50);
             byte[] ack = emCardEncoder.ReadMessage();
             if ((ack != null) && (ack.Length > 0))
             {
                 System.Console.WriteLine(EMCardEncoder.FormatBytes(ack));
             }
         }
     }
 }
Example #4
0
 public static void Hello(string port)
 {
     using (var emCardEncoder = new EMCardEncoder())
     {
         emCardEncoder.Open(port);
         emCardEncoder.SendHello();
         Thread.Sleep(50);
         byte[] ack = emCardEncoder.ReadMessage();
         if ((ack != null) && (ack.Length > 0))
         {
             System.Console.WriteLine(EMCardEncoder.FormatBytes(ack));
         }
         else if ((ack != null) && (ack.Length == 0))
         {
             System.Console.WriteLine("ACK");
         }
     }
 }
Example #5
0
        public static void Write(string port, string data, bool wait, bool writeProtect)
        {
            byte[] dataBytes = EMCardEncoder.ReadBytes(data);
            if (dataBytes == null)
            {
                throw new ArgumentNullException("data");
            }
            if (dataBytes.Length == 0)
            {
                throw new ArgumentException("Data must be provided", "data");
            }
            if (dataBytes.Length != 5)
            {
                throw new ArgumentException("Provided data must be 5 bytes", "data");
            }

            using (var emCardEncoder = new EMCardEncoder())
            {
                emCardEncoder.Open(port);

                byte[] ack = null;
                do
                {
                    emCardEncoder.SendWrite(dataBytes, writeProtect);
                    Thread.Sleep(250);
                    ack = emCardEncoder.ReadMessage();
                    Thread.Sleep(50);
                    emCardEncoder.SendRead();
                    Thread.Sleep(50);
                    ack = emCardEncoder.ReadMessage();
                }while ((!ByteArrayCompare(ack, dataBytes)) && wait);

                if ((ack != null) && (ack.Length > 0))
                {
                    System.Console.WriteLine(EMCardEncoder.FormatBytes(ack));
                }
                else if ((ack != null) && (ack.Length == 0))
                {
                    System.Console.WriteLine("ACK");
                }
            }
        }
Example #6
0
        private void buttonWrite_Click(object sender, EventArgs e)
        {
            byte[] dataBytes = EMCardEncoder.ReadBytes(maskedTextBoxWrite.Text);
            if (dataBytes == null)
            {
                throw new ArgumentNullException("data");
            }
            if (dataBytes.Length == 0)
            {
                throw new ArgumentException("Data must be provided", "data");
            }
            if (dataBytes.Length != 5)
            {
                throw new ArgumentException("Provided data must be 5 bytes", "data");
            }

            if (emCardEncoder != null)
            {
                byte[] ack = null;
                do
                {
                    emCardEncoder.SendWrite(dataBytes, writeProtect);
                    Thread.Sleep(250);
                    ack = emCardEncoder.ReadMessage();

                    emCardEncoder.SendRead();
                    Thread.Sleep(50);
                    ack = emCardEncoder.ReadMessage();
                }while ((!ByteArrayCompare(ack, dataBytes)) && wait);


                if ((ack != null) && (ack.Length > 0))
                {
                    maskedTextBoxRead.Text = (EMCardEncoder.FormatBytes(ack));
                }
            }
        }
Example #7
0
        private void comboBoxPortNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            port = null;
            if (emCardEncoder != null)
            {
                emCardEncoder.Close();
                emCardEncoder.Dispose();
                emCardEncoder = null;
            }
            string selectedPort = comboBoxPortNames.SelectedItem as string;

            try
            {
                emCardEncoder = new EMCardEncoder();

                emCardEncoder.Open(selectedPort);
                emCardEncoder.SendHello();
                Thread.Sleep(50);
                byte[] ack = emCardEncoder.ReadMessage();
                if ((ack != null) && (ack.Length == 0))
                {
                    port = selectedPort;
                }
                else
                {
                    port = null;
                    emCardEncoder.Close();
                    emCardEncoder.Dispose();
                    emCardEncoder = null;
                }
            }
            catch (Exception)
            {
            }

            TogglePortCorrect();
        }
Example #8
0
        private void buttonRead_Click(object sender, EventArgs e)
        {
            if (wait)
            {
                timerRead.Enabled = true;
                return;
            }

            if (emCardEncoder != null)
            {
                byte[] ack = null;
                do
                {
                    emCardEncoder.SendRead();
                    Thread.Sleep(50);
                    ack = emCardEncoder.ReadMessage();
                }while ((ack == null) && wait);

                if ((ack != null) && (ack.Length > 0))
                {
                    maskedTextBoxRead.Text = (EMCardEncoder.FormatBytes(ack));
                }
            }
        }