Esempio n. 1
0
        // Handle incoming commands from the serial link
        void Process()
        {
            // Todo - receive incoming commands from the serial link and act on those commands by calling the low-level hardwarwe APIs, etc.


            byte ACK  = 0xA5;
            byte NACK = 0xFF;

            float xVoltage     = 0;
            float yVoltage     = 0;
            float oldZLocation = 0;

            while (!fDone)
            {
                var receivedHeader = new byte[4];
                var ACKorNACK      = new byte[1];
                ReceiveHeaderAndSend(receivedHeader);
                var paramData = new byte[receivedHeader[1]];
                printer.WaitMicroseconds(3000);
                while (ACKorNACK[0] != ACK && ACKorNACK[0] != NACK)
                {
                    printer.ReadSerialFromHost(ACKorNACK, 1);
                }
                if (ACKorNACK[0] == ACK)
                {
                    printer.WaitMicroseconds(3000);
                    //var readParamByte = ReadParamBytes(receivedHeader, paramData);
                    //var paramBytes = new byte[header[1]];
                    //var paramBytesRead = printer.ReadSerialFromHost(paramData, receivedHeader[1]);
                    //var responseByte = new byte[successBytes.Length];
                    if (printer.ReadSerialFromHost(paramData, receivedHeader[1]) != receivedHeader[1] /*ByteArraysEquals(readParamByte, timeoutBytes)*/)
                    {
                        printer.WriteSerialToHost(timeoutBytes, responseBytesLen);
                    }
                    else
                    {
                        //Console.WriteLine("Firmware: " + BitConverter.ToString(receivedHeader) + "|-|" + BitConverter.ToString(paramData));
                        //responseByte = successBytes;
                        var calculatedChecksum = CalculateChecksum(receivedHeader, paramData);
                        if (receivedHeader[2] == calculatedChecksum[0] && receivedHeader[3] == calculatedChecksum[1])
                        {
                            printer.WriteSerialToHost(successBytes, successBytes.Length);
                            if (receivedHeader[0] == 0)
                            {
                                var distance = BitConverter.ToSingle(paramData, 0) - oldZLocation;
                                oldZLocation = BitConverter.ToSingle(paramData, 0);
                                MoveZrail(distance, PrinterControl.StepperDir.STEP_UP);
                            }
                            else if (receivedHeader[0] == 1)
                            {
                                //Console.WriteLine("Execute setLaser with data: " + BitConverter.ToBoolean(paramData, 0));
                                printer.SetLaser(BitConverter.ToBoolean(paramData, 0));
                            }
                            else if (receivedHeader[0] == 2)
                            {
                                xVoltage = BitConverter.ToSingle(paramData, 0) / 20;
                                yVoltage = BitConverter.ToSingle(paramData, 4) / 20;
                                if ((xVoltage > -2.25 && xVoltage < 2.25) && (yVoltage > -2.25 && yVoltage < 2.25))
                                {
                                    printer.MoveGalvos(xVoltage, yVoltage);
                                    //Console.WriteLine("Execute moveGalvos with data: xVoltage: " + xVoltage + " yVoltage: " + yVoltage);
                                }
                            }
                            //printer.WriteSerialToHost(successBytes, successBytes.Length);
                        }
                        else
                        {
                            printer.WriteSerialToHost(checksumBytes, responseBytesLen);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void executeCommand(byte command, byte[] param)
        {
            try
            {
                switch (command)
                {
                case 0x00:
                    return;

                case 0x01:
                    commandsExecuted["ResetStepper"] += 1;
                    printer.ResetStepper();
                    moveStepperToTop();
                    moveStepperFromTopToBuildPlate();
                    break;

                case 0x02:
                    float direction = BitConverter.ToSingle(param, 0);
                    if (direction == 1)
                    {
                        Console.WriteLine("Step up");
                        commandsExecuted["StepStepperUp"] += 1;
                        bool result = printer.StepStepper(PrinterControl.StepperDir.STEP_UP);
                    }
                    else if (direction == 0)
                    {
                        Console.WriteLine("Step down");
                        commandsExecuted["StepStepperDown"] += 1;
                        bool result = printer.StepStepper(PrinterControl.StepperDir.STEP_DOWN);
                    }
                    break;

                case 0x03:
                    float value = BitConverter.ToSingle(param, 0);
                    if (value == 1)
                    {
                        printer.SetLaser(true);
                        Console.WriteLine("Turned on laser");
                    }
                    else if (value == 0)
                    {
                        printer.SetLaser(false);
                        Console.WriteLine("Turned laser off");
                    }
                    else
                    {
                        Console.WriteLine("Invalid laser value for on/off");
                    }
                    break;

                case 0x04:
                    commandsExecuted["MoveGalvonometer"] += 1;
                    float x = BitConverter.ToSingle(param, 0);
                    float y = BitConverter.ToSingle(param, 4);
                    printer.MoveGalvos(x, y);
                    Console.WriteLine("Moved galvo");
                    break;

                default:
                    Console.WriteLine("Bad command");
                    break;
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
            }
        }