Example #1
0
        private Task <T> ReadNCAsync <T>(int?timeout = null)
        {
            using (ObtainLock())
            {
                int DefaultTimeout = 2000;
                if (timeout != null)
                {
#if VISACOM
                    DefaultTimeout  = session.Timeout;
                    session.Timeout = timeout.Value;
#else
                    DefaultTimeout = Session.TimeoutMilliseconds;
                    Session.TimeoutMilliseconds = timeout.Value;
#endif
                }

#if VISACOM
                // Do things synchronously
                return(Task.FromResult(ReadNC <T>(session)));
#else
                var task = new TaskCompletionSource <T>();
                Session.RawIO.BeginRead(1000, res =>
                {
                    if (res.IsCompleted)
                    {
                        if (res.Count == 0)
                        {
                            //throw new Exception(string.Format("Asynchronous operation {0} timed out.", nameof(ReadNCAsync)));
                            Exception ex = new TimeoutException(string.Format("Asynchronous operation {0} timed out.", nameof(ReadNCAsync)));
                            task.SetException(ex);
                            //task.SetCanceled();
                        }
                        else
                        {
                            string str = System.Text.Encoding.Default.GetString(res.Buffer, 0, (int)res.Count);

                            TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(T));
                            LogCommandReceived(str);
                            task.SetResult(ScpiResponseConverter.ConvertFromString <T>(str));
                        }
                    }
                    else
                    {
                        var Exception = new OperationCanceledException(string.Format("Asynchronous operation {0} not completed.", nameof(ReadNCAsync)));
                        task.SetException(Exception);
                    }
                    Session.RawIO.EndRead(res);
                    // Restore timeout
                    if (timeout != null)
                    {
                        Session.TimeoutMilliseconds = DefaultTimeout;
                    }
                }, null);
                return(task.Task);
#endif
            }
        }
Example #2
0
        private T ReadNC <T>()
        {
            using (ObtainLock())
            {
#if VISACOM
                string response = session.ReadString(65546);
#else
                string response = Session.FormattedIO.ReadLine();
#endif
                // remove trailing termination character if enabled
                if (Session.TerminationCharacterEnabled && !string.IsNullOrEmpty(response) && response[response.Length - 1] == Session.TerminationCharacter)
                {
                    response = response.Substring(0, response.Length - 1);
                }
                LogCommandReceived(response);
                return(ScpiResponseConverter.ConvertFromString <T>(response));
            }
        }