예제 #1
0
        public bool LoadFile(string filename)
        {
            bool ok = true, isComment;

            FileInfo file = new FileInfo(filename);

            StreamReader sr = file.OpenText();

            string block = sr.ReadLine();

            AddBlock(filename, Action.New);

            while (block != null)
            {
                try
                {
                    block = block.Trim();
                    if (Parser.ParseBlock(ref block, false, out isComment))
                    {
                        gcode.Rows.Add(new object[] { LineNumber++, block, block.Length + 1, true, isComment, Parser.ProgramEnd, "", false });
                        while (commands.Count > 0)
                        {
                            block = commands.Dequeue();
                            gcode.Rows.Add(new object[] { LineNumber++, block, block.Length + 1, true, false, false, "", false });
                        }
                    }
                    block = sr.ReadLine();
                }
                catch (Exception e)
                {
                    if ((ok = MessageBox.Show(string.Format(LibStrings.FindResource("LoadError").Replace("\\n", "\r"), e.Message, LineNumber, block), "ioSender", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
                    {
                        block = sr.ReadLine();
                    }
                    else
                    {
                        block = null;
                    }
                }
            }

            sr.Close();

            if (ok)
            {
                AddBlock("", Action.End);
            }
            else
            {
                CloseFile();
            }

            return(ok);
        }
예제 #2
0
        public SerialStream(string PortParams, int ResetDelay, Dispatcher dispatcher)
        {
            Comms.com  = this;
            Dispatcher = dispatcher;
            Reply      = string.Empty;

            if (PortParams.IndexOf(":") < 0)
            {
                PortParams += ":115200,N,8,1";
            }

            string[] parameter = PortParams.Substring(PortParams.IndexOf(":") + 1).Split(',');

            if (parameter.Count() < 4)
            {
                MessageBox.Show(string.Format(LibStrings.FindResource("SerialPortError"), PortParams), "ioSender");
                System.Environment.Exit(2);
            }

            serialPort          = new SerialPort();
            serialPort.PortName = PortParams.Substring(0, PortParams.IndexOf(":"));
            serialPort.BaudRate = int.Parse(parameter[0]);
            serialPort.Parity   = ParseParity(parameter[1]);
            serialPort.DataBits = int.Parse(parameter[2]);
            serialPort.StopBits = int.Parse(parameter[3]) == 1 ? StopBits.One : StopBits.Two;
            serialPort.ReceivedBytesThreshold = 1;
            serialPort.ReadTimeout            = 50;
            serialPort.ReadBufferSize         = Comms.RXBUFFERSIZE;
            serialPort.WriteBufferSize        = Comms.TXBUFFERSIZE;

            if (parameter.Count() > 4)
            {
                switch (parameter[4])
                {
                case "P": // Cannot be used With ESP32!
                    serialPort.Handshake = Handshake.RequestToSend;
                    break;

                case "X":
                    serialPort.Handshake = Handshake.XOnXOff;
                    break;
                }
            }

            try
            {
                serialPort.Open();
            }
            catch
            {
            }

            if (serialPort.IsOpen)
            {
                serialPort.DtrEnable = true;

                Comms.ResetMode ResetMode = Comms.ResetMode.None;

                PurgeQueue();
                serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);

                if (parameter.Count() > 5)
                {
                    Enum.TryParse(parameter[5], true, out ResetMode);
                }

                switch (ResetMode)
                {
                case Comms.ResetMode.RTS:
                    /* For resetting ESP32 */
                    serialPort.RtsEnable = true;
                    System.Threading.Thread.Sleep(5);
                    serialPort.RtsEnable = false;
                    if (ResetDelay > 0)
                    {
                        System.Threading.Thread.Sleep(ResetDelay);
                    }
                    break;

                case Comms.ResetMode.DTR:
                    /* For resetting Arduino */
                    serialPort.DtrEnable = false;
                    System.Threading.Thread.Sleep(5);
                    serialPort.DtrEnable = true;
                    if (ResetDelay > 0)
                    {
                        System.Threading.Thread.Sleep(ResetDelay);
                    }
                    break;
                }

#if RESPONSELOG
                if (Resources.DebugFile != string.Empty)
                {
                    try
                    {
                        log = new StreamWriter(Resources.DebugFile);
                    }
                    catch
                    {
                        MessageBox.Show("Unable to open log file: " + Resources.DebugFile, "ioSender");
                    }
                }
#endif
            }
        }