示例#1
0
        //TODO: getMsgLenFromType: check test runs for this function
        /// <summary>
        /// Returns the expected byte message length for a specific message type.
        /// </summary>
        /// <param name="msgType">Message type to find length of.</param>
        /// <returns>The expected byte length of the passed in message type.</returns>
        public static int getMsgLenFromType(CNCRMSG_TYPE msgType)
        {
            switch (msgType)
            {
            case CNCRMSG_TYPE.CMD_ACKNOWLEDGE:
                return(CNCRConstants.MSG_LEN_CMD_ACK);

            case CNCRMSG_TYPE.E_STOP:
                return(CNCRConstants.MSG_LEN_ESTOP);

            case CNCRMSG_TYPE.MOVE:
                return(CNCRConstants.MSG_LEN_MOVE);

            case CNCRMSG_TYPE.PING:
                return(CNCRConstants.MSG_LEN_PING);

            case CNCRMSG_TYPE.REQUEST_COMMAND:
                return(CNCRConstants.MSG_LEN_RQST_COMM);

            case CNCRMSG_TYPE.SET_SPEED:
                return(CNCRConstants.MSG_LEN_SETSPD);

            case CNCRMSG_TYPE.START_QUEUE:
                return(CNCRConstants.MSG_LEN_STARTQ);

            case CNCRMSG_TYPE.TOOL_CMD:
                return(CNCRConstants.MSG_TOOL_CMD);

            default:
                throw new ArgumentException("msgType of "
                                            + msgType.ToString() + " has no defined length.",
                                            "msgType");
            }
        }
示例#2
0
        public void getMsgLenFromTypeTest()
        {
            CNCRMSG_TYPE msgType  = CNCRMSG_TYPE.MOVE;
            int          expected = 11;
            int          actual;

            actual = CNCRTools.getMsgLenFromType(msgType);
            Assert.AreEqual(expected, actual);
        }
示例#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);
        }
示例#4
0
        private CNCRMSG_TYPE getCurType()
        {
            CNCRMSG_TYPE result       = CNCRMSG_TYPE.zMAX_TYPE;
            bool         threadLocked = _curTypeLock.WaitOne(100);

            if (threadLocked)
            {
                result = _curType;
                _curTypeLock.Release();
            }
            else
            {
                throw new Exception("Failed to lock thread");
            }
            return(result);
        }
示例#5
0
 //TODO: getMsgLenFromType: check test runs for this function
 /// <summary>
 /// Returns the expected byte message length for a specific message type.
 /// </summary>
 /// <param name="msgType">Message type to find length of.</param>
 /// <returns>The expected byte length of the passed in message type.</returns>
 public static int getMsgLenFromType(CNCRMSG_TYPE msgType)
 {
     switch (msgType)
     {
         case CNCRMSG_TYPE.CMD_ACKNOWLEDGE:
             return CNCRConstants.MSG_LEN_CMD_ACK;
         case CNCRMSG_TYPE.E_STOP:
             return CNCRConstants.MSG_LEN_ESTOP;
         case CNCRMSG_TYPE.MOVE:
             return CNCRConstants.MSG_LEN_MOVE;
         case CNCRMSG_TYPE.PING:
             return CNCRConstants.MSG_LEN_PING;
         case CNCRMSG_TYPE.REQUEST_COMMAND:
             return CNCRConstants.MSG_LEN_RQST_COMM;
         case CNCRMSG_TYPE.SET_SPEED:
             return CNCRConstants.MSG_LEN_SETSPD;
         case CNCRMSG_TYPE.START_QUEUE:
             return CNCRConstants.MSG_LEN_STARTQ;
         case CNCRMSG_TYPE.TOOL_CMD:
             return CNCRConstants.MSG_TOOL_CMD;
         default:
             throw new ArgumentException("msgType of "
                 + msgType.ToString() + " has no defined length.",
                 "msgType");
     }
 }
