예제 #1
0
        public GCodeCompressed(GCode c)
        {
            int    p   = c.orig.IndexOf(';');
            string tmp = (p >= 0 ? c.orig.Substring(0, p) : c.orig).Trim();

            data = enc.GetBytes(tmp);
        }
예제 #2
0
        /// <summary>
        /// This method estimates the amount of seconds needed for printing a specified file
        /// </summary>
        /// <param name="path">The GCode file to estimate the printing time of</param>
        public double estimateTotalTimeForFile(string path)
        {
            double totalTime = 0;

            try
            {
                IsolatedStorageFile       isf    = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, FileMode.OpenOrCreate, isf);
                StreamReader streamReader        = new StreamReader(stream);//path);

                int linecount = 0;

                while (!streamReader.EndOfStream)
                {
                    linecount++;

                    GCode currentLine = new GCode(streamReader.ReadLine());

                    double addTime = calculateTimeForLine(currentLine);

                    //if (addTime > 0.0)
                    totalTime += addTime;
                }

                streamReader.Dispose();
                stream.Dispose();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(totalTime);
        }
예제 #3
0
        async void responseReciever_ResendRecieved(int resendLine)
        {
            currentResetLine = resendLine;

            if (resendLine > currentLineNumber)
            {
                if (!errorStateSent)
                {
                    dispatcher.BeginInvoke(() => MessageBox.Show("Please reset your printer and the app because the printer is in an error state"));
                    errorStateSent = true;
                }

                problemWithSending = true;
                return;//Ignore previous error state
            }

            string lineToResend = null;

            lock (commandsInBuffer)
            {
                if (commandsInBuffer.ContainsKey(resendLine))
                {
                    haveToResendLines      = true;           //prevents the loop from sending new lines until the ones that should be in the buffer have been resent
                    sizeOfCommandsInBuffer = 0;              //the buffer should now be empty and waiting for the lines to be resent'
                    currentLineNumber      = resendLine + 1; //We need to send all commands after this line over

                    lineToResend = commandsInBuffer[resendLine];
                }
            }

            if (lineToResend != null)
            {
                haveToResendLines      = true;           //prevents the loop from sending new lines until the ones that should be in the buffer have been resent
                sizeOfCommandsInBuffer = 0;              //the buffer should now be empty and waiting for the lines to be resent'
                currentLineNumber      = resendLine + 1; //We need to send all commands after this line over

                lineToResend = commandsInBuffer[resendLine];

                GCode encodedCommand = new GCode(lineToResend);
                encodedCommand.N = resendLine;

                try
                {
                    await bluetoothIO.SendCommand(emptyBytes);//Send clean bytes to clean the buffer

                    var text = (useRepetierprotocol) ? encodedCommand.ToString() : encodedCommand.getAscii(true, false);

                    if (useRepetierprotocol)
                    {
                        await bluetoothIO.SendCommand(encodedCommand.getBinary(2));
                    }
                    else
                    {
                        await bluetoothIO.SendCommand(text + "\n");
                    }

                    if (CommandSent != null)
                    {
                        CommandSent(text);
                    }
                }
                catch (Exception)
                {
                    dispatcher.BeginInvoke(() => MessageBox.Show("There was a problem resending the command"));
                }
            }
            else
            {
                //dispatcher.BeginInvoke(() => MessageBox.Show("There was an error resending a line and a blank one will be sent in its place"));

                GCode encodedCommand = new GCode("M119");//If we don't have the command in our list then just send this endstop checking line simply to move on to the next line
                encodedCommand.N = resendLine;

                try
                {
                    await bluetoothIO.SendCommand(emptyBytes);//Send clean bytes to clean the buffer

                    var text = (useRepetierprotocol) ? encodedCommand.ToString() : encodedCommand.getAscii(true, false);

                    if (useRepetierprotocol)
                    {
                        await bluetoothIO.SendCommand(encodedCommand.getBinary(2));
                    }
                    else
                    {
                        await bluetoothIO.SendCommand(text + "\n");
                    }

                    if (CommandSent != null)
                    {
                        CommandSent(text);
                    }
                }
                catch (Exception)
                {
                    dispatcher.BeginInvoke(() => MessageBox.Show("There was a problem resending the command"));
                }
            }
        }
