Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        private bool ProcessConnectedPrinter()
        {
            try
            {
                if (Status == PrinterStatus.Error_PrinterNotAlive)
                {
                    return(false);
                }

                if (!IsOpen)
                {
                    Status = PrinterStatus.Error_PrinterNotAlive;
                }

                BaseController controllerSelf = ControllerSelf;
                if (controllerSelf != null)
                {
                    controllerSelf.DoConnectionLogic();
                    if (!controllerSelf.IsWorking || Status == PrinterStatus.Firmware_PrintingPaused)
                    {
                        var num = CurrentRPC_id.Value;
                        if (num != 0U && (int)num != (int)Info.synchronization.LastCompletedRPCID)
                        {
                            Info.synchronization.LastCompletedRPCID = num;
                        }
                    }
                    else
                    {
                        lock (lockResetTimer)
                        {
                            if (lockResetTimer.IsRunning)
                            {
                                lockResetTimer.Restart();
                            }
                        }
                    }
                    if (lockStatus == PrinterConnection.LockStatus.Pending && Status != PrinterStatus.Connecting && (!controllerSelf.IsWorking && !controllerSelf.IsPrinting || Status == PrinterStatus.Firmware_PrintingPaused))
                    {
                        lockStatus = PrinterConnection.LockStatus.Locked;
                        Info.synchronization.Locked = true;
                        BroadcastServer.SendMessageToClient(clientID, new SpoolerMessage(MessageType.LockConfirmed, Info.serial_number, lockID.ToString()).Serialize());
                    }
                    if (controllerSelf.Idle && lockStatus == PrinterConnection.LockStatus.Locked)
                    {
                        lock (lockResetTimer)
                        {
                            if (!lockResetTimer.IsRunning)
                            {
                                lockResetTimer.Restart();
                            }

                            if (lockResetTimer.Elapsed.TotalSeconds > 30.0)
                            {
                                DoBreakLock();
                            }
                        }
                    }
                    else
                    {
                        lock (lockResetTimer)
                        {
                            if (lockResetTimer.IsRunning)
                            {
                                lockResetTimer.Stop();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().ToLower() != "unable to write program memory.  the serial port is not open.")
                {
                    ErrorLogger.LogException("PrinterConnection Exception", ex);
                }

                Shutdown();
                return(false);
            }
            return(true);
        }