static void Main(string[] args) { var log = LogManager.GetCurrentClassLogger(); var connectionString = "COM3:9600"; var factory = new ChannelFactory(); var channel = factory.FromConnectionString(connectionString); var observer = new TransactionObserver(channel); var processor = new ReactiveTransactionProcessor(); processor.SubscribeTransactionObserver(observer); channel.Open(); // normmaly done in drivers Connected property var transaction = new TerminatedStringTransaction("*OPEN_MIRROR_COVER\r", initiator: '*', terminator: '\r'); // ":GR#" log.Debug($"Created transaction: {transaction}"); processor.CommitTransaction(transaction); transaction.WaitForCompletionOrTimeout(); log.Debug($"Finished transaction: {transaction}"); if (transaction.Successful && transaction.Response.Any()) { var response = transaction.Response.Single(); log.Info($"received response: {response}"); } Console.WriteLine("Press enter.."); Console.Read(); }
private static void Main(string[] args) { #region Setup for Reactive ASCOM var connectionString = Settings.Default.ConnectionString; // Edit in App.config, default is "COM1:" var channelFactory = new ChannelFactory(); var channel = channelFactory.FromConnectionString(connectionString); var transactionObserver = new TransactionObserver(channel); var transactionProcessor = new ReactiveTransactionProcessor(); transactionProcessor.SubscribeTransactionObserver(transactionObserver); channel.Open(); #endregion Setup for Reactive ASCOM #region Submit some transactions // Ready to go. We are going to use tasks to submit the transactions, just to demonstrate thread safety. var raTransaction = new TerminatedStringTransaction(":GR#", '#', ':') { Timeout = TimeSpan.FromSeconds(2) }; // The terminator and initiator are optional parameters and default to values that work for Meade style protocols. var decTransaction = new TerminatedStringTransaction(":GD#") { Timeout = TimeSpan.FromSeconds(2) }; Task.Run(() => transactionProcessor.CommitTransaction(raTransaction)); Task.Run(() => transactionProcessor.CommitTransaction(decTransaction)); #endregion Submit some transactions #region Wait for the results // NOTE we are using the transactions in the reverse order that we committed them, just to prove a point. Console.WriteLine("Waiting for declination"); decTransaction.WaitForCompletionOrTimeout(); Console.WriteLine("Declination: {0}", decTransaction.Response); Console.WriteLine("Waiting for Right Ascensions"); raTransaction.WaitForCompletionOrTimeout(); Console.WriteLine("Right Ascension: {0}", raTransaction.Response); #endregion Wait for the results #region Cleanup /* * To clean up, we just need to dispose the transaction processor. This terminates the * sequence of transactions, which causes the TransactionObserver's OnCompleted method * to be called, which unsubscribes the receive sequence, which closes the * communications channel */ transactionProcessor.Dispose(); #endregion Cleanup Console.WriteLine("Press ENTER:"); Console.ReadLine(); }