コード例 #1
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        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
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        public void Connect(string mac)
        {
            LogMessage($"Connecting to {mac}");
            Mac = mac;
            if (Connection == ConnectionStatus.Connected || Connection == ConnectionStatus.Online)
            {
                throw new InvalidOperationException("Already connected to an interface");
            }

            TxCommunication?.Dispose();
            TxCommunication = new TxCommunication(SerialAdapter);

            try
            {
                TxCommunication.OpenConnection(mac);
                Connection = TxCommunication.Connected ? ConnectionStatus.Connected : ConnectionStatus.NotConnected;
            }
            catch (Exception e)
            {
                LogMessage($"Exception while connecting: {e.Message}");
                Connection = ConnectionStatus.Invalid;
                HandleException(e);
                return;
            }


            _masterInterface.ResetValues();

            LogMessage("Connected");
            _connected?.Invoke(this, new EventArgs());
        }
コード例 #3
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        public void Disconnect()
        {
            LogMessage("Disconnecting");
            ThrowWhenNotConnected();

            try
            {
                TxCommunication.CloseConnection();
                Connection = ConnectionStatus.NotConnected;
            }
            catch (Exception e)
            {
                LogMessage($"Exception while disconnecting: {e.Message}");
                Connection = ConnectionStatus.Invalid;
                HandleException(e);
            }

            TxCommunication.Dispose();
            TxCommunication = null;


            _disconnected?.Invoke(this, new EventArgs());

            LogMessage("Disconnected");
            _masterInterface.ResetValues();
        }
コード例 #4
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        public bool IsValidInterface(string address)
        {
            if (TxCommunication == null)
            {
                TxCommunication = new TxCommunication(SerialAdapter);
            }

            return(TxCommunication.IsValidInterface(address));
        }
コード例 #5
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        public string RequestControllerName(string address)
        {
            if (TxCommunication == null)
            {
                TxCommunication = new TxCommunication(SerialAdapter);
            }

            return(TxCommunication.RequestControllerName(address));
        }
コード例 #6
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        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);
            }
        }
コード例 #7
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        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);
        }
コード例 #8
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        public void Dispose()
        {
            Connection = ConnectionStatus.NotConnected;

            if (TxCommunication != null)
            {
                TxCommunication.Dispose();
                TxCommunication = null;
            }

            if (SerialAdapter != null)
            {
                SerialAdapter.Dispose();
            }

            _masterInterface.ResetValues();
        }
コード例 #9
0
ファイル: TxInterface.cs プロジェクト: Bennik2000/FtApp
        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");
        }