public void CNCRMsgCmdAckConstructorTest1()
 {
     byte[] msgBytes = { 0x12, 0xFF, 0xED };
     CNCRMsgCmdAck target = new CNCRMsgCmdAck(msgBytes);
     Assert.AreEqual(true, target.getError());
     Assert.AreEqual(127, target.getFirmware());
 }
 public void CNCRMsgCmdAckConstructorTest_ErrBadFW()
 {
     bool isError = true;
     byte firmware = 128;
     CNCRMsgCmdAck target = new CNCRMsgCmdAck(isError, firmware);
     Assert.AreEqual(isError, target.getError());
     Assert.AreEqual(firmware, target.getFirmware());
 }
Exemple #3
0
        //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 toSerialTest()
 {
     CNCRMsgCmdAck target = new CNCRMsgCmdAck(true, 127);
     byte[] expected = { 0x12, 0xFF, 0xED };
     byte[] actual;
     actual = target.toSerial();
     for (int i = 0; i < expected.Length; i++)
     {
         Assert.AreEqual<byte>(expected[i], actual[i]);
     }
 }
 public void getFirmwareTest()
 {
     CNCRMsgCmdAck target = new CNCRMsgCmdAck(true, 127); // TODO: Initialize to an appropriate value
     byte expected = 127; // TODO: Initialize to an appropriate value
     byte actual;
     actual = target.getFirmware();
     Assert.AreEqual<byte>(expected, actual);
 }
 public void getErrorTest()
 {
     CNCRMsgCmdAck target = new CNCRMsgCmdAck(true, 127); // TODO: Initialize to an appropriate value
     bool expected = true; // TODO: Initialize to an appropriate value
     bool actual;
     actual = target.getError();
     Assert.AreEqual(expected, actual);
 }
 public void CNCRMsgCmdAckConstructorTest_valid()
 {
     bool isError = false;
     byte firmware = 0;
     CNCRMsgCmdAck target = new CNCRMsgCmdAck(isError, firmware);
     Assert.AreEqual(isError, target.getError());
     Assert.AreEqual(firmware, target.getFirmware());
 }
Exemple #8
0
        //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 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);
        }
        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);
            }
        }
Exemple #11
0
        /// <summary>
        /// Takes the passed in message and performs the nessessary actions
        /// required by that message.
        /// </summary>
        /// <param name="msg">Message to act on.</param>
        public void actOnMessage(object msgObj)
        {
            CNCRMessage msg = (CNCRMessage)msgObj;
            if (msg == null)
                throw new ArgumentNullException();

            switch (msg.getMessageType())
            {
                case CNCRMSG_TYPE.CMD_ACKNOWLEDGE:
                    if (((CNCRMsgCmdAck)msg).getError())
                    {
                        // if error, resend last message
                        CNCRMessage lastMsg = getLastMessage();
                        lastMsg.setPriority(CNCRMSG_PRIORITY.HIGH);
                        commPriorityQueueEnqueue(lastMsg);
                    }
                    else
                    {
                        // if not error, clear "Waiting for Ack"
                        setWaitingOnAck(false);
                    }
                    launchProcessQueues();
                    break;
                case CNCRMSG_TYPE.E_STOP:
                    // Send Ack.
                    CNCRMessage ack = new CNCRMsgCmdAck(false, 0);
                    ack.setPriority(CNCRMSG_PRIORITY.MEDIUM); // TODO: what about the E-stop Ack.
                    commPriorityQueueEnqueue(ack);

                    // Set the "Stop Sending Messages variable"
                    setEStopActive(true);
                    break;
                case CNCRMSG_TYPE.REQUEST_COMMAND:
                    // Send Ack.
                    CNCRMessage ack2 = new CNCRMsgCmdAck(false, 0);
                    ack2.setPriority(CNCRMSG_PRIORITY.MEDIUM);
                    commPriorityQueueEnqueue(ack2);

                    // Set the "Send Commands" variable to the # of messages.
                    CNCRMsgRequestCommands msgRC = (CNCRMsgRequestCommands)msg;
                    setNumCmdsToSend(msgRC.getCommandCount() + getNumCmdsToSend());

                    // If it is not already started, kick off the "SendMessages" method.
                    launchProcessQueues();
                    break;
                case CNCRMSG_TYPE.MOVE:
                case CNCRMSG_TYPE.PING:
                case CNCRMSG_TYPE.SET_SPEED:
                case CNCRMSG_TYPE.START_QUEUE:
                case CNCRMSG_TYPE.TOOL_CMD:
                    // We should not be receiving any of these messages.
                    DisplayData("Received a valid message that should not have " +
                        "been sent by the router. " + msg.ToString() + "\n");
                    break;
                default:
                    throw new ArgumentException("CNCRMessage has an invalid type");
            }
        }
Exemple #12
0
        /// <summary>
        /// Takes the passed in message and performs the nessessary actions
        /// required by that message.
        /// </summary>
        /// <param name="msg">Message to act on.</param>
        public void actOnMessage(object msgObj)
        {
            CNCRMessage msg = (CNCRMessage)msgObj;

            if (msg == null)
            {
                throw new ArgumentNullException();
            }

            switch (msg.getMessageType())
            {
            case CNCRMSG_TYPE.CMD_ACKNOWLEDGE:
                if (((CNCRMsgCmdAck)msg).getError())
                {
                    // if error, resend last message
                    CNCRMessage lastMsg = getLastMessage();
                    lastMsg.setPriority(CNCRMSG_PRIORITY.HIGH);
                    commPriorityQueueEnqueue(lastMsg);
                }
                else
                {
                    // if not error, clear "Waiting for Ack"
                    setWaitingOnAck(false);
                }
                launchProcessQueues();
                break;

            case CNCRMSG_TYPE.E_STOP:
                // Send Ack.
                CNCRMessage ack = new CNCRMsgCmdAck(false, 0);
                ack.setPriority(CNCRMSG_PRIORITY.MEDIUM);     // TODO: what about the E-stop Ack.
                commPriorityQueueEnqueue(ack);

                // Set the "Stop Sending Messages variable"
                setEStopActive(true);
                break;

            case CNCRMSG_TYPE.REQUEST_COMMAND:
                // Send Ack.
                CNCRMessage ack2 = new CNCRMsgCmdAck(false, 0);
                ack2.setPriority(CNCRMSG_PRIORITY.MEDIUM);
                commPriorityQueueEnqueue(ack2);

                // Set the "Send Commands" variable to the # of messages.
                CNCRMsgRequestCommands msgRC = (CNCRMsgRequestCommands)msg;
                setNumCmdsToSend(msgRC.getCommandCount() + getNumCmdsToSend());

                // If it is not already started, kick off the "SendMessages" method.
                launchProcessQueues();
                break;

            case CNCRMSG_TYPE.MOVE:
            case CNCRMSG_TYPE.PING:
            case CNCRMSG_TYPE.SET_SPEED:
            case CNCRMSG_TYPE.START_QUEUE:
            case CNCRMSG_TYPE.TOOL_CMD:
                // We should not be receiving any of these messages.
                DisplayData("Received a valid message that should not have " +
                            "been sent by the router. " + msg.ToString() + "\n");
                break;

            default:
                throw new ArgumentException("CNCRMessage has an invalid type");
            }
        }