private void Rx_TakCleanup()
 {
     // everything operates on _xmsg
     Array.Copy(_xbf, _xmsg.RplyMsg, _xmsg.RplyMsg.Length);
     _xmsg.Stat = _xbf_stat;
     RxTakState = TakeDataState.Done;
 }
 private void Rx_TakGroupExtendedContinue()
 {
     if (Rx1Byte(ref _xbf[_xbf_i++]))
     {
         if (--_xbf_cntdwn == 0)
         {
             // if we've gotten all bytes, verify checksum
             Rx_TestCS();
             if (_xbf_i <= _xmsg.RplyMsg.Length)
             {
                 // if the current index is the expected reply length,
                 // then we're done; group complete
                 RxTakState = TakeDataState.Cleanup;
             }
             else
             {
                 // it's a group message reply; take more bytes
                 // reverse what checksum test did
                 _xbf_stat  = '?';
                 RxTakState = TakeDataState.GroupExtended;
             }
         }
         else
         {
             // timed out; time to bail.
             _xbf_stat  = 'T';
             RxTakState = TakeDataState.Cleanup;
         }
     }
 }
        private void Rx_TakUnsolCleanup()
        {
            // unsolicited goes around a few things and restarts
            // it has to build a new ECM message and output it
            // then back to servicing the original msg
            ECMsg m = new ECMsg(null);

            m.SetUp('U', 0, _xbf_i);  // indexs + 1 'cause there's 0x4F precursor...
            Array.Copy(_xbf, m.RplyMsg, m.RplyMsg.Length);
            m.Stat = _xbf_stat;
            _out_q.Enqueue(m);
            RxTakState = TakeDataState.Initial;
        }
 private void Rx_TakGroupExtended()
 {
     if (Rx1Byte(ref _xbf[_xbf_i++]))
     {
         // take the count for the next reply in the group
         // note the index doesn't change.
         _xbf_i_lenpos = _xbf_i - 1;
         _xbf_cntdwn   = _xbf[_xbf_i_lenpos] + 1; // checksum too
         RxTakState    = TakeDataState.GroupExtendedContinue;
     }
     else
     {
         // timed out; time to bail.
         _xbf_stat  = 'T';
         RxTakState = TakeDataState.Cleanup;
     }
 }
        /// <summary>
        /// Synchronously recvs an _xmsg; will pack up an unsolicited message.
        /// </summary>
        private void Rx()
        {
            RxTakState = TakeDataState.Initial;

            while (RxTakState != TakeDataState.Done)
            {
                switch (RxTakState)
                {
                case TakeDataState.Initial:
                    Rx_TakInitial();
                    break;

                case TakeDataState.Unsol:
                    Rx_TakUnsol();
                    break;

                case TakeDataState.Brief:
                    Rx_TakBrief();
                    break;

                case TakeDataState.Typical:
                    Rx_TakTypical();
                    break;

                case TakeDataState.GroupExtended:
                    Rx_TakGroupExtended();
                    break;

                case TakeDataState.GroupExtendedContinue:
                    Rx_TakGroupExtendedContinue();
                    break;

                case TakeDataState.UnsolCleanup:
                    Rx_TakUnsolCleanup();
                    break;

                case TakeDataState.Cleanup:
                    Rx_TakCleanup();
                    break;

                default:
                    ;
                    break;
                }
            }
        }
 private void Rx_TakUnsol()
 {
     if (Rx1Byte(ref _xbf[_xbf_i++]))
     {
         if (--_xbf_cntdwn == 0)
         {
             Rx_TestCS();
             RxTakState = TakeDataState.UnsolCleanup;
         }
         // implied else is stay in take unsolicited state
     }
     else
     {
         // timed out; time to bail.
         _xbf_stat  = 'T';
         RxTakState = TakeDataState.Cleanup;
     }
 }
        private void Rx_TakInitial()
        {
            _xbf_cntdwn = 0;
            _xbf_i      = _xbf_i_lenpos = 0;
            _xbf_stat   = '?';

            if (!Rx1Byte(ref _xbf[_xbf_i++]))
            {
                // first byte has timed out; bail.
                // _xbf_cntdwn undefined, strictly speaking
                _xbf_i_lenpos = _xbf_i;
                _xbf_stat     = 'T';
                RxTakState    = TakeDataState.Cleanup;
            }
            else if (_xbf[_xbf_i - 1] == 0x4F)
            {
                // first byte is 0x4F, may be a brief or typical response
                // _xbf_cntdwn may be defined in brief state
                // _xbf_i_lenpos may be defined in brief state
                RxTakState = TakeDataState.Brief;
            }
            else if (_xbf[_xbf_i - 1] <= 0x0C)
            {
                // else if the first byte is 0x0C or less, then unsolicited in progress
                // move the length around to the next byte, fake a 0x4F in the first byte,
                // update counters accordingly
                _xbf[_xbf_i_lenpos = _xbf_i] = _xbf[_xbf_i - 1];
                _xbf[_xbf_i - 1]             = 0x4F;
                _xbf_cntdwn = _xbf[_xbf_i_lenpos] + 1;  // checksum too
                ++_xbf_i;
                RxTakState = TakeDataState.Unsol;
            }
            else
            {
                // else some kind of sync problem has occured; bail.
                // _xbf_cntdwn undefined, strictly speaking
                _xbf_i_lenpos = _xbf_i;
                _xbf_stat     = 'Y';
                RxTakState    = TakeDataState.Cleanup;
            }
        }
 private void Rx_TakBrief()
 {
     if (_xmsg.RplyMsg.Length == 1)
     {
         // the reply length is 1, we're done; no, really, we are
         _xbf_stat  = 'C';
         RxTakState = TakeDataState.Cleanup;
     }
     else if (Rx1Byte(ref _xbf[_xbf_i++]))
     {
         // get the count without errors, continue to typical
         _xbf_i_lenpos = _xbf_i - 1;
         _xbf_cntdwn   = _xbf[_xbf_i_lenpos] + 1; // checksum too
         RxTakState    = TakeDataState.Typical;
     }
     else
     {
         // else we timed out and this is over
         // _xbf_cntdwn undefined, strictly speaking
         _xbf_i_lenpos = _xbf_i;
         _xbf_stat     = 'T';
         RxTakState    = TakeDataState.Cleanup;
     }
 }