예제 #1
0
        private void UpdateConfiguration()
        {
            ThrowWhenNotConnected();


            // ReSharper disable once UseObjectOrCollectionInitializer
            var configPacket         = new ConfigPacket();
            var configResponsePacket = new ConfigResponsePacket();


            for (int i = 0; i < _masterInterface.InputModes.Length; i++)
            {
                configPacket.UniversalInputs[i].Mode = _masterInterface.InputModes[i];
            }
            for (int i = 0; i < _masterInterface.InputIsDigital.Length; i++)
            {
                configPacket.UniversalInputs[i].Digital = _masterInterface.InputIsDigital[i];
            }

            for (int i = 0; i < _masterInterface.OutputModes.Length; i++)
            {
                configPacket.Motor[i] = _masterInterface.OutputModes[i];
            }

            try
            {
                TxCommunication.SendPacket(configPacket, configResponsePacket);
            }
            catch (Exception e)
            {
                HandleException(e);
            }

            _configurationChanged = false;
        }
예제 #2
0
        public void UpdateValues()
        {
            ThrowWhenNotConnected();

            if (_configurationChanged)
            {
                UpdateConfiguration();
                _configurationChanged = false;
            }


            var outputPacket = new OutputPacket();
            var inputPacket  = new InputPacket();


            for (int i = 0; i < _masterInterface.OutputValues.Length; i++)
            {
                outputPacket.PwmOutputValues[i] = (short)_masterInterface.OutputValues[i];
            }



            try
            {
                TxCommunication.SendPacket(outputPacket, inputPacket);
            }
            catch (Exception e)
            {
                HandleException(e);
                return;
            }


            IList <int> valueChanged = new List <int>();

            for (int i = 0; i < inputPacket.UniversalInputs.Length; i++)
            {
                var newInputValue = inputPacket.UniversalInputs[i];

                if (_masterInterface.GetInputValue(i) != newInputValue)
                {
                    _masterInterface.SetInputValue(i, (short)newInputValue);

                    valueChanged.Add(i);
                }
            }

            if (valueChanged.Count > 0)
            {
                // Fire an event when an input value has changed
                InputValueChangedEventArgs eventArgs = new InputValueChangedEventArgs(valueChanged);
                _inputValueChanged?.Invoke(this, eventArgs);
            }
        }
예제 #3
0
        public string GetInterfaceVersionCode()
        {
            ThrowWhenNotConnected();

            try
            {
                var requestInfoResponsePacket = new RequestInfoResponsePacket();
                TxCommunication.SendPacket(new RequestInfoPacket(), requestInfoResponsePacket);
                return(requestInfoResponsePacket.FirmwareVersion);
            }
            catch (Exception e)
            {
                HandleException(e);
            }
            return(string.Empty);
        }
예제 #4
0
        public void StartOnlineMode()
        {
            LogMessage("Starting online mode");
            if (Connection == ConnectionStatus.Online)
            {
                throw new InvalidOperationException("Already in online mode");
            }
            ThrowWhenNotConnected();

            _updateValuesTimer           = new Timer(UpdateInterval);
            _updateValuesTimer.Elapsed  += UpdateValuesTimerTick;
            _updateValuesTimer.AutoReset = true;


            try
            {
                // Send an echo packet to obtain the session id
                var echoResponsePacket = new EchoResponsePacket();
                TxCommunication.SendPacket(new EchoPacket(), echoResponsePacket);

                Connection = ConnectionStatus.Online;

                // Start update timer
                _updateValuesTimer.Start();

                // Fire event to notify that online mode has started
                _onlineStarted?.Invoke(this, new EventArgs());

                // Fire InputValueChanged event with default values
                List <int> inputPorts = new List <int>();
                for (int i = 0; i < UniversalInputs; i++)
                {
                    inputPorts.Add(i);
                }
                InputValueChangedEventArgs eventArgs = new InputValueChangedEventArgs(inputPorts);

                _inputValueChanged?.Invoke(this, eventArgs);
            }
            catch (Exception e)
            {
                LogMessage($"Exception while starting online mode: {e.Message}");
                Connection = ConnectionStatus.Invalid;
                HandleException(e);
            }
            LogMessage("Online mode started");
        }