//TODO: Should this be in CNCRMessage? //TODO: getMsgFromBytes: Generate more test cases for this function, preferable edge cases. /// <summary> /// Returns the message contained in the passed in byte array. /// </summary> /// <param name="msgBytes">Array of bytes containing a CNCRMessage</param> /// <returns>The message contained in the bytes.</returns> public static CNCRMessage getMsgFromBytes(byte[] msgBytes) { // Byte 0 should be CNCRMSG_TYPE msgType = (CNCRMSG_TYPE)Enum.ToObject(typeof(CNCRMSG_TYPE), (msgBytes[0] & 0xF0) >> 4); int msgLen = getMsgLenFromType(msgType); // Validate the message length. if (msgLen != msgBytes.Length) { throw new RankException("MsgCommandAcknowledge is " + CNCRConstants.MSG_LEN_CMD_ACK + " not " + msgBytes.Length + " bytes long."); } // Build the correct message. CNCRMessage resultMsg; switch (msgType) { case CNCRMSG_TYPE.CMD_ACKNOWLEDGE: resultMsg = new CNCRMsgCmdAck(msgBytes); break; case CNCRMSG_TYPE.E_STOP: resultMsg = new CNCRMsgEStop(); break; case CNCRMSG_TYPE.MOVE: resultMsg = new CNCRMsgMove(msgBytes); break; case CNCRMSG_TYPE.PING: resultMsg = new CNCRMsgPing(); break; case CNCRMSG_TYPE.REQUEST_COMMAND: resultMsg = new CNCRMsgRequestCommands(msgBytes); break; case CNCRMSG_TYPE.SET_SPEED: resultMsg = new CNCRMsgSetSpeed(msgBytes); break; case CNCRMSG_TYPE.START_QUEUE: resultMsg = new CNCRMsgStartQueue(msgBytes); break; case CNCRMSG_TYPE.TOOL_CMD: resultMsg = new CNCRMsgToolCmd(msgBytes); break; default: throw new FormatException("getMsgFromBytes: Unknown message type"); } return(resultMsg); }
public void CNCRMsgMoveConstructorTest1() { byte[] msgBytes = { 0x60, 0x81, 0x00, 0x00, 0x7E, 0xFF, 0x06, 0x5F, 0x5A, 0x03, 0x60 }; CNCRMsgMove target = new CNCRMsgMove(msgBytes); Assert.AreEqual <CNCRMSG_TYPE>(CNCRMSG_TYPE.MOVE, target.getMessageType()); Assert.AreEqual<byte>(0x60, target.getMsgTypeByte()); Assert.AreEqual<short>(-32768, target.getX()); Assert.AreEqual<short>(32767, target.getY()); Assert.AreEqual<short>(24155, target.getZ()); }
public void CNCRMsgMoveConstructorTest() { //-32768, 32767, 24155 short X = -32768; // TODO: Initialize to an appropriate value short Y = 32767; // TODO: Initialize to an appropriate value short Z = 24155; // TODO: Initialize to an appropriate value CNCRMsgMove target = new CNCRMsgMove(X, Y, Z); Assert.AreEqual<CNCRMSG_TYPE>(CNCRMSG_TYPE.MOVE, target.getMessageType()); Assert.AreEqual<byte>(0x60, target.getMsgTypeByte()); Assert.AreEqual<short>(-32768, target.getX()); Assert.AreEqual<short>(32767, target.getY()); Assert.AreEqual<short>(24155, target.getZ()); }
private void sendShortMove(CNCRMsgMove msg, bool isXY) { float fSpeed; // To be safe, set the speed to 30.0 mm/min UInt16 uSpeed = CNCRConstants.DEFAULT_Z_FEEDRATE; if (isXY) { if (!float.TryParse(txtXYFeedrate.Text, out fSpeed)) { fSpeed = CNCRConstants.DEFAULT_XY_FEEDRATE / 10; } } else { if (!float.TryParse(txtZFeedrate.Text, out fSpeed)) { fSpeed = CNCRConstants.DEFAULT_Z_FEEDRATE / 10; } } try { uSpeed = Convert.ToUInt16(fSpeed); } catch (Exception ex) { // eat my shorts. } //commCmd.PortName = cmbRouterPort.SelectedItem.ToString(); // Enque the "startQueue" in the priority queue. commCmd.commPriorityQueueEnqueue(new CNCRMsgStartQueue(false)); commCmd.commCommandQueueEnqueue(new CNCRMsgSetSpeed(uSpeed)); commCmd.commCommandQueueEnqueue(msg); commCmd.commCommandQueueEnqueue(new CNCRMsgStartQueue(true)); commCmd.launchProcessQueues(); }
//TODO: Should this be in CNCRMessage? //TODO: getMsgFromBytes: Generate more test cases for this function, preferable edge cases. /// <summary> /// Returns the message contained in the passed in byte array. /// </summary> /// <param name="msgBytes">Array of bytes containing a CNCRMessage</param> /// <returns>The message contained in the bytes.</returns> public static CNCRMessage getMsgFromBytes(byte[] msgBytes) { // Byte 0 should be CNCRMSG_TYPE msgType = (CNCRMSG_TYPE)Enum.ToObject(typeof(CNCRMSG_TYPE), (msgBytes[0] & 0xF0) >> 4); int msgLen = getMsgLenFromType(msgType); // Validate the message length. if (msgLen != msgBytes.Length) throw new RankException("MsgCommandAcknowledge is " + CNCRConstants.MSG_LEN_CMD_ACK + " not " + msgBytes.Length + " bytes long."); // Build the correct message. CNCRMessage resultMsg; switch (msgType) { case CNCRMSG_TYPE.CMD_ACKNOWLEDGE: resultMsg = new CNCRMsgCmdAck(msgBytes); break; case CNCRMSG_TYPE.E_STOP: resultMsg = new CNCRMsgEStop(); break; case CNCRMSG_TYPE.MOVE: resultMsg = new CNCRMsgMove(msgBytes); break; case CNCRMSG_TYPE.PING: resultMsg = new CNCRMsgPing(); break; case CNCRMSG_TYPE.REQUEST_COMMAND: resultMsg = new CNCRMsgRequestCommands(msgBytes); break; case CNCRMSG_TYPE.SET_SPEED: resultMsg = new CNCRMsgSetSpeed(msgBytes); break; case CNCRMSG_TYPE.START_QUEUE: resultMsg = new CNCRMsgStartQueue(msgBytes); break; case CNCRMSG_TYPE.TOOL_CMD: resultMsg = new CNCRMsgToolCmd(msgBytes); break; default: throw new FormatException("getMsgFromBytes: Unknown message type"); } return resultMsg; }
private void sendShortMove(CNCRMsgMove msg, bool isXY) { float fSpeed; // To be safe, set the speed to 30.0 mm/min UInt16 uSpeed = CNCRConstants.DEFAULT_Z_FEEDRATE; if (isXY) { if (!float.TryParse(txtXYFeedrate.Text, out fSpeed)) fSpeed = CNCRConstants.DEFAULT_XY_FEEDRATE / 10; } else { if (!float.TryParse(txtZFeedrate.Text, out fSpeed)) fSpeed = CNCRConstants.DEFAULT_Z_FEEDRATE / 10; } try { uSpeed = Convert.ToUInt16(fSpeed); } catch (Exception ex) { // eat my shorts. } //commCmd.PortName = cmbRouterPort.SelectedItem.ToString(); // Enque the "startQueue" in the priority queue. commCmd.commPriorityQueueEnqueue(new CNCRMsgStartQueue(false)); commCmd.commCommandQueueEnqueue(new CNCRMsgSetSpeed(uSpeed)); commCmd.commCommandQueueEnqueue(msg); commCmd.commCommandQueueEnqueue(new CNCRMsgStartQueue(true)); commCmd.launchProcessQueues(); }
private void btnSndMsg_Click(object sender, EventArgs e) { /* int discarded = 0; byte[] bytes = CNCRTools.GetBytes(txtHex.Text, out discarded); lblDbgOut.Text = ""; for (int i = 0; i < bytes.Length; i++) { lblDbgOut.Text += bytes[i].ToString() + " "; }//*/ byte[] sendBytes = {0}; CNCRMessage sendMsg = null; switch (cmbMsgs.SelectedIndex) { case 0: int discarded = 0; sendBytes = CNCRTools.GetBytes(txtHex.Text, out discarded); break; case 1: sendMsg = new CNCRMsgPing(); sendBytes = sendMsg.toSerial(); break; case 2: sendMsg = new CNCRMsgCmdAck(false, 127); sendBytes = sendMsg.toSerial(); break; case 3: sendMsg = new CNCRMsgEStop(); sendBytes = sendMsg.toSerial(); break; case 4: sendMsg = new CNCRMsgRequestCommands(128); sendBytes = sendMsg.toSerial(); break; case 5: sendMsg = new CNCRMsgStartQueue(false); sendBytes = sendMsg.toSerial(); break; case 6: sendMsg = new CNCRMsgSetSpeed(true, true, false, 40000); sendBytes = sendMsg.toSerial(); break; case 7: sendMsg = new CNCRMsgMove(Int16.MinValue, Int16.MaxValue, 0); sendBytes = sendMsg.toSerial(); break; case 8: sendMsg = new CNCRMsgToolCmd(true); sendBytes = sendMsg.toSerial(); break; } rtbTraffic.AppendText(CNCRTools.BytesToHex(sendBytes) + "\n"); if (sendMsg == null) commCmd.SendBytes(sendBytes); else commCmd.SendMsg(sendMsg); }
public void getXTest() { CNCRMsgMove target = new CNCRMsgMove(-32768, 32767, 24155); short expected = -32768; short actual; actual = target.getX(); Assert.AreEqual<short>(expected, actual); }
public void toSerialTest() { CNCRMsgMove target = new CNCRMsgMove(-32768, 32767, 24155); byte[] expected = {0x60, 0x81, 0x00, 0x00, 0x7E, 0xFF, 0x06, 0x5F, 0x5A, 0x03, 0x60}; byte[] actual; actual = target.toSerial(); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual<byte>(expected[i], actual[i], "Byte " + i); } }
private void btnSndMsg_Click(object sender, EventArgs e) { /* * int discarded = 0; * byte[] bytes = CNCRTools.GetBytes(txtHex.Text, out discarded); * lblDbgOut.Text = ""; * for (int i = 0; i < bytes.Length; i++) * { * lblDbgOut.Text += bytes[i].ToString() + " "; * }//*/ byte[] sendBytes = { 0 }; CNCRMessage sendMsg = null; switch (cmbMsgs.SelectedIndex) { case 0: int discarded = 0; sendBytes = CNCRTools.GetBytes(txtHex.Text, out discarded); break; case 1: sendMsg = new CNCRMsgPing(); sendBytes = sendMsg.toSerial(); break; case 2: sendMsg = new CNCRMsgCmdAck(false, 127); sendBytes = sendMsg.toSerial(); break; case 3: sendMsg = new CNCRMsgEStop(); sendBytes = sendMsg.toSerial(); break; case 4: sendMsg = new CNCRMsgRequestCommands(128); sendBytes = sendMsg.toSerial(); break; case 5: sendMsg = new CNCRMsgStartQueue(false); sendBytes = sendMsg.toSerial(); break; case 6: sendMsg = new CNCRMsgSetSpeed(true, true, false, 40000); sendBytes = sendMsg.toSerial(); break; case 7: sendMsg = new CNCRMsgMove(Int16.MinValue, Int16.MaxValue, 0); sendBytes = sendMsg.toSerial(); break; case 8: sendMsg = new CNCRMsgToolCmd(true); sendBytes = sendMsg.toSerial(); break; } rtbTraffic.AppendText(CNCRTools.BytesToHex(sendBytes) + "\n"); if (sendMsg == null) { commCmd.SendBytes(sendBytes); } else { commCmd.SendMsg(sendMsg); } }