public static int Scan(ArduinoCom com, bool message = true, bool timeout = true)
        {
            //if (message)

            //    Log.Info("Place thumb on scanner");
            Stopwatch stopwatch = new Stopwatch();

            if (timeout)
            {
                stopwatch.Start();
            }
            var    finrelay = com.FingerRelay;
            string str      = finrelay.SendCommandGetData(FingerRelay.Commands.GET_FINGERPRINT).Trim('\n', '\r');

            while (str == "END")
            {
                if (timeout)
                {
                    if (stopwatch.ElapsedMilliseconds > 10000)
                    {
                        return(-1);
                    }
                }
                str = finrelay.SendCommandGetData(FingerRelay.Commands.GET_FINGERPRINT).Trim('\n', '\r');
            }
            try
            {
                return(Convert.ToInt16(str.Split('_').Last()));
            }
            catch (Exception)
            {
                return(-1);
            }
        }
Beispiel #2
0
 public bool TryOpen(string port, int baud, out ArduinoCom arduinoCom)
 {
     arduinoCom = new ArduinoCom();
     try
     {
         SerialPort serialPort = new SerialPort(port, baud);
         serialPort.Open();
         //Form1.AppendTextBox($"Arduino communications service started on {serialPort.PortName}\n");
         arduinoCom = new ArduinoCom(serialPort);
         return(true);
     }
     catch (Exception e)
     {
         Form1.AppendTextBox($"{e.Message}\n");
     }
     return(false);
 }
        //public static void DeleteWizard()
        //{
        //    bool deletefromarduino = false;
        //    deletefromarduino = Log.QueryBool("Delete the key from the arduino?");
        //    if (Log.QueryBool("Delete by user (y) or Id (n)"))
        //    {
        //        var user = Log.QueryMultiChoice("User to delete?", Program.fingerAccounts.Select(n => n.Name).ToList());
        //        var tempid = Program.fingerAccounts.Where(n => n.Name == user).First().templateId;
        //        if (deletefromarduino && INTERFACEDelete(tempid) == 1)
        //        {
        //            if (Log.QueryBool("An error occured, delete the users with matching Id?"))
        //            {

        //                var to = deletes(tempid);
        //                if (to.Count < 1)
        //                {
        //                    Log.Info("The users have been spared");
        //                }
        //                else
        //                {
        //                    Log.Info("Deleting...");
        //                    foreach (var usr in to)
        //                    {
        //                        Console.WriteLine(usr.Name);
        //                    }
        //                    Program.fingerAccounts.RemoveAll(n => to.Any(n.Equals));
        //                }
        //            }
        //            else
        //            {
        //                Log.Info("The users have been spared");
        //            }
        //        }
        //        else
        //        {
        //            var to = deletes(tempid);
        //            if (to.Count < 1)
        //            {
        //                Log.Info("The users have been spared");
        //            }
        //            else
        //            {
        //                Log.Info("Deleting...");
        //                foreach (var usr in to)
        //                {
        //                    Console.WriteLine(usr.Name);
        //                }
        //                Program.fingerAccounts.RemoveAll(n => to.Any(n.Equals));
        //            }

        //        }
        //    }
        //    else
        //    {
        //        int delete = Log.QueryInt($"Template Number?", 1, 127);
        //        if (deletefromarduino && INTERFACEDelete(delete) == 1)
        //        {
        //            if (Log.QueryBool("An error occured, delete the users with matching Id?"))
        //            {
        //                var to = deletes(delete);
        //                if (to.Count < 1)
        //                {
        //                    Log.Info("The users have been spared");
        //                }
        //                else
        //                {
        //                    Log.Info("Deleting...");
        //                    foreach (var usr in to)
        //                    {
        //                        Console.WriteLine(usr.Name);
        //                    }
        //                    Program.fingerAccounts.RemoveAll(n => to.Any(n.Equals));
        //                }
        //            }
        //            else
        //            {
        //                Log.Info("The users have been spared");
        //            }
        //        }
        //        else
        //        {
        //            var to = deletes(delete);
        //            if (to.Count < 1)
        //            {
        //                Log.Info("The users have been spared");
        //            }
        //            else
        //            {
        //                Log.Info("Deleting...");
        //                foreach (var usr in to)
        //                {
        //                    Console.WriteLine(usr.Name);
        //                }
        //                Program.fingerAccounts.RemoveAll(n => to.Any(n.Equals));
        //            }
        //        }
        //    }
        //}

        //private static List<FingerAccount> deletes(int id)
        //{
        //    var ans = (Log.QueryMultiChoice("Type of delete", options));
        //    if (ans == options[0])
        //    {
        //        return Program.fingerAccounts.Where(n => n.templateId == id).ToList();
        //    }
        //    else if (ans == options[1])
        //    {
        //        List<FingerAccount> fff = new List<FingerAccount>();
        //        while (true)
        //        {
        //            var op = Log.QueryMultiChoice("Users to delete", new List<string>(Program.fingerAccounts.Where(n => n.templateId == id && !(fff.Any(n.Equals))).Select(n => n.Name)) { "Done" });
        //            if (op == "Done")
        //            {
        //                return fff;
        //            }
        //            fff.Add(Program.fingerAccounts.Where(n => n.Name == op && n.templateId == id).First());
        //        }
        //    }
        //    else if (ans == options[2])
        //    {
        //        return new List<FingerAccount>() { Program.fingerAccounts.Where(n => n.templateId == id).First() };
        //    }
        //    else if (ans == options[3])
        //    {
        //        return new List<FingerAccount>();
        //    }
        //    else if (ans == options[4])
        //    {
        //        Program.fingerAccounts.Where(n => n.templateId == id).Select(n => n.templateId = 0);
        //        return new List<FingerAccount>();
        //    }
        //    return new List<FingerAccount>();
        //}

        public static int INTERFACEDelete(ArduinoCom com, int templatenum = 0)
        {
            var finray = com.FingerRelay;

            if (templatenum == 0)
            {
                return(1);
                //templatenum = Log.QueryInt($"Template Number, Current Templates {Program.fingerRelay.TemplateCount}", 1, 127);
            }
            string init = finray.SendCommandGetData(FingerRelay.Commands.DELETE_USER);

            finray.serialPort.DiscardInBuffer();
            finray.SendByte((byte)templatenum);
            Console.WriteLine(finray.RecieveData());
            switch (finray.RecieveData())
            {
            case "OK":
                return(0);

            case "PACKREC":
            case "ERROR":
                //Log.Warning("Arduino errored");
                Form1.AppendTextBox("Arduino errored\n");
                return(1);

            case "FLASHERR":
                Form1.AppendTextBox("Flash error\n");
                //Log.Warning("Flash error");
                return(1);

            case "BADLOC":
                Form1.AppendTextBox("Bad location\n");
                //Log.Warning("Bad location");
                return(1);
            }
            finray.serialPort.DiscardInBuffer();
            return(1);
        }
        public static int CreateAScan(ArduinoCom arduinoCom, int templatenum = 0)
        {
            var fingerrelay = arduinoCom.FingerRelay;

            if (templatenum == 0)
            {
                return(0);
                //templatenum = Log.QueryInt($"Template Number, Current Templates {Program.fingerRelay.TemplateCount}", 1, 127);
            }
            string init = fingerrelay.SendCommandGetData(FingerRelay.Commands.ENROLL_FINGER);

            fingerrelay.serialPort.DiscardInBuffer();
            fingerrelay.SendByte((byte)templatenum);
            Console.WriteLine(fingerrelay.RecieveData());
            //Log.Info("Waiting for finger...");
            bool boolBreak = false;

            while (!boolBreak)
            {
                init = fingerrelay.RecieveData();
                switch (init)
                {
                case "SCANNER_OK":
                    boolBreak = true;
                    break;

                case "SCANNER_NOFINGER":
                    continue;

                case "SCANNER_IMAGEFAIL":
                    continue;

                case "SCANNER_ERROR":
                    continue;
                }
            }
            //Log.Success("Found Finger");
            boolBreak = false;
            while (!boolBreak)
            {
                init = fingerrelay.RecieveData();
                switch (init)
                {
                case "SCANNER_OK":
                    boolBreak = true;
                    break;

                case "SCANNER_IMAGEMESS":
                    Form1.AppendTextBox("Imaged finger was too messy, try again\n");
                    //Log.Info("Imaged finger was too messy, try again");
                    return(0);

                case "SCANNER_IMAGEFAIL":
                    Form1.AppendTextBox("Could not find fingerprint features, try again\n");
                    //Log.Info("Could not find fingerprint features, try again");
                    return(0);

                case "SCANNER_ERROR":
                    Form1.AppendTextBox("Scanner errored during scan\n");
                    //Log.Info("Scanner errored.");
                    return(0);
                }
            }
            //Log.Success("Image converted successfully");
            //Log.Info("Now remove finger");
            Form1.AppendTextBox("Remove finger from scanner\n");
            Task.Delay(3000).GetAwaiter();
            int id = 0;

            try
            {
                id = Convert.ToInt16(fingerrelay.RecieveData().Split('_').Last());
                //Console.WriteLine($"Template Id: {id}");
            }
            catch (Exception)
            {
                //Log.Error("Failed. Try again");
            }

            //Log.Info("Place same finger again");
            Form1.AppendTextBox("Place same finger again\n");
            boolBreak = false;
            while (!boolBreak)
            {
                init = fingerrelay.RecieveData();
                switch (init)
                {
                case "SCANNER_OK":
                    boolBreak = true;
                    break;

                case "SCANNER_NOFINGER":
                    continue;

                case "SCANNER_IMAGEFAIL":
                    continue;

                case "SCANNER_ERROR":
                    continue;
                }
            }

            boolBreak = false;
            while (!boolBreak)
            {
                init = fingerrelay.RecieveData();
                switch (init)
                {
                case "SCANNER_OK":
                    boolBreak = true;
                    break;

                case "SCANNER_IMAGEMESS":
                    Form1.AppendTextBox("Imaged finger was too messy, try again\n");
                    //Log.Info("Imaged finger was too messy, try again");
                    return(0);

                case "SCANNER_IMAGEFAIL":
                    //Log.Info("Could not find fingerprint features, try again");
                    Form1.AppendTextBox("Could not find fingerprint features, try again\n");
                    return(0);

                case "SCANNER_ERROR":
                    //Log.Info("Scanner errored.");
                    Form1.AppendTextBox("Scanner errored, try again\n");
                    return(0);
                }
            }

            boolBreak = false;
            while (!boolBreak)
            {
                init = fingerrelay.RecieveData();
                switch (init)
                {
                case "MATCH":
                    boolBreak = true;
                    Form1.AppendTextBox("Success!\n");
                    //Log.Success("Fingers matched!");
                    break;

                case "ERROR":
                    Form1.AppendTextBox("Scanner errored, try again\n");
                    //Log.Info("Scanner errored.");
                    return(0);

                case "NOMATCH":
                    Form1.AppendTextBox("Fingers did not match, try again\n");
                    //Log.Info("Fingers did not match.");
                    return(0);
                }
            }

            boolBreak = false;
            while (!boolBreak)
            {
                init = fingerrelay.RecieveData();
                switch (init)
                {
                case "OK":
                    //Log.Success("Finger stored");
                    return(id);

                case "ERROR":
                    //Log.Info("Scanner errored.");
                    Form1.AppendTextBox("Scanner errored, try again\n");
                    return(0);

                case "BADLOC":
                    //Log.Info("Bad scan location, try again");
                    Form1.AppendTextBox("Bad scan location, try again\n");
                    return(0);

                case "FLASHERR":
                    //Log.Info("Error writing to flash, try again");
                    Form1.AppendTextBox("Error, scanner could not write template to flash\n");
                    return(0);
                }
            }
            return(0);
        }