public void Stop() { switch (CommunicationState) { case CommunicationStates.Printing: #if CANCEL_INJECTION { //Only run cancel gcode if actively printing. string cancelGCode = ActiveSliceSettings.Instance.GetActiveValue("cancel_gcode"); WriteLineToPrinter(cancelGCode); } break; #endif { CommunicationState = CommunicationStates.Connected; if (sendGCodeToPrinterThread != null) { sendGCodeToPrinterThread.Join(); sendGCodeToPrinterThread = null; } ClearQueuedGCode(); ReleaseMotors(); TargetExtruderTemperature = 0; TargetBedTemperature = 0; } break; case CommunicationStates.Paused: { CommunicationState = CommunicationStates.Connected; if (sendGCodeToPrinterThread != null) { sendGCodeToPrinterThread.Join(); sendGCodeToPrinterThread = null; } } break; case CommunicationStates.AttemptingToConnect: CommunicationState = CommunicationStates.FailedToConnect; connectThread.Join(); CommunicationState = CommunicationStates.Disconnecting; if (readFromPrinterThread != null) { readFromPrinterThread.Join(); } if (serialPort != null) { serialPort.Close(); serialPort.Dispose(); serialPort = null; } CommunicationState = CommunicationStates.Disconnected; break; case CommunicationStates.PreparingToPrint: CommunicationState = CommunicationStates.Connected; break; } }
public void PulseRtsLow() { if (serialPort == null && this.ActivePrinter != null) { serialPort = new FrostedSerialPort(this.ActivePrinter.ComPort); serialPort.BaudRate = this.BaudRate; if (PrinterCommunication.Instance.DtrEnableOnConnect) { serialPort.DtrEnable = true; } // Set the read/write timeouts serialPort.ReadTimeout = 500; serialPort.WriteTimeout = 500; serialPort.Open(); serialPort.RtsEnable = true; serialPort.RtsEnable = false; try { Thread.Sleep(1); } catch { } serialPort.RtsEnable = true; serialPort.Close(); } }
public void Disable() { if (PrinterIsConnected) { // Make sure we send this without waiting for the printer to respond. We want to try and turn off the heaters. // It may be possible in the future to make this go into the printer queue for assured sending but it means // the program has to be smart about closing an able to wait until the printer has agreed that it shut off // the motors and heaters (a good idea ane something for the future). ForceImmediateWrites = true; ReleaseMotors(); TargetExtruderTemperature = 0; TargetBedTemperature = 0; FanSpeed = 0; ForceImmediateWrites = false; CommunicationState = CommunicationStates.Disconnecting; if (readFromPrinterThread != null) { readFromPrinterThread.Join(); } serialPort.Close(); serialPort.Dispose(); serialPort = null; CommunicationState = CommunicationStates.Disconnected; LinesToWriteQueue.Clear(); } OnEnabledChanged(null); }
void AttemptToConnect(string serialPortName, int baudRate) { connectionFailureMessage = "Unknown Reason"; if (PrinterIsConnected) { throw new Exception("You can only connect when not currently connected."); } CommunicationState = CommunicationStates.AttemptingToConnect; bool serialPortIsAvailable = SerialPortIsAvailable(serialPortName); bool serialPortIsAlreadyOpen = SerialPortAlreadyOpen(serialPortName); if (serialPortIsAvailable && !serialPortIsAlreadyOpen) { serialPort = new FrostedSerialPort(serialPortName); serialPort.BaudRate = baudRate; //serialPort.Parity = Parity.None; //serialPort.DataBits = 8; //serialPort.StopBits = StopBits.One; //serialPort.Handshake = Handshake.None; if (PrinterCommunication.Instance.DtrEnableOnConnect) { serialPort.DtrEnable = true; } // Set the read/write timeouts serialPort.ReadTimeout = 500; serialPort.WriteTimeout = 500; if (CommunicationState == CommunicationStates.AttemptingToConnect) { try { serialPort.Open(); readFromPrinterThread = new Thread(ReadFromPrinter); readFromPrinterThread.Name = "Read From Printer"; readFromPrinterThread.IsBackground = true; readFromPrinterThread.Start(); // let's check if the printer will talk to us ReadPosition(); QueueLineToPrinter("M105"); QueueLineToPrinter("M115"); } catch (System.ArgumentOutOfRangeException) { connectionFailureMessage = "Unsupported Baud Rate"; OnConnectionFailed(null); } catch (Exception e) { OnConnectionFailed(null); } } } }