예제 #4
0
        async Task <bool> sendCommandWhenSpace(string command)
        {
            if (command == null)
            {
                return(false);
            }

            lastCommand = command;

            GCode encodedCommand = new GCode(command);

            encodedCommand.N = (int)currentLineNumber;

            var text = (useRepetierprotocol) ? encodedCommand.ToString() : encodedCommand.getAscii(true, false);

            var commandBytes = encodedCommand.getBinary(2);

            if (!useRepetierprotocol)
            {
                commandBytes = Encoding.UTF8.GetBytes(text);
            }
            int commandSize = commandBytes.Length;

            if (useRepetierprotocol)
            {
                while (commandSize + sizeOfCommandsInBuffer > bufferSize && !haveToResendLines)//Check if the command will fit into the buffer
                {
                    Thread.Sleep(0);
                }
            }
            else
            {
                while (sizeOfCommandsInBuffer > 50)
                {
                    Thread.Sleep(0);
                }
            }

            if (haveToResendLines)
            {
                return(false);//Let the lines that have to be resent be sent first
            }
            try
            {
                if (useRepetierprotocol)
                {
                    await bluetoothIO.SendCommand(commandBytes);
                }
                else
                {
                    await bluetoothIO.SendCommand(text + "\n");
                }

                if (CommandSent != null)
                {
                    CommandSent(text);
                }

                //Check if the command sets the temperature then we need to refelect that
                dispatcher.BeginInvoke(() =>
                {
                    if (encodedCommand.hasS)
                    {
                        if (encodedCommand.S == 0)
                        {
                            Values.isHeating = false;
                        }
                        else
                        {
                            Values.isHeating             = true;
                            Settings.printingTemperature = (short)encodedCommand.S;
                        }
                    }
                });
            }
            catch (Exception)
            {
                dispatcher.BeginInvoke(() => MessageBox.Show("There was an error sending the command"));
                problemWithSending = true;
                return(false);
            }

            int timesTried = 0;//If we have already tied adding the command for more than 100 times then it will probably never be added

            //For some reason they are sometimes not added to the list so we try until they are

            /*while (!commandsInBuffer.ContainsKey(currentLineNumber) && timesTried < 10)
             * {
             *  try
             *  {
             *      commandsInBuffer.Add(currentLineNumber, command);
             *  }
             *  catch (Exception) { Thread.Sleep(10); timesTried++; }//Give the list time to sort out its nonsense
             * }*/
            try
            {
                lock (commandsInBuffer)
                {
                    commandsInBuffer.Add(currentLineNumber, command);
                }
            }
            catch (Exception e) { Debug.WriteLine(e); }

            timesTried = 0;

            /*while (!bytesInBuffer.ContainsKey(currentLineNumber) && timesTried < 10)
             * {
             *  try
             *  {
             *      bytesInBuffer.Add(currentLineNumber, commandSize);
             *  }
             *  catch (Exception) { Thread.Sleep(10); timesTried++; }//Give the list time to sort out its nonsense
             * }*/
            try
            {
                lock (bytesInBuffer)
                {
                    bytesInBuffer.Add(currentLineNumber, commandSize);
                }
            }
            catch (Exception e) { Debug.WriteLine(e); }

            if (timesTried < 100)
            {
                sizeOfCommandsInBuffer += commandSize;//If the bytes were added to the list then their real size can be added to the estimated buffer size
            }
            else
            {
                sizeOfCommandsInBuffer += 19;//Otherwise 19 is a very good average size
            }
            return(true);
        }
