Ejemplo n.º 1
0
        /// <summary>
        /// Configures the unique UDP connection.
        /// </summary>
        /// <param name="connectionInfo">The connection information.</param>
        /// <param name="reporter">The reporter.</param>
        /// <param name="timeout">The timeout in milliseconds.</param>
        /// <param name="retryLimit">The retry limit.</param>
        /// <returns>UdpConnectionInfo.</returns>
        /// <exception cref="System.Exception">Device send IP address and/or port could not be configured for unique connection.</exception>
        /// <autogeneratedoc />
        /// <remarks>
        /// The device may not respond for up to 5 seconds. The total retry period must therefore be greater than 5 seconds.
        /// </remarks>
        public static UdpConnectionInfo ConfigureUniqueUdpConnection(UdpConnectionInfo connectionInfo, IReporter reporter = null, int timeout = 500, int retryLimit = 20)
        {
            UdpConnectionInfo uniqueConnectionInfo = UdpConnectionInfo.CreateUniqueConnectionInfo(connectionInfo);

            Connection uniqueConnection = new Connection(uniqueConnectionInfo);

            try
            {
                if (reporter != null)
                {
                    uniqueConnection.Error     += reporter.OnError;
                    uniqueConnection.Exception += reporter.OnException;
                    uniqueConnection.Info      += reporter.OnInfo;
                    uniqueConnection.Message   += reporter.OnMessage;
                }

                // connect using the correct receive Port
                uniqueConnection.Connect();

                uniqueConnection.Settings.WifiSendIPAddress.Value = uniqueConnectionInfo.AdapterIPAddress;
                uniqueConnection.Settings.WifiSendPort.Value      = (ushort)uniqueConnectionInfo.ReceivePort;

                ISettingItem[] settingsToBeWritten = new ISettingItem[] { uniqueConnection.Settings.WifiSendIPAddress, uniqueConnection.Settings.WifiSendPort };

                int retryCount = 0;

                while (true)
                {
                    if (uniqueConnection.Settings.Write(settingsToBeWritten, reporter, timeout, 0) == CommunicationProcessResult.Success)
                    {
                        break;
                    }
                    else if (retryCount++ <= retryLimit)
                    {
                        Commands.Send(uniqueConnection, Command.Apply, 0, 0);
                    }
                    else
                    {
                        throw new Exception("Device send IP address and/or port could not be configured for unique connection.");
                    }
                }
            }
            finally
            {
                uniqueConnection.Close();

                if (reporter != null)
                {
                    uniqueConnection.Error     -= reporter.OnError;
                    uniqueConnection.Exception -= reporter.OnException;
                    uniqueConnection.Info      -= reporter.OnInfo;
                    uniqueConnection.Message   -= reporter.OnMessage;
                }
            }

            return(uniqueConnectionInfo);
        }
Ejemplo n.º 2
0
        private void CreateUdpComms()
        {
            UdpConnectionInfo info = ConnectionInfo as UdpConnectionInfo;

            connectionImplementation = new UdpConnectionImplementation(this, info, CommunicationStatistics);
        }