public async Task Start(string portName, int baudRate, CancellationTokenSource cts) { Debug.WriteLine("Element:CommunicationTask: Start: port: {0} baudRate: {1}", portName, baudRate); //serialPort = new CommunicationChannelBT(); serialChannel = new CommunicationChannelSerial(cts) { Name = portName, BaudRate = (uint)baudRate }; try { Debug.WriteLine("IP: Element:CommunicationTask: serialPort " + portName + " opening..."); await serialChannel.Open(); Debug.WriteLine("OK: Element:CommunicationTask: serialPort " + portName + " opened"); //await Task.Delay(2000); // Notify users to initialize any devices // they have before we start processing commands: element.StartingCommunication(serialChannel); running = true; ClearJobQueues(); Task workerTask = new Task(() => ThreadProc(cts.Token), cts.Token); workerTask.Start(); Debug.WriteLine("IP: Element:CommunicationTask: trying to synch up with the board - resetting..."); // try to synch up with the board string resp = EnqueueCommJobAndWait(Priority.High, "reset"); //await Task.Delay(3000); // let other processes continue // This HAS TO BE HERE...Since it takes the Element about a second to boot up // using the new Tiny Bootloader... //Thread.Sleep(1000); await Task.Delay(2000); // Clear receive buffer out, since the bootloader can send // some junk characters, which might hose subsequent command responses: serialChannel.DiscardInBuffer(); stopWatch.Start(); } catch (AggregateException aexc) { throw new CommunicationException("CommunicationTask: Could not start communication"); } catch (Exception exc) { Debug.WriteLine("Error: Element:CommunicationTask: exception while opening serial port " + portName + " : " + exc); //await Stop(); //throw new CommunicationException("CommunicationTask: Could not start communication"); throw; } }
public async Task Open(CancellationTokenSource cts) { cancellationTokenSource = cts; serialChannel = new CommunicationChannelSerial(cts, false) { Name = ComPortName, BaudRate = (uint)ComBaudRate, NewLineIn = "\n", NewLineOut = "\n" // not really used }; try { Debug.WriteLine("IP: PixyCamera: serialPort " + ComPortName + " opening..."); await serialChannel.Open(); Debug.WriteLine("OK: PixyCamera: serialPort " + ComPortName + " opened"); running = true; Task workerTask = new Task(() => ThreadProc(cts.Token), cts.Token); workerTask.Start(); } catch (AggregateException aexc) { throw new CommunicationException("PixyCamera: Could not start communication to Pixy board"); } catch (Exception exc) { Debug.WriteLine("Error: PixyCamera: exception while opening serial port " + ComPortName + " : " + exc); throw; } }
private void HandleConnectionPolling(object state) { lock (_syncLock) { if (!_connected) { //try connection.. _communication.Open(_rem); } } }
/// <summary> /// Creates the transaction processor ready for use. Also creates and initialises the /// device endpoint and the communications channel and opens the channel. /// </summary> /// <returns>ITransactionProcessor.</returns> public ITransactionProcessor CreateTransactionProcessor() { Channel = new ChannelFactory().FromConnectionString(ConnectionString); observer = new TransactionObserver(Channel); processor = new ReactiveTransactionProcessor(); processor.SubscribeTransactionObserver(observer, TimeSpan.FromMilliseconds(100)); Channel.Open(); // iOptron ZEQ25 may need a second to initialize after starting the connection Task.Delay(TimeSpan.FromSeconds(1)).Wait(); return(processor); }
/// <inheritdoc /> /// <summary> /// Creates the transaction processor ready for use. Also creates and initialises the /// device endpoint and the communications channel and opens the channel. /// </summary> /// <returns>ITransactionProcessor.</returns> public ITransactionProcessor CreateTransactionProcessor() { var factory = new ChannelFactory(); // ToDo - inject the factory in the constructor Channel = factory.FromConnectionString(connectionString); observer = new TransactionObserver(Channel); processor = new ReactiveTransactionProcessor(); processor.SubscribeTransactionObserver(observer); Channel.Open(); return(processor); }
/// <summary> /// Creates the transaction processor ready for use. Also creates and initialises the /// device endpoint and the communications channel and opens the channel. /// </summary> /// <returns>ITransactionProcessor.</returns> public ITransactionProcessor CreateTransactionProcessor() { Endpoint = DeviceEndpoint.FromConnectionString(ConnectionString); Channel = CommunicationsStackBuilder.BuildChannel(Endpoint); observer = new TransactionObserver(Channel); processor = new ReactiveTransactionProcessor(); processor.SubscribeTransactionObserver(observer, TimeSpan.FromMilliseconds(100)); Channel.Open(); //Task.Delay(TimeSpan.FromSeconds(2)).Wait(); // Arduino needs 2 seconds to initialize Thread.Sleep(TimeSpan.FromSeconds(3)); return(processor); }
public void Open(bool performOnConnectActions = true) { SubscribeControllerEvents(); channel.Open(); stateMachine.Initialize(new RequestStatusState(stateMachine)); // Rotator stateMachine.Initialize(new OfflineState(stateMachine)); // Shutter stateMachine.WaitForReady(configuration.TimeToWaitForShutterOnConnect); connectedTimestamp = DateTime.UtcNow; if (performOnConnectActions) { PerformActionsOnConnect(); } }
public void Open(bool performOnConnectActions = true) { SubscribeControllerEvents(); channel.Open(); if (performOnConnectActions) { stateMachine.Initialize(new RequestStatus(stateMachine)); } else { stateMachine.Initialize(new Ready(stateMachine)); } stateMachine.WaitForReady(TimeSpan.FromSeconds(5)); if (performOnConnectActions && configuration.PerformShutterRecovery) { PerformShutterRecovery(); } }
public async Task Start(string portName, int baudRate, CancellationTokenSource cts) { Debug.WriteLine("ArduinoBrick:CommunicationTask: Start: port: {0} baudRate: {1}", portName, baudRate); cancellationTokenSource = cts; //serialPort = new CommunicationChannelBT(); serialChannel = new CommunicationChannelSerial(cts) { Name = portName, BaudRate = (uint)baudRate, NewLineIn = "\r\n>", NewLineOut = "\r" }; try { Debug.WriteLine("IP: ArduinoBrick:CommunicationTask: serialPort " + portName + " opening..."); await serialChannel.Open(); Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: serialPort " + portName + " opened"); //await Task.Delay(2000); // Notify users to initialize any devices // they have before we start processing commands: arduinoBrick.StartingCommunication(serialChannel); //commandsBufferBlock = new BufferBlock<CommandAndResponse>( // new DataflowBlockOptions() { CancellationToken = cts.Token }); actionBlock = new ActionBlock <CommandAndResponse>( async prompt => { await serialChannel.WriteLine(prompt.command); string response = await serialChannel.ReadLine(); prompt.completionSource.SetResult(response); }, new ExecutionDataflowBlockOptions() { CancellationToken = cts.Token, BoundedCapacity = 10 }); //commandsBufferBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true }); Debug.WriteLine("IP: ArduinoBrick:CommunicationTask: trying to synch up with the board - resetting..."); string resp = null; int count = 10; bool boardFound = false; while (count-- > 0) { // try to sync up with the board resp = await SendAndReceive("reset"); Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: 'reset' -> '" + resp + "'"); if (string.Equals(resp, "Arduino firmware Plucky Wheels")) { boardFound = true; break; } } if (boardFound) { Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: found Plucky Wheels Arduino brick"); } else { throw new CommunicationException("CommunicationTask: Could not find Plucky Wheels Arduino brick, invalid response to 'reset' at serial port " + portName); } stopWatch.Start(); running = true; } catch (AggregateException aexc) { throw new CommunicationException("CommunicationTask: Could not start communication"); } catch (Exception exc) { Debug.WriteLine("Error: ArduinoBrick:CommunicationTask: exception while opening serial port " + portName + " : " + exc); //await Stop(); //throw new CommunicationException("CommunicationTask: Could not start communication"); throw; } }
public async Task Start(string portName, int baudRate, CancellationTokenSource cts) { Debug.WriteLine("ArduinoBrick:CommunicationTask: Start: port: {0} baudRate: {1}", portName, baudRate); cancellationTokenSource = cts; //serialPort = new CommunicationChannelBT(); serialChannel = new CommunicationChannelSerial(cts) { Name = portName, BaudRate = (uint)baudRate, NewLineIn = "\r\n>", NewLineOut = "\r" }; try { Debug.WriteLine("IP: ArduinoBrick:CommunicationTask: serialPort " + portName + " opening..."); await serialChannel.Open(); Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: serialPort " + portName + " opened"); //await Task.Delay(2000); // Notify users to initialize any devices // they have before we start processing commands: arduinoBrick.StartingCommunication(serialChannel); //commandsBufferBlock = new BufferBlock<CommandAndResponse>( // new DataflowBlockOptions() { CancellationToken = cts.Token }); actionBlock = new ActionBlock<CommandAndResponse>( async prompt => { await serialChannel.WriteLine(prompt.command); string response = await serialChannel.ReadLine(); prompt.completionSource.SetResult(response); }, new ExecutionDataflowBlockOptions() { CancellationToken = cts.Token, BoundedCapacity = 10 }); //commandsBufferBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true }); Debug.WriteLine("IP: ArduinoBrick:CommunicationTask: trying to synch up with the board - resetting..."); string resp = null; int count = 10; bool boardFound = false; while (count-- > 0) { // try to sync up with the board resp = await SendAndReceive("reset"); Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: 'reset' -> '" + resp + "'"); if (string.Equals(resp, "Arduino firmware Plucky Wheels")) { boardFound = true; break; } } if (boardFound) { Debug.WriteLine("OK: ArduinoBrick:CommunicationTask: found Plucky Wheels Arduino brick"); } else { throw new CommunicationException("CommunicationTask: Could not find Plucky Wheels Arduino brick, invalid response to 'reset' at serial port " + portName); } stopWatch.Start(); running = true; } catch (AggregateException aexc) { throw new CommunicationException("CommunicationTask: Could not start communication"); } catch (Exception exc) { Debug.WriteLine("Error: ArduinoBrick:CommunicationTask: exception while opening serial port " + portName + " : " + exc); //await Stop(); //throw new CommunicationException("CommunicationTask: Could not start communication"); throw; } }
public async Task Open(CancellationTokenSource cts) { cancellationTokenSource = cts; serialChannel = new CommunicationChannelSerial(cts) { Name = ComPortName, BaudRate = (uint)ComBaudRate, NewLineIn = "\r\n", NewLineOut = "\r\n" }; try { Debug.WriteLine("IP: PixyCamera: serialPort " + ComPortName + " opening..."); await serialChannel.Open(); Debug.WriteLine("OK: PixyCamera: serialPort " + ComPortName + " opened"); //commandsBufferBlock = new BufferBlock<CommandAndResponse>( // new DataflowBlockOptions() { CancellationToken = cts.Token }); //actionBlock = new ActionBlock<CommandAndResponse>( // async prompt => // { // await serialChannel.WriteLine(prompt.command); // string response = await serialChannel.ReadLine(); // prompt.completionSource.SetResult(response); // }, // new ExecutionDataflowBlockOptions() { CancellationToken = cts.Token, BoundedCapacity = 10 }); //commandsBufferBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true }); Debug.WriteLine("IP: PixyCamera: trying to synch up with the board - resetting..."); string resp = null; int count = 10; bool boardFound = false; while (count-- > 0) { // try to sync up with the board //resp = await SendAndReceive("reset"); Debug.WriteLine("OK: PixyCamera: 'reset' -> '" + resp + "'"); if (string.Equals(resp, "Arduino firmware Plucky Wheels")) { boardFound = true; break; } } if (boardFound) { Debug.WriteLine("OK: PixyCamera: found Plucky Wheels Arduino brick"); } else { throw new CommunicationException("CommunicationTask: Could not find Plucky Wheels Arduino brick, invalid response to 'reset' at serial port " + ComPortName); } stopWatch.Start(); running = true; } catch (AggregateException aexc) { throw new CommunicationException("CommunicationTask: Could not start communication"); } catch (Exception exc) { Debug.WriteLine("Error: PixyCamera: exception while opening serial port " + ComPortName + " : " + exc); //await Stop(); //throw new CommunicationException("CommunicationTask: Could not start communication"); throw; } /* * TODO: port to Universal Windows try { _serialPort = new SerialPort(ComPortName, ComBaudRate, Parity.None, 8, StopBits.One); _serialPort.Handshake = Handshake.RequestToSendXOnXOff; //.None; _serialPort.Encoding = Encoding.ASCII; // that's only for text read, not binary _serialPort.NewLine = "\r\n"; _serialPort.ReadTimeout = 1100; _serialPort.WriteTimeout = 10000; _serialPort.DtrEnable = false; _serialPort.RtsEnable = false; //p.ParityReplace = 0; _serialPort.Open(); _serialPort.DiscardInBuffer(); _serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); Debug.WriteLine("OK: PixyCamera Open(" + ComPortName + ") success!"); } catch { Debug.WriteLine("Error: PixyCamera Open(" + ComPortName + ") failed"); _serialPort = null; } */ }