Пример #1
0
        public static bool TryParse(byte[] bytes, out MoCoBusFrame frame)
        {
            frame = null;
            if (bytes.Length < 10) return false;
            if (bytes[0] != 0 || bytes[1] != 0 || bytes[2] != 0 || bytes[3] != 0 || bytes[4] != 0) return false;
            if (bytes[5] != 0xff) return false;
            if (bytes[9] != (bytes.Length - 10)) return false;

            switch (bytes[7])
            {
                case 0:
                    frame = new MoCoBusMainCommandFrame(bytes[6], (MoCoBusMainCommand)bytes[8], bytes.Skip(10).ToArray());
                    return true;
                case 1:
                case 2:
                case 3:
                    frame = new MoCoBusMotorCommandFrame(bytes[6], bytes[7], (MoCoBusMotorCommand)bytes[8], bytes.Skip(10).ToArray());
                    return true;
                case 4:
                    frame = new MoCoBusCameraCommandFrame(bytes[6], (MoCoBusCameraCommand)bytes[8], bytes.Skip(10).ToArray());
                    return true;
                default:
                    frame = new MoCoBusFrame(bytes[6], bytes[7], bytes[8], bytes.Skip(10).ToArray());
                    return true;
            }
        }
        public virtual async Task<MoCoBusFrame> SendAndReceiveAsync(MoCoBusFrame frame, CancellationToken token)
        {
            using (await _mutex.LockAsync(token).ConfigureAwait(false))
            {
                for (var retry = 0; retry < MaxRetry; retry++)
                {
                    token.ThrowIfCancellationRequested();
                    
                    try
                    {
                        ClearReceiveBuffer();
                        Send(frame);
                        var result = await ReceiveAsync(CancellationToken.None).ConfigureAwait(false);
                        if (_retryCounter > 0) 
                        {
                            _retryCounter--;   
                        }
                        return result;
                    }
                    catch (TimeoutException)
                    {
                        Debug.WriteLine("SendAndReceiveAsync: Timeout! (Counter: {0})", _retryCounter);

                        if (_retryCounter < 1000)
                        {
                            _retryCounter += 5;
                        }
                        if (_retryCounter >= 100)
                        {
                            Debug.WriteLine("RetryCounter >= 100 ({0}): Try to reconnect!", _retryCounter);
                            Insights.Track("MoCoBusCommServiceBaseRetryCounterOverrun", 
                                new Dictionary<string, string> 
                                { 
                                    { "RetryCounter", _retryCounter.ToString() } 
                                });

                            _retryCounter = 0;
                            Disconnect();
                            await Task.Delay(500).ConfigureAwait(false);
                            break;
                        }
                    }

                    await Task.Delay(50).ConfigureAwait(false);
                    Debug.WriteLine("SendAndReceiveAsync: Retry!");
                }
            }

            throw new TimeoutException();
        }
 public abstract void Send(MoCoBusFrame frame);
 public virtual async Task<MoCoBusFrame> SendAndReceiveAsync(MoCoBusFrame frame)
 {
     return await SendAndReceiveAsync(frame, CancellationToken.None).ConfigureAwait(false);
 }
        public override void Send(MoCoBusFrame frame)
        {
            if (!IsConnected || _moCoBusTxCharacteristic == null)
                return;

            _moCoBusTxCharacteristic.Write(frame.ToByteArray());
        }