Exemplo n.º 1
0
        public byte[] getDataToSend()
        {
            byte[] bufferToSend = null;

            if (RequestState == eRequestState.eSENDING_NAK)
            {
                bufferToSend    = new byte[1];
                bufferToSend[0] = NAK;
                RequestState    = eRequestState.eNAK_SENT;
            }
            else if (RequestState == eRequestState.eSENDING_ANSWER)
            {
                bufferToSend    = new byte[1 + 1 + 1 + MessageToSend.Length + 1];
                bufferToSend[0] = STX;
                bufferToSend[1] = MessageToSend.Opcode;
                bufferToSend[2] = MessageToSend.Length;

                if (MessageToSend.Length > 0)
                {
                    Buffer.BlockCopy(MessageToSend.Msg, 0, bufferToSend, 3, MessageToSend.Length);
                }

                bufferToSend[MessageToSend.Length + 3] = MessageToSend.chks();
                RequestTimer.Start();
                RequestTimer.Restart();
                RequestState = eRequestState.eANSWER_SENT;
            }
            else if (RequestState == eRequestState.eSENDING_REQUEST)
            {
                bufferToSend    = new byte[1 + 1 + 1 + MessageToSend.Length + 1];
                bufferToSend[0] = STX;
                bufferToSend[1] = MessageToSend.Opcode;
                bufferToSend[2] = MessageToSend.Length;

                if (MessageToSend.Length > 0)
                {
                    Buffer.BlockCopy(MessageToSend.Msg, 0, bufferToSend, 3, MessageToSend.Length);
                }

                bufferToSend[MessageToSend.Length + 3] = MessageToSend.chks();
                RequestTimer.Start();
                RequestTimer.Restart();
                RequestState = eRequestState.eREQUEST_SENT;
            }

            return(bufferToSend);
        }
Exemplo n.º 2
0
        public void messagePoll(int data)
        {
            switch (MessageState)
            {
            default:
            case eMessageState.eSTX:
            {
                if (data >= 0)
                {
                    if ((byte)data == STX)
                    {
                        MessageState = eMessageState.eOPCODE;
                        MessageTimer.Restart();
                    }
                    else if ((byte)data == NAK)
                    {
                        MessageState = eMessageState.eNAK_RECEIVED;
                        MessageTimer.Restart();
                    }
                }
                break;
            }

            case eMessageState.eOPCODE:
            {
                if (data >= 0)
                {
                    ReceivedMessage.Opcode = (byte)data;
                    MessageTimer.Restart();
                    MessageState = eMessageState.eLENGTH;
                }
                else if (MessageTimer.ElapsedMilliseconds > Timeout)
                {
                    MessageState = eMessageState.eSTX;
                }
                break;
            }

            case eMessageState.eLENGTH:
            {
                if (data >= 0)
                {
                    ReceivedMessage.Length = (byte)data;
                    MessageTimer.Restart();

                    if (ReceivedMessage.Length > 0)
                    {
                        MessageReceivedBytes = 0;
                        MessageState         = eMessageState.eMSG;
                    }
                    else
                    {
                        MessageState = eMessageState.eCHKS;
                    }
                }
                else if (MessageTimer.ElapsedMilliseconds > Timeout)
                {
                    MessageState = eMessageState.eSTX;
                }
                break;
            }

            case eMessageState.eMSG:
            {
                if (data >= 0)
                {
                    ReceivedMessage.Msg[MessageReceivedBytes] = (byte)data;
                    MessageReceivedBytes++;
                    MessageTimer.Restart();

                    if (MessageReceivedBytes == ReceivedMessage.Length)
                    {
                        MessageState = eMessageState.eCHKS;
                    }
                    else if (MessageReceivedBytes > ReceivedMessage.Length)
                    {
                        MessageState = eMessageState.eSTX;
                    }
                }
                else if (MessageTimer.ElapsedMilliseconds > Timeout)
                {
                    MessageState = eMessageState.eSTX;
                }

                break;
            }

            case eMessageState.eCHKS:
            {
                if (data >= 0)
                {
                    MessageTimer.Restart();

                    if ((byte)data == ReceivedMessage.chks())
                    {
                        MessageState = eMessageState.eNEW_MSG;
                    }
                    else
                    {
                        MessageState = eMessageState.eNAK_TO_SEND;
                    }
                }
                else if (MessageTimer.ElapsedMilliseconds > Timeout)
                {
                    MessageState = eMessageState.eSTX;
                }
                break;
            }

            case eMessageState.eNEW_MSG:
            case eMessageState.eNAK_RECEIVED:
            case eMessageState.eNAK_TO_SEND:
            {
                break;
            }
            }
        }