//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 CNCRMsgSetSpeedConstructorTest() { byte[] msgBytes = { 0x5C, 0x00, 0x2D, 0x05, 0x74 }; CNCRMsgSetSpeed target = new CNCRMsgSetSpeed(msgBytes); Assert.AreEqual(true, target.isX()); Assert.AreEqual(true, target.isY()); Assert.AreEqual(false, target.isZ()); Assert.AreEqual(300, target.getSpeed()); Assert.AreEqual(CNCRMSG_TYPE.SET_SPEED, target.getMessageType()); Assert.AreEqual(0x50, target.getMsgTypeByte()); }
public void CNCRMsgSetSpeedConstructorTest2() { bool X = false; bool Y = false; bool Z = false; ushort speed = 0; CNCRMsgSetSpeed target = new CNCRMsgSetSpeed(X, Y, Z, speed); Assert.AreEqual(X, target.isX()); Assert.AreEqual(Y, target.isY()); Assert.AreEqual(Z, target.isZ()); Assert.AreEqual(speed, target.getSpeed()); Assert.AreEqual(CNCRMSG_TYPE.SET_SPEED, target.getMessageType()); Assert.AreEqual(0x50, target.getMsgTypeByte()); }
//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 getMsgFromBytesTest() { byte[] msgBytes = {0x5C, 0x00, 0x2D, 0x05, 0x74}; CNCRMessage expected = new CNCRMsgSetSpeed(msgBytes); CNCRMessage actual; actual = CNCRTools.getMsgFromBytes(msgBytes); Assert.AreEqual<CNCRMSG_TYPE>(expected.getMessageType(), actual.getMessageType()); Assert.AreEqual(((CNCRMsgSetSpeed)expected).isX(), ((CNCRMsgSetSpeed)actual).isX()); Assert.AreEqual(((CNCRMsgSetSpeed)expected).isY(), ((CNCRMsgSetSpeed)actual).isY()); Assert.AreEqual(((CNCRMsgSetSpeed)expected).isZ(), ((CNCRMsgSetSpeed)actual).isZ()); Assert.AreEqual(((CNCRMsgSetSpeed)expected).getSpeed(), ((CNCRMsgSetSpeed)actual).getSpeed()); }
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 getSpeedTest_0() { CNCRMsgSetSpeed target = new CNCRMsgSetSpeed(true, true, true, 0); ushort expected = 0; ushort actual; actual = target.getSpeed(); Assert.AreEqual(expected, actual); }
public void toSerialTest_Z() { CNCRMsgSetSpeed target = new CNCRMsgSetSpeed(false, false, true, 300); byte[] expected = { 0x53, 0x00, 0x2D, 0x05, 0x7B }; 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]); } }
public void isZTest_True() { CNCRMsgSetSpeed target = new CNCRMsgSetSpeed(true, true, true, 300); bool expected = true; bool actual; actual = target.isZ(); Assert.AreEqual(expected, actual); }
public void isYTest_False() { CNCRMsgSetSpeed target = new CNCRMsgSetSpeed(true, false, true, 300); bool expected = false; bool actual; actual = target.isY(); Assert.AreEqual(expected, actual); }
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); } }