예제 #5
0
        void responseReciever_OkRecieved(int okLine)
        {
            lastOkLine = okLine;

            if (okLine == currentResetLine)
            {
                resendOtherCommands();
            }

            int timesTried = 0;

            lock (commandsInBuffer)
            {
                //TODO: make sure the line is removed completely

                if (commandsInBuffer.ContainsKey(okLine))
                {
                    //If we recieved ok for a command and the command contains coordinates then it means that the printhead should be at the position
                    //specified by the command
                    GCode command = new GCode(commandsInBuffer[okLine]);

                    if (command.hasX)
                    {
                        Values.currentPos_X = command.X;
                    }

                    if (command.hasY)
                    {
                        Values.currentPos_Y = command.Y;
                    }

                    if (command.hasZ)
                    {
                        Values.currentPos_Z = command.Z;
                    }

                    commandsInBuffer.Remove(okLine);
                }
            }

            timesTried = 0;

            lock (bytesInBuffer)
            {
                //Sometimes the ok comes in just a few milis before the command was fully added to list
                while (!bytesInBuffer.ContainsKey(okLine) && timesTried < 3)
                {
                    Thread.Sleep(1);
                    timesTried++;
                }

                //If we have tried removing the command 100 times and it still is not there then for some reason it was probably never added
                if (bytesInBuffer.ContainsKey(okLine) && timesTried < 3)
                {
                    sizeOfCommandsInBuffer -= bytesInBuffer[okLine];
                    bytesInBuffer.Remove(okLine);
                }
                else if (timesTried >= 10)
                {
                    sizeOfCommandsInBuffer -= 19;//If the command was never in the list then we assume 19 bytes were added in the buffer for it
                }
                //Recalculate the size of bytes in the buffer
                int tempSize = 0;

                try
                {
                    Dictionary <long, int> tempList = new Dictionary <long, int>(bytesInBuffer);

                    //Determine if there are any lines thatare way to old and remove them
                    foreach (long lineNum in tempList.Keys)
                    {
                        if ((currentLineNumber - lineNum) > 10)
                        {
                            bytesInBuffer.Remove(lineNum);
                        }
                    }

                    //Recalculate the amount of bytes in the buffer
                    foreach (int ding in bytesInBuffer.Values)
                    {
                        tempSize += ding;
                    }

                    sizeOfCommandsInBuffer = tempSize;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Debug.WriteLine(e.Message);
                }
            }
        }
예제 #6
0
        /// <summary>
        /// This method calculates the amount of seconds it will take to print a specified line of GCode
        /// </summary>
        /// <param name="line">The line of GCode to estimate printing time on.</param>
        public float calculateTimeForLine(GCode line)
        {
            if (line.hasG && line.G == 91)
            {
                relative = true;
                lastX    = 0;
                lastY    = 0;
                lastZ    = 0;
            }

            if (!(line.hasG && (line.G == 0 || line.G == 1)))
            {
                return(0);
            }

            float timeNeeded = 0;

            float totalDistance = 0;

            float xDistance = 0;
            float yDistance = 0;
            float zDistance = 0;

            if (line.hasX)
            {
                xDistance = line.X - lastX;
                if (!relative)
                {
                    lastX = line.X;
                }
            }

            if (line.hasY)
            {
                yDistance = line.Y - lastY;
                if (!relative)
                {
                    lastY = line.Y;
                }
            }

            if (line.hasZ)
            {
                zDistance = line.Z - lastZ;
                if (!relative)
                {
                    lastZ = line.Z;
                }
            }

            //find the total distance
            totalDistance = (float)Math.Sqrt((xDistance * xDistance) + (yDistance * yDistance) + (zDistance * zDistance));
            //totalDistance = (float)Math.Pow(xDistance * xDistance + yDistance * yDistance + zDistance * zDistance, 1 / 2);

            float feedRt = 0;

            if (line.hasF)
            {
                feedRt = line.F;
                if (line.G == 0)
                {
                    lastF0 = line.F;
                }
                else if (line.G == 1)
                {
                    lastF1 = line.F;
                }
            }
            else
            {
                if (line.G == 0)
                {
                    feedRt = lastF0;
                }
                else if (line.G == 1)
                {
                    feedRt = lastF1;
                }
            }

            float mmPerSecond = feedRt / 60.0f;//The speed of the movement in terms of mm covered per secdeond

            timeNeeded = (float)(totalDistance / mmPerSecond);

            return(timeNeeded);
        }