public void Ping() { var pingCommand = new SendCommand((int)Command.PingRequest, (int)Command.PingResult, 2000); var pingResultCommand = _cmdMessenger.SendCommand(pingCommand); IsConnected = pingResultCommand.Ok; }
public void SendSensorValue(ISensor sensor, int sensorIndex, int sensorAllWatchedCount) { string firstLine, secondLine; var sensorIndexString = string.Format("{0}/{1}", sensorIndex, sensorAllWatchedCount); firstLine = string.Format("{0}-{1}", sensor.Hardware.HardwareType.ShortName(), sensor.SensorType.ShortName()); firstLine = firstLine.PadRight(16); firstLine = firstLine.Substring(0, 16 - sensorIndexString.Length - 1);//our LCD has only 16 chars and we want the sensorIndexString to be at the end of the line firstLine += " " + sensorIndexString; var value = GetSensorFormatedValue(sensor); var sensorName = sensor.Name; if (sensor.Hardware.HardwareType == HardwareType.CPU && sensorName.StartsWith("CPU")) { sensorName = sensorName.Substring(4); } secondLine = sensorName.PadRight(16); secondLine = secondLine.Substring(0, 16 - value.Length - 1); secondLine += " " + value; var commandSend = new SendCommand((int)Command.pSendValues); commandSend.AddArgument(firstLine); commandSend.AddArgument(secondLine); _cmdMessenger.SendCommand(commandSend); }
// Sent command to change led on/of state public void SetLedState(bool ledState) { // Create command to start sending data //var command = new SendCommand((int)Command.SetLed, ledState); // Send command _cmdMessenger.SendCommand(new SendCommand((int)Command.SetLed, ledState)); }
public void SetLedFrequency(double ledFrequency) { // Send command to start sending data var command = new SendCommand((int)Command.SetLedFrequency, ledFrequency); // Send command _cmdMessenger.SendCommand(command); }
private void Rotate(int motorIndex, double angle) { // Create command var command = new SendCommand((int)Command.rotateAngle); // Send command command.AddArgument(motorIndex); command.AddArgument(angle); _cmdMessenger.SendCommand(command); }
// Set the start time on the embedded controller public void SetStartTime(float startTime) { var command = new SendCommand((int)Command.SetStartTime, (int)Command.Acknowledge, 500); command.AddBinArgument((float)startTime); // We place this command at the front of the queue in order to receive correctly timestamped data as soon as possible // Meanwhile, the data in the receivedQueue is cleared as these will contain the wrong timestamp _cmdMessenger.SendCommand(command, SendQueue.ClearQueue, ReceiveQueue.ClearQueue, UseQueue.BypassQueue); }
private void WaitAndClear() { var requestResetCommand = new SendCommand(_command["RequestReset"], _command["RequestResetAcknowledge"], 1000); var requestResetAcknowledge = _cmdMessenger.SendCommand(requestResetCommand, SendQueue.ClearQueue, ReceiveQueue.ClearQueue); Common.WriteLine(!requestResetAcknowledge.Ok ? "No Wait OK received" : "Wait received"); // Wait another second to see if Thread.Sleep(1000); // Clear queues again to be very sure _cmdMessenger.ClearReceiveQueue(); _cmdMessenger.ClearSendQueue(); }
private const float SeriesBase = 1111111.111111F; // Base of values to return: SeriesBase * (0..SeriesLength-1) // ------------------ M A I N ---------------------- // Setup function public void Setup() { // Create Serial Port object _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM6", BaudRate = 115200 } // 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(); // Start listening _cmdMessenger.Connect(); _receivedItemsCount = 0; _receivedBytesCount = 0; // Send command requesting a series of 100 float values send in plain text form var commandPlainText = new SendCommand((int)Command.RequestPlainTextFloatSeries); commandPlainText.AddArgument(SeriesLength); commandPlainText.AddArgument(SeriesBase); // Send command _cmdMessenger.SendCommand(commandPlainText); // Now wait until all values have arrived while (!_receivePlainTextFloatSeriesFinished) { } _receivedItemsCount = 0; _receivedBytesCount = 0; // Send command requesting a series of 100 float values send in binary form var commandBinary = new SendCommand((int)Command.RequestBinaryFloatSeries); commandBinary.AddBinArgument((UInt16)SeriesLength); commandBinary.AddBinArgument((float)SeriesBase); // Send command _cmdMessenger.SendCommand(commandBinary); // Now wait until all values have arrived while (!_receiveBinaryFloatSeriesFinished) { } }
// Signal the embedded controller to start sending temperature data. public void StartAcquisition() { // Send command to start sending data var command = new SendCommand((int)Command.StartLogging, (int)Command.Acknowledge, 5); // Wait for an acknowledgment that data is being sent. Clear both the receive queue until the acknowledgment is received var receivedCommand = _cmdMessenger.SendCommand(command, SendQueue.WaitForEmptyQueue, ReceiveQueue.ClearQueue); if (!receivedCommand.Ok) { Console.WriteLine(@" Failure > no OK received from controller"); } }
private void OnDisable() { for (int retry = 0; retry < 10; ++retry) { var command = new SendCommand((int)Command.TurnOffRequest, (int)Command.TurnOffResponse, 5); var result = cmdMessenger.SendCommand(command); if (result.Ok) { break; } } Close(); }
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); }
// Loop function public void Loop() { // Create command FloatAddition, which will wait for a return command FloatAdditionResult var command = new SendCommand((int)Command.FloatAddition, (int)Command.FloatAdditionResult, 1000); // Add 2 float command arguments var a = 3.14F; var b = 2.71F; command.AddArgument(a); command.AddArgument(b); // Send command var floatAdditionResultCommand = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (floatAdditionResultCommand.Ok) { // Read returned argument var sum = floatAdditionResultCommand.ReadFloatArg(); var diff = floatAdditionResultCommand.ReadFloatArg(); // Compare with sum of sent values var errorSum = sum - (a + b); var errorDiff = diff - (a - b); Console.WriteLine("Received sum {0}, difference of {1}", sum, diff); Console.WriteLine("with errors {0} and {1}, respectively", errorSum, errorDiff); } else Console.WriteLine("No response!"); // Stop running loop RunLoop = false; }
private const float SeriesBase = 1111111.111111F; // Base of values to return: SeriesBase * (0..SeriesLength-1) // ------------------ M A I N ---------------------- // Setup function public void Setup() { // Create Serial Port object _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM6", BaudRate = 115200 } // object initializer }; // Initialize the command messenger with the Serial Port transport layer _cmdMessenger = new CmdMessenger(_serialTransport); // Attach the callbacks to the Command Messenger AttachCommandCallBacks(); // Start listening _cmdMessenger.StartListening(); _receivedPlainTextCount = 0; // Send command requesting a series of 100 float values send in plain text var commandPlainText = new SendCommand((int)Command.RequestPlainTextFloatSeries); commandPlainText.AddArgument(SeriesLength); commandPlainText.AddArgument(SeriesBase); // Send command _cmdMessenger.SendCommand(commandPlainText); // Now wait until all values have arrived while (!_receivePlainTextFloatSeriesFinished) { } // Send command requesting a series of 100 float values send in plain text var commandBinary = new SendCommand((int)Command.RequestBinaryFloatSeries); commandBinary.AddBinArgument((UInt16)SeriesLength); commandBinary.AddBinArgument((Single)SeriesBase); // Send command _cmdMessenger.SendCommand(commandBinary); // Now wait until all values have arrived while (!_receiveBinaryFloatSeriesFinished) { } }
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; }
// --------------------------------- // --- Command Sender ---- // --------------------------------- /// <summary> /// Gets the number of leds available to effectively store the points. /// </summary> /// <returns>The led number.</returns> public int GetLedNumber() { var command = new SendCommand((int)Command.LedNumberRequest, (int)Command.LedNumberResponse, 1000); var response = _cmdMessenger.SendCommand(command); return(response.ReadInt32Arg()); }
private void SendAll() { while (m_CommandQueue.TryDequeue(out SendCommand topicMessageCommand)) { CmdMessenger.SendCommand(topicMessageCommand); } CmdMessenger.SendCommand(new SendCommand((int)Commands.BatchDone)); }
public void SetTarget(byte stepperMotorIndex, ushort targetSteps) { var command = new SendCommand((int)Command.SetTarget); command.AddArgument(stepperMotorIndex); command.AddArgument(targetSteps); cmdMessenger.SendCommand(command); }
public string Move(int mov) { tl.LogMessage("Transmitting", ""); SendCommand command = new SendCommand((int)Command.Move, (int)Command.Acknowledge, 2000); command.AddArgument(mov); rcmd = _cmdMessenger.SendCommand(command); if (rcmd.Ok) { tl.LogMessage("Transmit", "true"); System.Threading.Thread.Sleep(mov); } else { tl.LogMessage("Transmit", "false"); } return(status); }
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); }
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 += new Action(() => _chartForm.GoalTemperatureTrackBarScroll(null, null)); // Create Serial Port object _serialTransport = new SerialTransport { CurrentSerialSettings = { PortName = "COM6", BaudRate = 115200 } // object initializer }; // Initialize the command messenger with the Serial Port transport layer _cmdMessenger = new CmdMessenger(_serialTransport); // 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; }
// Loop function public void Loop() { // Create command var command = new SendCommand((int)Command.SetLed, _ledState); // Send command _cmdMessenger.SendCommand(command); // Wait for 1 second and repeat Thread.Sleep(1000); _ledState = !_ledState; // Toggle led state }
public void StartInitialization() { Initializing = true; Logger.Log("Initialization Started"); OnLog("+----- Initialization Started -----+"); Logger.Log("Sending Initialize()"); _messenger.SendCommand(new SendCommand((int)Command.Initialize)); }
private void ValuePingPongBinBool(bool value) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.BBool); pingCommand.AddBinArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var result = pongCommand.ReadBinBoolArg(); if (result == value) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
private void ValuePingPongInt16(Int16 value, Int16 accuracy) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.Int16); pingCommand.AddArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); } var result = pongCommand.ReadInt16Arg(); var difference = Math.Abs(result - value); if (difference <= accuracy) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
public override void Send(string message) { try { var cmd = new SendCommand((int)SerialCommand.SetLed, message); var recieve = cmdMessenger.SendCommand(cmd); OnSendSuccess(new MessageEventArgs(string.Format("Message Send Success: Id={0}, Message={1}", recieve.RawString, message))); } catch (Exception e) { OnSendFailure(new MessageEventArgs("Message Send Failure: " + e.ToString())); } }
// Loop function public void Loop() { _count++; // Create command var command = new SendCommand((int)Command.SetLed, _ledState); // Send command _cmdMessenger.SendCommand(command); // Wait for 1 second and repeat //Thread.Sleep(1000); //_ledState = !_ledState; // Toggle led state _ledState = true; /*Console.WriteLine("LED state: "); * Console.WriteLine(_ledState);*/ if (_count > 0) { RunLoop = false; // Stop loop after 2 rounds (On -> Off -> stop) } }
private void ValuePingPongBinInt16Int32Double(Int16 int16Value, Int32 int32Value, double doubleValue) { var pingCommand = new SendCommand(_command["MultiValuePing"], _command["MultiValuePong"], 1000); pingCommand.AddBinArgument(int16Value); pingCommand.AddBinArgument(int32Value); pingCommand.AddBinArgument(doubleValue); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var int16Result = pongCommand.ReadBinInt16Arg(); var int32Result = pongCommand.ReadBinInt32Arg(); var doubleResult = pongCommand.ReadBinDoubleArg(); if (int16Result == int16Value) { Common.TestOk("1st parameter value, Int16, as expected: " + int16Result); } else { Common.TestNotOk("unexpected 1st parameter value received: " + int16Result + " instead of " + int16Value); } if (int32Result == int32Value) { Common.TestOk("2nd parameter value, Int32, as expected: " + int32Result); } else { Common.TestNotOk("unexpected 2nd parameter value, Int32, received: " + int32Result + " instead of " + int32Value); } // For 16bit, because of double-float-float-double casting a small error is introduced var accuracy = (_systemSettings.BoardType == BoardType.Bit32) ? double.Epsilon : Math.Abs(doubleValue * 1e-6); var difference = Math.Abs(doubleResult - doubleValue); if (difference <= accuracy) { Common.TestOk("3rd parameter value, Double, as expected: " + doubleResult); } else { Common.TestNotOk("unexpected 3rd parameter value, Double, received: " + doubleResult + " instead of " + doubleValue); } }
// Loop function public void Loop() { // Create command var command = new SendCommand((int)Command.SetLed, _ledState); // Send command _cmdMessenger.SendCommand(command); Console.Write("Turning led "); Console.WriteLine(_ledState?"on":"off"); // Wait for 1 second and repeat Thread.Sleep(1000); _ledState = !_ledState; // Toggle led state }
// Loop function public void Loop() { for (int i = 0; i < 100; i++) { // Create command FloatAddition, which will wait for a return command FloatAdditionResult var command = new SendCommand((int)Command.FloatAddition, (int)Command.FloatAdditionResult, 1000); // Add 2 float command arguments var a = 3.14F; var b = 2.71F; command.AddArgument(a); command.AddArgument(b); // Send command var floatAdditionResultCommand = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (floatAdditionResultCommand.Ok) { // Read returned argument var sum = floatAdditionResultCommand.ReadFloatArg(); var diff = floatAdditionResultCommand.ReadFloatArg(); // Compare with sum of sent values var errorSum = sum - (a + b); var errorDiff = diff - (a - b); //Console.WriteLine("Received sum {0}, difference of {1}", sum, diff); ///Console.WriteLine("with errors {0} and {1}, respectively", errorSum, errorDiff); if (errorDiff < 1e-6 && errorSum < 1e-6) { Console.WriteLine("OK: " + i); } else { Console.WriteLine("FAILED: " + i); } } else { Console.WriteLine("No response!"); } //Thread.Sleep(1); } Console.WriteLine("FINISHED"); }
private ReceivedCommand InteractInternal(SendCommand command) { while (true) { var resultCommand = messenger.SendCommand(command); if (resultCommand.Ok) { return(resultCommand); } else { System.Console.WriteLine($"[{DateTime.Now.ToString("HH':'mm':'ss'.'fffffff")}] [SERIAL] !! NO RESPONSE!"); Update(); } } }