private void SetupCommunication(string COMPort, int BaudRate) { _ledState = false; // Create Serial Port transport object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = COMPort, BaudRate = BaudRate, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer // Set if it is communicating with a 16- or 32-bit Arduino board _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit32); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Start listening var status = _cmdMessenger.Connect(); if (!status) { Console.WriteLine("No connection could be made"); return; } }
public bool SearchAndConnectToArduino() { for (var i = 0; i <= 20; i++) { _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM" + i.ToString(), BaudRate = 115200 } }; _cmdMessenger = new CmdMessenger(_serialTransport) { BoardType = BoardType.Bit16 }; AttachCommandCallBacks(); _cmdMessenger.Connect(); var commandCheck = new SendCommand((int)Command.pCheckArduino); _cmdMessenger.SendCommand(commandCheck); Thread.Sleep(100);//wait 100 ms for the response if (_arduinoFound) { return(true); } //else _cmdMessenger.Disconnect(); _cmdMessenger.Dispose(); _serialTransport.Dispose(); } return(false); }
/// <summary> Process the queue. </summary> protected override void ProcessQueue() { // Endless loop unless aborted while (ThreadRunState != ThreadRunStates.Abort) { // Calculate sleep time based on incoming command speed //_queueSpeed.SetCount(Queue.Count); //_queueSpeed.CalcSleepTime(); EventWaiter.Wait(1000); // Process queue unless stopped if (ThreadRunState == ThreadRunStates.Start) { // Only actually sleep if there are no commands in the queue //if (Queue.Count == 0) _queueSpeed.Sleep(); var dequeueCommand = DequeueCommand(); if (dequeueCommand != null) { CmdMessenger.HandleMessage(dequeueCommand); } } //else //{ // _queueSpeed.Sleep(); //} } }
/// <summary> Receive command queue constructor. </summary> /// <param name="disposeStack"> DisposeStack. </param> /// <param name="cmdMessenger"> The command messenger. </param> public ReceiveCommandQueue(DisposeStack disposeStack, CmdMessenger cmdMessenger ) : base(disposeStack, cmdMessenger) { disposeStack.Push(this); QueueThread.Name = "ReceiveCommandQueue"; // _queueSpeed.Name = "ReceiveCommandQueue"; }
public static void Disconnect() { LogCommands(false); CmdMessenger.Disconnect(); Connected = false; //CmdMessenger.Dispose(); }
// Setup function public void Setup(string comm, ASCOM.Utilities.TraceLogger tr) { tl = tr; // Create Serial Port object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = comm, BaudRate = 115200, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer _cmdMessenger = new CmdMessenger(_serialTransport) { BoardType = BoardType.Bit16 // Set if it is communicating with a 16- or 32-bit Arduino board }; // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Attach to NewLinesReceived for logging purposes // _cmdMessenger.NewLineReceived += NewLineReceived; // Attach to NewLineSent for logging purposes // _cmdMessenger.NewLineSent += NewLineSent; // Start listening _cmdMessenger.StartListening(); }
// ------------------ M A I N ---------------------- // Setup function public void Setup() { // Create Serial Port object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM6", BaudRate = 115200, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer // Set if it is communicating with a 16- or 32-bit Arduino board _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit16); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Attach to NewLinesReceived for logging purposes _cmdMessenger.NewLineReceived += NewLineReceived; // Attach to NewLineSent for logging purposes _cmdMessenger.NewLineSent += NewLineSent; // Start listening _cmdMessenger.Connect(); }
public DisplayOutput(CmdMessenger cmdMessenger, byte id, EnumDisplayDataTypes dataType, int length, bool fillBlankSpace) : base(cmdMessenger, EnumIoObjectType.DisplayOutput) { _id = id; _length = length; _fillBlankSpace = fillBlankSpace; _dataType = dataType; }
public BoardInterface(Control c) { OutputPins = new List <NamedPin>(); InputPins = new List <NamedPin>(); _transport = new SerialTransport() { CurrentSerialSettings = { DtrEnable = false, BaudRate = 9600 } }; _messenger = new CmdMessenger(_transport, BoardType.Bit32) { PrintLfCr = false, ControlToInvokeOn = c }; _messenger.NewLineReceived += Messenger_NewLineReceived; _messenger.NewLineSent += Messenger_NewLineSent; _messenger.Attach(Messenger_UnknownCommand); _messenger.Attach((int)Command.Debug, Messenger_DebugCommand); _messenger.Attach((int)Command.InitializationFinished, Messenger_InitialzationFinishedCommand); _messenger.Attach((int)Command.PinSet, Messenger_PinSetCommand); _connectionManager = new SerialConnectionManager(_transport as SerialTransport, _messenger, (int)Command.Watchdog, COMMUNICATION_IDENTIFIER) { WatchdogEnabled = true, DeviceScanBaudRateSelection = false }; _connectionManager.Progress += ConnectionManager_Progress; _connectionManager.ConnectionFound += ConnectionManager_ConnectionFound; _connectionManager.ConnectionTimeout += ConnectionManager_ConnectionTimeout; }
// Setup function public void Setup() { _ledState = false; BluetoothInfo(); // Create Bluetooth transport object _bluetoothTransport = new BluetoothTransport { //CurrentBluetoothDeviceInfo = BluetoothUtils.DeviceByAdress("98-D3-31-B0-FB-B5") CurrentBluetoothDeviceInfo = BluetoothUtils.DeviceByAdress("20:13:07:26:10:08") }; // Create Serial Port transport object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM16", BaudRate = 9600, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer _cmdMessenger = new CmdMessenger(_bluetoothTransport) { BoardType = BoardType.Bit16 // Set if it is communicating with a 16- or 32-bit Arduino board }; // Tell CmdMessenger if it is communicating with a 16 or 32 bit Arduino board // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Start listening _cmdMessenger.Connect(); }
public void Start(string port) { _connectedPort = port; // Create Serial Port object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = _connectedPort, BaudRate = 115200, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer // Set if it is communicating with a 16- or 32-bit Arduino board _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit16); AttachCommandCallBacks(); // Attach the callbacks to the Command Messenger var status = _cmdMessenger.Connect(); // Start listening if (!status) { Console.WriteLine("No connection could be made"); return; } _isRunning = true; }
public bool Setup() { _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM4", BaudRate = 57600, DtrEnable = false } // object initializer }; _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit32); _cmdMessenger.Attach(OnUnknownCommand); _cmdMessenger.Attach((int)Command.Acknowledge, OnAcknowledge); _cmdMessenger.Attach((int)Command.Error, OnError); _cmdMessenger.Attach((int)Command.TempChange, OnTempChange); _cmdMessenger.Attach((int)Command.SsrChange, OnSsrChange); _cmdMessenger.Attach((int)Command.HeaterChange, OnHeaterChange); _cmdMessenger.NewLineReceived += NewLineReceived; _cmdMessenger.NewLineSent += NewLineSent; IsConnected = _cmdMessenger.Connect(); if (IsConnected) { ConnectionStatusEventHandler?.Invoke(this, new ConnectionStatusEvent { Type = ConnectionStatusEvent.EventType.Connected }); //TODO: Request Status } return(IsConnected); }
/// <summary> Sends all commands currently on queue. </summary> private void SendCommandFromQueue() { CommandStrategy commandStrategy; lock (Queue) { commandStrategy = Queue.Count != 0 ? Queue.Peek() : null; // Process command specific dequeue strategy if (commandStrategy != null) { commandStrategy.DeQueue(); } // Process all generic dequeue strategies foreach (var generalStrategy in GeneralStrategies) { generalStrategy.OnDequeue(); } } // Send command if (commandStrategy != null && commandStrategy.Command != null) { CmdMessenger.ExecuteSendCommand((SendCommand)commandStrategy.Command, ClearQueue.KeepQueue); } }
public void TestOpenConnection() { Common.StartTest("Test opening connection"); try { _cmdMessenger = Common.Connect(_systemSettings); AttachCommandCallBacks(); if (!Common.Connected) { Common.TestNotOk("Not connected after opening connection"); } } catch (Exception) { Common.TestNotOk("Exception during opening connection"); Common.EndTest(); if (_cmdMessenger == null) { Common.EndTestSet(); } return; } if (_systemSettings.Transport.IsConnected()) { Common.TestOk("No issues during opening connection"); } else { Common.TestNotOk("Not open after trying to open connection"); } Common.EndTest(); }
// Setup function public void Setup() { _ledState = false; _servoState = 90; // Create Serial Port object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM3", BaudRate = 115200, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer _cmdMessenger = new CmdMessenger(_serialTransport) { BoardType = BoardType.Bit16 // Set if it is communicating with a 16- or 32-bit Arduino board }; // Tell CmdMessenger if it is communicating with a 16 or 32 bit Arduino board // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Start listening _cmdMessenger.StartListening(); }
public SerialComm(Form1 form) { _form = form; // Create Serial Port object _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM3", BaudRate = 115200, DtrEnable = true } // object initializer }; // Initialize the command messenger with the Serial Port transport layer // Set if it is communicating with a 16- or 32-bit Arduino board _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit16); // Clear queues _cmdMessenger.ClearReceiveQueue(); _cmdMessenger.ClearSendQueue(); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); IsTriggered = false; Channels = new List <Channels>(); _traceBuffer = new List <float>(); _traces = new List <ISeismicTrace>(); }
public void Start() { var serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = ConfigurationManager.AppSettings.Get("serialPortName"), BaudRate = int.Parse(ConfigurationManager.AppSettings.Get("serialBaudRate")), DtrEnable = false } }; m_CmdMessenger = new CmdMessenger(serialTransport, BoardType.Bit16, (char)0x13); // TODO: Route messages to/from more than one CmdMessenger (serial port) based on MQTT topic. var sendBroker = new CommandMessengerSendBroker(m_CmdMessenger, s_MqttOptions, m_Topic); sendBroker.MqttConnectionClosed += SendBroker_MqttConnectionClosed; sendBroker.MqttConnectionError += SendBroker_MqttConnectionError; sendBroker.Connect(); var receiveBroker = new CommandMessengerReceiveBrokerBuffered(m_CmdMessenger, s_MqttOptions); receiveBroker.MqttConnectionClosed += ReceiveBroker_MqttConnectionClosed; receiveBroker.MqttConnectionError += ReceiveBroker_MqttConnectionError; receiveBroker.Connect($"{m_Topic}"); m_CmdMessenger.Connect(); Log.Information("MQTT Broker Started"); }
// ------------------ MAIN ---------------------- // Setup function public void Setup(ControllerForm controllerForm) { // storing the controller form for later reference _controllerForm = controllerForm; // Create Serial Port object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM3", BaudRate = 9600, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer // Set if it is communicating with a 16- or 32-bit Arduino board _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit16); // Tell CmdMessenger to "Invoke" commands on the thread running the WinForms UI _cmdMessenger.ControlToInvokeOn = _controllerForm; // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Attach to NewLinesReceived for logging purposes _cmdMessenger.NewLineReceived += NewLineReceived; // Attach to NewLineSent for logging purposes _cmdMessenger.NewLineSent += NewLineSent; // Start listening _cmdMessenger.Connect(); }
public async Task <bool> Setup() { _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM3", BaudRate = 57600, DtrEnable = false } // object initializer }; _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit32); _cmdMessenger.Attach(OnUnknownCommand); _cmdMessenger.Attach((int)Command.Acknowledge, OnAcknowledge); _cmdMessenger.Attach((int)Command.Error, OnError); _cmdMessenger.Attach((int)Command.TempChange, OnTempChange); _cmdMessenger.Attach((int)Command.SsrChange, OnSsrChange); _cmdMessenger.Attach((int)Command.HeaterChange, OnHeaterChange); _cmdMessenger.NewLineReceived += NewLineReceived; _cmdMessenger.NewLineSent += NewLineSent; IsConnected = _cmdMessenger.Connect(); if (IsConnected) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { _eventAggregator.GetEvent <ConnectionStatusEvent>().Publish(new ConnectionStatus { Type = ConnectionStatus.EventType.Connected }); }); RequestStatus(); } return(IsConnected); }
/// <summary> Receive command queue constructor. </summary> /// <param name="disposeStack"> DisposeStack. </param> /// <param name="cmdMessenger"> The command messenger. </param> public ReceiveCommandQueue(DisposeStack disposeStack, CmdMessenger cmdMessenger) : base(disposeStack, cmdMessenger) { disposeStack.Push(this); QueueThread.Name = "ReceiveCommandQueue"; // _queueSpeed.Name = "ReceiveCommandQueue"; }
// static SandR_mono() //{ } // Use this for initialization public void Start() { // Create Serial Port transport object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM9", BaudRate = 115200, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer // Set if it is communicating with a 16- or 32-bit Arduino board _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit16); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Start listening var Button1 = _cmdMessenger.Connect(); if (!Button1) { Debug.Log("No connection could be made"); return; } }
// ------------------ MAIN ---------------------- // Setup function public void Setup(ControllerForm controllerForm) { // storing the controller form for later reference _controllerForm = controllerForm; // Create Serial Port object _serialPortManager = new SerialPortManager { CurrentSerialSettings = { PortName = "COM6", BaudRate = 115200 } // object initializer }; _cmdMessenger = new CmdMessenger(_serialPortManager); // Tell CmdMessenger to "Invoke" commands on the thread running the WinForms UI _cmdMessenger.SetControlToInvokeOn(_controllerForm); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Attach to NewLineReceived for logging purposes _cmdMessenger.NewLineReceived += NewLineReceived; // Attach to NewLineSent for logging purposes _cmdMessenger.NewLineSent += NewLineSent; // Start listening _cmdMessenger.StartListening(); _controllerForm.SetLedState(true); _controllerForm.SetFrequency(2); }
/// <summary> /// Connection manager for Bluetooth devices /// </summary> public BluetoothConnectionManager(BluetoothTransport bluetoothTransport, CmdMessenger cmdMessenger, int challengeCommandId, int responseCommandId) : base(cmdMessenger, challengeCommandId, responseCommandId) { WatchdogTimeOut = 2000; WatchdogRetryTimeOut = 1000; MaxWatchdogTries = 3; if (bluetoothTransport == null) { return; } if (cmdMessenger == null) { return; } ControlToInvokeOn = null; _bluetoothTransport = bluetoothTransport; _bluetoothConfiguration = new BluetoothConfiguration(); ReadSettings(); _deviceList = new List <BluetoothDeviceInfo>(); _prevDeviceList = new List <BluetoothDeviceInfo>(); StartConnectionManager(); }
public void Dispose() { cmdMessenger.StopListening(); cmdMessenger.Dispose(); serialTransport.Dispose(); cmdMessenger = null; serialTransport = null; }
public CommandMessengerBroker(CmdMessenger cmdMessenger, MqttOptions mqttOptions, Encoding encoding = null) { CmdMessenger = cmdMessenger; MqttClient = mqttOptions.CreateMqttClient(); MqttClient.ConnectionClosed += (sender, e) => MqttConnectionClosed?.Invoke(sender, e); Encoding = encoding ?? Encoding.UTF8; }
public Action GoalTemperatureChanged; // Action that is called when the goal temperature has changed // Setup function public void Setup(ChartForm chartForm) { // getting the chart control on top of the chart form. _chartForm = chartForm; // Set up chart _chartForm.SetupChart(); // Connect slider to GoalTemperatureChanged GoalTemperatureChanged += () => _chartForm.GoalTemperatureTrackBarScroll(null, null); // Create Serial Port object // Note that for some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true. _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM6", BaudRate = 115200, DtrEnable = false } // object initializer }; // Initialize the command messenger with the Serial Port transport layer _cmdMessenger = new CmdMessenger(_serialTransport) { BoardType = BoardType.Bit16, // Set if it is communicating with a 16- or 32-bit Arduino board PrintLfCr = false // Do not print newLine at end of command, to reduce data being sent }; // Tell CmdMessenger to "Invoke" commands on the thread running the WinForms UI _cmdMessenger.SetControlToInvokeOn(chartForm); // Set command strategy to continuously to remove all commands on the receive queue that // are older than 1 sec. This makes sure that if data logging comes in faster that it can // be plotted, the graph will not start lagging _cmdMessenger.AddReceiveCommandStrategy(new StaleGeneralStrategy(1000)); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Attach to NewLinesReceived for logging purposes _cmdMessenger.NewLineReceived += NewLineReceived; // Attach to NewLineSent for logging purposes _cmdMessenger.NewLineSent += NewLineSent; // Start listening _cmdMessenger.StartListening(); // Send command to start sending data var command = new SendCommand((int)Command.StartLogging); // Send command _cmdMessenger.SendCommand(command); // Wait for a little bit and clear the receive queue Thread.Sleep(250); _cmdMessenger.ClearReceiveQueue(); // Set initial goal temperature GoalTemperature = 25; }
private void SendAll() { while (m_CommandQueue.TryDequeue(out SendCommand topicMessageCommand)) { CmdMessenger.SendCommand(topicMessageCommand); } CmdMessenger.SendCommand(new SendCommand((int)Commands.BatchDone)); }
public CommandMessengerSendBroker( CmdMessenger cmdMessenger, MqttOptions mqttOptions, string topicPrefix = "", string unknownCommandTopic = "unknown-cmd", Encoding encoding = null) : base(cmdMessenger, mqttOptions, encoding) { m_TopicPrefix = topicPrefix; m_UnknownCommandTopic = unknownCommandTopic; }
public void Connect() { // Connect MQTT clients ConnectMqttForever($"sender-{Guid.NewGuid()}"); // Attach Commands to CmdMessenger CmdMessenger.Attach(OnUnknownCommand); CmdMessenger.Attach((int)Commands.TopicMessage, OnTopicMessageCommand); }
public void SetUpConnection() { _cmdMessenger = Common.Connect(_systemSettings); AttachCommandCallBacks(); if (!Common.Connected) { Common.TestNotOk("Not connected after opening connection"); } }
public TcpConnectionManager(TcpTransport tcpTransport, CmdMessenger cmdMessenger, int identifyCommandId = 0, string uniqueDeviceId = null) : base(cmdMessenger, identifyCommandId, uniqueDeviceId) { DeviceScanEnabled = false; if (tcpTransport == null) throw new ArgumentNullException("tcpTransport", "Transport is null."); _transport = tcpTransport; }
protected virtual void MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { // This is just to wake up any sleeping device. CmdMessenger.SendCommand(new SendCommand((int)Commands.WakeUp)); System.Threading.Thread.Sleep(50); var topicMessageCommand = BuildCommandForMqttMessage(e); CmdMessenger.SendCommand(topicMessageCommand); }
/// <summary> /// Connection manager for serial port connection /// </summary> public SerialConnectionManager(SerialTransport serialTransport, CmdMessenger cmdMessenger, int watchdogCommandId = 0, string uniqueDeviceId = null, ISerialConnectionStorer serialConnectionStorer = null) : base(cmdMessenger, watchdogCommandId, uniqueDeviceId) { if (serialTransport == null) throw new ArgumentNullException("serialTransport", "Transport is null."); _serialTransport = serialTransport; _serialConnectionStorer = serialConnectionStorer; PersistentSettings = (_serialConnectionStorer != null); DeviceScanBaudRateSelection = true; UpdateAvailablePorts(); _serialConnectionManagerSettings = new SerialConnectionManagerSettings(); ReadSettings(); }
/// <summary> /// Connection manager for Bluetooth devices /// </summary> public BluetoothConnectionManager(BluetoothTransport bluetoothTransport, CmdMessenger cmdMessenger, int watchdogCommandId = 0, string uniqueDeviceId = null, IBluetoothConnectionStorer bluetoothConnectionStorer = null) : base(cmdMessenger, watchdogCommandId, uniqueDeviceId) { if (bluetoothTransport == null) throw new ArgumentNullException("bluetoothTransport", "Transport is null."); _bluetoothTransport = bluetoothTransport; _bluetoothConnectionManagerSettings = new BluetoothConnectionManagerSettings(); _bluetoothConnectionStorer = bluetoothConnectionStorer; PersistentSettings = (_bluetoothConnectionStorer != null); ReadSettings(); _deviceList = new List<BluetoothDeviceInfo>(); _prevDeviceList = new List<BluetoothDeviceInfo>(); DevicePins = new Dictionary<string, string>(); GeneralPins = new List<string>(); }
/// <summary> send command queue constructor. </summary> /// <param name="disposeStack"> DisposeStack. </param> /// <param name="cmdMessenger"> The command messenger. </param> public SendCommandQueue(DisposeStack disposeStack, CmdMessenger cmdMessenger) : base(disposeStack, cmdMessenger) { QueueThread.Name = "SendCommandQueue"; // _queueSpeed.Name = "SendCommandQueue"; }