private void StoreHistory(GCode gcode)
 {
     history.AddLast(gcode);
     log(gcode.getAscii(true, true), false, 0);
     if (history.Count > 40)
     {
         history.RemoveFirst();
     }
 }
 private void StoreHistory(GCode gcode)
 {
     history.AddLast(gcode);
     log(gcode.getAscii(true, true), false, 0);
     if (history.Count > 40)
         history.RemoveFirst();
 }
示例#3
0
 private void writeString(BinaryWriter file, string code, bool binary)
 {
     GCode gc = new GCode();
     gc.Parse(code);
     if (gc.hostCommand) return;
     if (binary)
     {
         if (gc.hasCode)
         {
             byte[] data = gc.getBinary(1);
             file.Write(data);
         }
     }
     else
     {
         System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
         string cmd = gc.getAscii(false, false);
         if (cmd.Length > 0)
             file.Write(enc.GetBytes(cmd + "\n"));
     }
 }
示例#4
0
 private void writeArray(BinaryWriter file, List<GCodeShort> list, bool binary)
 {
     foreach (GCodeShort code in list)
     {
         GCode gc = new GCode();
         gc.Parse(code.text);
         if (gc.hostCommand) continue;
         if (binary)
         {
             if (gc.hasCode)
             {
                 byte[] data = gc.getBinary(1);
                 file.Write(data);
             }
         }
         else
         {
             System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
             string cmd = gc.getAscii(false, false);
             if (cmd.Length > 0)
                 file.Write(enc.GetBytes(cmd + "\n"));
         }
     }
 }
        public void TrySendNextLine()
        {
            string logtext       = null;
            int    loglevel      = 0;
            float  logprogress   = -1;
            string printeraction = null;
            GCode  historygc     = null;

            try
            {
                lock (nextlineLock)
                {
                    if (pingpong && !readyForNextSend)
                    {
                        return;
                    }
                    if (serial == null)
                    {
                        return;         // Not ready yet
                    }
                    if (!serial.IsOpen) // someone unplugged the cord?
                    {
                        close();
                        return;
                    }
                    GCode gc = null;
                    try
                    {
                        // first resolve old communication problems
                        if (resendNode != null)
                        {
                            gc = resendNode.Value;
                            if (binaryVersion == 0)
                            {
                                string cmd = gc.getAscii(true, true);
                                if (!pingpong && receivedCount() + cmd.Length + 2 > receiveCacheSize)
                                {
                                    return;                                                                   // printer cache full
                                }
                                if (pingpong)
                                {
                                    readyForNextSend = false;
                                }
                                else
                                {
                                    lock (nackLines) { nackLines.AddLast(cmd.Length + 2); }
                                }
                                serial.WriteLine(cmd);
                                bytesSend += cmd.Length + 2;
                            }
                            else
                            {
                                byte[] cmd = gc.getBinary(binaryVersion);
                                if (!pingpong && receivedCount() + cmd.Length > receiveCacheSize)
                                {
                                    return;                                                               // printer cache full
                                }
                                if (pingpong)
                                {
                                    readyForNextSend = false;
                                }
                                else
                                {
                                    lock (nackLines) { nackLines.AddLast(cmd.Length); }
                                }
                                serial.Write(cmd, 0, cmd.Length);
                                bytesSend += cmd.Length;
                            }
                            linesSend++;
                            lastCommandSend = DateTime.Now.Ticks;
                            resendNode      = resendNode.Next;
                            logtext         = "Resend: " + gc.getAscii(true, true);
                            //  if (resendNode == null) readyForNextSend = true;
                            //readyForNextSend = false;
                            return;
                        }
                        if (resendError > 0)
                        {
                            resendError--;                  // Drop error counter
                        }
                        // then check for manual commands
                        if (injectCommands.Count > 0)
                        {
                            lock (history)
                            {
                                gc   = injectCommands.First.Value;
                                gc.N = ++lastline;
                                if (binaryVersion == 0)
                                {
                                    string cmd = gc.getAscii(true, true);
                                    if (!pingpong && receivedCount() + cmd.Length + 2 > receiveCacheSize)
                                    {
                                        --lastline; return;
                                    }                                                                                             // printer cache full
                                    if (pingpong)
                                    {
                                        readyForNextSend = false;
                                    }
                                    else
                                    {
                                        lock (nackLines) { nackLines.AddLast(cmd.Length); }
                                    }
                                    serial.WriteLine(cmd);
                                    bytesSend += cmd.Length + 2;
                                }
                                else
                                {
                                    byte[] cmd = gc.getBinary(binaryVersion);
                                    if (!pingpong && receivedCount() + cmd.Length > receiveCacheSize)
                                    {
                                        --lastline; return;
                                    }                                                                                         // printer cache full
                                    if (pingpong)
                                    {
                                        readyForNextSend = false;
                                    }
                                    else
                                    {
                                        lock (nackLines) { nackLines.AddLast(cmd.Length); }
                                    }
                                    serial.Write(cmd, 0, cmd.Length);
                                    bytesSend += cmd.Length;
                                }
                                injectCommands.RemoveFirst();
                            }
                            linesSend++;
                            lastCommandSend = DateTime.Now.Ticks;
                            historygc       = gc;
                            analyzer.Analyze(gc);
                            if (job.dataComplete == false)
                            {
                                if (injectCommands.Count == 0)
                                {
                                    printeraction = "Idle";
                                }
                                else
                                {
                                    printeraction = injectCommands.Count.ToString() + " commands waiting";
                                }
                            }
                            return;
                        }
                        // do we have a printing job?
                        if (job.dataComplete)
                        {
                            lock (history)
                            {
                                gc   = job.PeekData();
                                gc.N = ++lastline;
                                if (binaryVersion == 0)
                                {
                                    string cmd = gc.getAscii(true, true);
                                    if (!pingpong && receivedCount() + cmd.Length + 2 > receiveCacheSize)
                                    {
                                        --lastline; return;
                                    }                                                                                             // printer cache full
                                    if (pingpong)
                                    {
                                        readyForNextSend = false;
                                    }
                                    else
                                    {
                                        lock (nackLines) { nackLines.AddLast(cmd.Length + 2); }
                                    }
                                    serial.WriteLine(cmd);
                                    bytesSend += cmd.Length + 2;
                                }
                                else
                                {
                                    byte[] cmd = gc.getBinary(binaryVersion);
                                    if (!pingpong && receivedCount() + cmd.Length > receiveCacheSize)
                                    {
                                        --lastline; return;
                                    }                                                                                         // printer cache full
                                    if (pingpong)
                                    {
                                        readyForNextSend = false;
                                    }
                                    else
                                    {
                                        lock (nackLines) { nackLines.AddLast(cmd.Length); }
                                    }
                                    serial.Write(cmd, 0, cmd.Length);
                                    bytesSend += cmd.Length;
                                }
                                historygc = gc;
                                job.PopData();
                            }
                            linesSend++;
                            lastCommandSend = DateTime.Now.Ticks;
                            analyzer.Analyze(gc);
                            printeraction = "Printing...ETA " + job.ETA;
                            logprogress   = job.PercentDone;
                        }
                    }
                    catch (InvalidOperationException ex)
                    {
                        logtext  = "Error sending data:" + ex;
                        loglevel = 2;
                    }
                }
            }
            finally
            {
                // need to extract log/event calls because they cause deadlocks inside
                // the lock statement.
                if (historygc != null)
                {
                    StoreHistory(historygc);
                }
                if (logtext != null)
                {
                    log(logtext, false, loglevel);
                }
                if (printeraction != null)
                {
                    firePrinterAction(printeraction);
                }
                if (logprogress >= 0 && eventJobProgress != null)
                {
                    Main.main.Invoke(eventJobProgress, job.PercentDone);
                }
            }
        }