Beispiel #1
0
        public bool DoInitialHandShaking()
        {
            var result = 0;

            Thread.Sleep(1000);
            var asciiEncoding = new ASCIIEncoding();

            try
            {
                var stopwatch1 = new Stopwatch();
                stopwatch1.Stop();
                stopwatch1.Reset();
                stopwatch1.Start();
                var stopwatch2 = new Stopwatch();
                stopwatch2.Stop();
                stopwatch2.Reset();
                stopwatch2.Start();
                WriteToSerial(asciiEncoding.GetBytes("M115\r\n"));
                InternalLogger.WriteLog("<< M115 :ASCII:", Logger.TextType.Write);
                var flag1 = true;
                var flag2 = false;
                var flag3 = false;
                var input = "";
                while (!shared_shutdown.Value)
                {
                    if (flag1 && stopwatch2.ElapsedMilliseconds > 2000L)
                    {
                        if (!flag2)
                        {
                            if (!WriteToSerial(asciiEncoding.GetBytes("M115\r\n")))
                            {
                                return(false);
                            }

                            InternalLogger.WriteLog("<< M115 :ASCII:(Resend)", Logger.TextType.Write);
                            stopwatch2.Stop();
                            stopwatch2.Reset();
                            stopwatch2.Start();
                            flag2 = true;
                        }
                        else
                        {
                            flag3 = true;
                        }
                    }
                    input += ReadExisting();
                    Match match = Regex.Match(input, "B\\d+", RegexOptions.CultureInvariant);
                    if (match.Success)
                    {
                        var str2 = match.Value;
                        if (!int.TryParse(str2.Substring(1), out result))
                        {
                            result = 0;
                        }

                        stopwatch1.Stop();
                        InternalLogger.WriteLog(">> " + str2, Logger.TextType.Read);
                        ControllerSelf = new BootloaderController(result, this, printerInfo, internal_logger, shared_shutdown, broadcastserver, MyPrinterProfile);
                        return(true);
                    }
                    var startIndex = input.IndexOf("ok");
                    if (startIndex >= 0)
                    {
                        if (flag1)
                        {
                            return(false);
                        }

                        stopwatch1.Stop();
                        ControllerSelf = new FirmwareController(input.Substring(startIndex), this, printerInfo, internal_logger, shared_shutdown, broadcastserver, MyPrinterProfile);
                        RegisterFirmwarePlugins();
                        return(true);
                    }
                    var length = input.IndexOf('\n');
                    if (length >= 0)
                    {
                        var str2 = input.Substring(0, length);
                        input = input.Substring(length + 1);
                        InternalLogger.WriteLog(">> " + str2, Logger.TextType.Read);
                        if (str2.Contains("e"))
                        {
                            flag3 = true;
                        }
                    }
                    if (flag3)
                    {
                        flag3 = false;
                        input = "";
                        if (!WriteToSerial(new GCode()
                        {
                            M = 115
                        }.getBinary(2)))
                        {
                            return(false);
                        }

                        InternalLogger.WriteLog("<< M115", Logger.TextType.Write);
                        flag1 = false;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.LogException("Exception in PrinterConnector.DoInitialHandshaking 3 " + ex.Message, ex);
                return(false);
            }
            return(false);
        }