示例#6
0
 public CNCRMessage(CNCRMSG_TYPE msgType)
     : this(msgType, CNCRMSG_PRIORITY.STANDARD)
 {
 }
示例#7
0
 public CNCRMessage(CNCRMSG_TYPE msgType, CNCRMSG_PRIORITY priority)
 {
     _msgType = msgType;
     _msgTypeByte = Convert.ToByte(Convert.ToByte(_msgType) << 4);
     _msgID = msg_counter++;
 }
示例#8
0
 public CNCRMessage(CNCRMSG_TYPE msgType)
     : this(msgType, CNCRMSG_PRIORITY.STANDARD)
 {
 }
示例#9
0
 public CNCRMessage(CNCRMSG_TYPE msgType, CNCRMSG_PRIORITY priority)
 {
     _msgType     = msgType;
     _msgTypeByte = Convert.ToByte(Convert.ToByte(_msgType) << 4);
     _msgID       = msg_counter++;
 }
示例#10
0
 private void setCurType(CNCRMSG_TYPE curType)
 {
     _curTypeLock.WaitOne();
     _curType = curType;
     _curTypeLock.Release();
 }
示例#11
0
        public void handleData(byte[] commBuffer)
        {
            // Are we currently in the middle of a type?
            if (_commBufferQueue.Count == 0)
            {
                // No, so grab the type in the next byte.
                setCurType((CNCRMSG_TYPE)Enum.ToObject(typeof(CNCRMSG_TYPE), (commBuffer[0] & 0xF0) >> 4));
            }

            // Get the current type, if it is not between MIN and MAX_TYPE
            CNCRMSG_TYPE currentType = getCurType();

            if (CNCRMSG_TYPE.zMIN_TYPE < currentType && currentType < CNCRMSG_TYPE.zMAX_TYPE)
            {
                // Drop all incoming bytes into the queue
                for (int i = 0; i < commBuffer.Length; i++)
                {
                    if (CNCRTools.validateParityBit(commBuffer[i]))
                    {
                        _commBufferQueue.Enqueue(commBuffer[i]);
                    }
                    else
                    {
                        // Bad Parity bit, Discard the data and log an error.
                        handleError("Invalid Parity Bit.");
                        return;
                    }
                }

                // Check how long of a message we are expecting
                int expectedLength = CNCRTools.getMsgLenFromType(getCurType());
                // Uh, Oh, what about expectedLength = 0, AKA, bad type?
                // - At this point, curType should be validated and curType
                //   should not be unknown, throw an error in getMsgLenFromType.
                if (expectedLength > 0)
                {
                    // Process the current Queue
                    while (_commBufferQueue.Count >= expectedLength)
                    {
                        // We have enough bytes, lets get the message for those bytes.
                        byte[] msgBytes = new byte[expectedLength];
                        for (int i = 0; i < msgBytes.Length; i++)
                        {
                            msgBytes[i] = _commBufferQueue.Dequeue();
                        }

                        if (CNCRTools.validateParityByte(msgBytes))
                        {
                            DisplayData("- Valid Parity\n");
                            CNCRMessage CommMsg = CNCRTools.getMsgFromBytes(msgBytes);
                            DisplayData("- Type: " + CommMsg.ToString() + "\n");
                            Thread runActOnMessage = new Thread(new ParameterizedThreadStart(actOnMessage));
                            runActOnMessage.Start(CommMsg);
                        }
                        else
                        {
                            handleError("Invalid Parity Byte.");
                            return;
                        }
                    }
                }
                else
                {
                    handleError("Expected length was 0 or less.");
                    return;
                }
            }
            else
            {
                // Bad type: log an error, discard bytes.
                handleError("Invalid message type.");
                return;
            }
        }
示例#12
0
 private void setCurType(CNCRMSG_TYPE curType)
 {
     _curTypeLock.WaitOne();
     _curType = curType;
     _curTypeLock.Release();
 }