Exemplo n.º 1
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"));
                }
            }
        }
Exemplo n.º 2
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);
        }