Ejemplo n.º 1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            string errmsg = "";

            timer1.Stop();

            // Handle device plug/unplug events
            YAPI.UpdateDeviceList(ref errmsg);
            YAPI.HandleEvents(ref errmsg);
            if (spiPort != null)
            {
                // Yocto-SPI connected, check if a card swipe message was received
                List <string> msgs = spiPort.readMessages("", 1);
                if (DEBUG)
                {
                    for (int i = 0; i < msgs.Count; i++)
                    {
                        Log("Read: " + msgs[i]);
                    }
                }
                if (msgs.Count > 0)
                {
                    // Decode ABA Track 2 data from magnetic card
                    string track2    = this.DecodeTrack2(msgs[0]);
                    string firstChar = track2.Substring(0, 1);
                    string lastChar  = track2.Substring(track2.Length - 1);
                    Log("Track 2: " + track2);
                    if (firstChar == "!")
                    {
                        card_id.Text    = "Error !";
                        card_exp.Text   = "";
                        card_extra.Text = track2.Substring(2);
                    }
                    else if (firstChar == ";" && lastChar == "?")
                    {
                        int    separator = track2.IndexOf("=");
                        string ident     = track2.Substring(1, separator - 1);
                        for (int i = 4; i < ident.Length; i += 5)
                        {
                            ident = ident.Insert(i, " ");
                        }
                        card_id.Text    = ident;
                        card_exp.Text   = track2.Substring(separator + 3, 2) + "/" + track2.Substring(separator + 1, 2);
                        card_extra.Text = track2.Substring(separator + 8, track2.Length - separator - 9);
                    }
                    else
                    {
                        card_id.Text    = "Error !";
                        card_exp.Text   = "";
                        card_extra.Text = "Bad format, may be a reverse swipe ?";
                    }

                    // Clear read buffer to catch next swipe
                    spiPort.reset();
                }
            }

            timer1.Start();
        }
Ejemplo n.º 2
0
 public void ArrivalCallback(YModule module)
 {
     // Check if the device features a SPI Port
     for (int i = 0; i < module.functionCount(); i++)
     {
         if (module.functionType(i) == "SpiPort")
         {
             // SPI Port found, use it
             string identifier = module.get_serialNumber() + "." + module.functionId(i);
             Log("Using " + identifier);
             spiPort = YSpiPort.FindSpiPort(identifier);
             spiPort.set_spiMode("0,0,lsb");
             spiPort.reset();
         }
     }
 }
        private bool SendAndReceive(string[] commands, out Frame[] result)
        {
            int  i;
            bool success = true;

            spiPort.reset();
            for (i = 0; i < commands.Length; i++)
            {
                spiPort.writeHex(commands[i]);
            }
            // append an extra command to read result of the last command
            spiPort.writeHex(READ_ID);
            // wait for the result of all commands to come
            int expectedLength = commands.Length * 4;

            while (spiPort.read_avail() < expectedLength)
            {
                string errmsg = "";
                YAPI.Sleep(3, ref errmsg);
            }
            string hexstr = spiPort.readHex(2 * expectedLength);

            // Parse result
            result = new Frame[commands.Length];
            for (i = 0; i < commands.Length; i++)
            {
                Frame query = new Frame(commands[i]);
                result[i] = new Frame(hexstr.Substring(8 + 8 * i, 8));
                if (result[i].crc_error || result[i].addr != query.addr)
                {
                    success = false;
                }
                _chip_ready = (result[i].rs == 1);
            }

            return(success);
        }