public byte[] ProcessCmd(byte cmd, byte[] data) { if (cmd == (byte)Packet.Cmds.LASER) { printer.SetLaser(BitConverter.ToBoolean(data, 0)); } else if (cmd == (byte)Packet.Cmds.GALVOS) { printer.MoveGalvos((float)BitConverter.ToDouble(data, 0), (float)BitConverter.ToDouble(data, 8)); } else if (cmd == (byte)Packet.Cmds.ZCOR) // Find more efficient method { //Console.WriteLine(BitConverter.ToDouble(data,0)); int steps = (int)(400 * BitConverter.ToDouble(data, 0)); if (((float)data[0]) < 0) { zRailController.MoveZRail(PrinterControl.StepperDir.STEP_DOWN, steps); } else { zRailController.MoveZRail(PrinterControl.StepperDir.STEP_UP, steps); } } else if (cmd == (byte)Packet.Cmds.RESET) { SetBuildPlateHome(); Console.WriteLine("return from setbuildplatehome"); } return(Encoding.ASCII.GetBytes("SUCCESS")); }
String DoCommand(byte cmd, byte[] cmdParams, byte offset) { string retString = ""; switch (cmd) { case FWCommands.CMD_VERSION: retString = FIRMWARE_VERSION; break; case FWCommands.CMD_RETRACT_PLATE: RetractPlate(); retString = "SUCCESS"; break; case FWCommands.CMD_SET_Z: SetZ(GetFloatParam(cmdParams, offset)); retString = "SUCCESS"; break; case FWCommands.CMD_REMOVE_MODEL: printer.RemoveModelFromPrinter(); retString = "SUCCESS"; break; case FWCommands.CMD_MOVE_GALVOS: printer.MoveGalvos(GetFloatParam(cmdParams, offset), GetFloatParam(cmdParams, offset + 4)); retString = "SUCCESS"; break; case FWCommands.CMD_SET_LASER: if (cmdParams[offset] == 1) { printer.SetLaser(true); } else { printer.SetLaser(false); } retString = "SUCCESS"; break; default: retString = "ERROR"; break; } return(retString); }
void ProcessCommand(byte[] packet, PrinterControl printer) { // Process // TYLER's Section, Recieves byte array . /* Byte 0: Command byte * Byte 1: Length of parameter data (# of bytes) * Byte 2: Low-byte of 16-bit checksum * Byte 3: High-byte of 16-bit checksum * Byte 4-n: Parameter data wait ms, stepper, move galvos, remove model, set laser * * Note: what we need to know is which bytes corespond to controling which of the below commands. */ if (firstCommand) { printer.ResetStepper(); moveTop(printer); firstCommand = false; } printByteArray(packet, "Firmware received successfully. Now in process command."); byte command = packet[0]; byte MoveGalvos_command = 0x00; byte MoveZ_command = 0x01; byte SetLaser_command = 0x02; byte PrintDone_command = 0x03; //byte WaitMicroseconds_command = 0x00; //byte ResetStepper_command = 0x00; //byte RemoveModelFromPrinter_command = 0x00; //if (command == WaitMicroseconds_command) //WaitMicroseconds //{ // // convert from byte to long // long microsec = 0; // printer.WaitMicroseconds(microsec); //} //if (command == ResetStepper_command) //ResetStepper //{ // // void function // printer.ResetStepper(); //} if (command == MoveGalvos_command) //MoveGalvos { // convert from byte to float x and float y byte[] x_substring = new byte[4]; // should I make this 4? Array.Copy(packet, 4, x_substring, 0, 4); byte[] y_substring = new byte[4]; // should I make this 4? Array.Copy(packet, 8, y_substring, 0, 4); float x = BitConverter.ToSingle(x_substring, 0); float y = BitConverter.ToSingle(y_substring, 0); float x_voltage = (float)(x * (2.5 / 100)); // find a better way to do these magic numbers float y_voltage = (float)(y * (2.5 / 100)); printer.MoveGalvos(x_voltage, y_voltage); // sends voltages to MoveGalvos(); } //else if (command == RemoveModelFromPrinter_command) //RemoveModelFromPrinter //{ // // void function // printer.RemoveModelFromPrinter(); //} else if (command == SetLaser_command) //SetLaser { // convert from byte to bool bool set = BitConverter.ToBoolean(packet, 4); printer.SetLaser(set); } else if (command == MoveZ_command) { // convert from byte to float float z_frombottom = BitConverter.ToSingle(packet, 4); // converting from byte[] starting at 4 to float movementWithSpeed(printer, calculateDirection(printer, z_frombottom), CalculateDistance(printer, z_frombottom)); // zrailcontroller } else if (command == PrintDone_command) { fDone = true; moveTop(printer); } }
// 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); } } } } }
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); } }