private void ValuePingPongBool(bool value) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.Bool); pingCommand.AddArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var result = pongCommand.ReadBoolArg(); if (result == value) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
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); }
private void ValuePingPongDoubleSci(double value, double accuracy) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.DoubleSci); pingCommand.AddArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var result = pongCommand.ReadDoubleArg(); var difference = RelativeError(value, result); if (difference <= accuracy) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
// 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; }
public void Update(int data) { string dataString = ""; if (data == -9999) { for (int i = 0; i < _length; i++) { dataString += " "; } } else { dataString = CalculateData(data); } if (_data == dataString) { return; } _data = dataString; // Prepare command var command = new SendCommand((byte)EnumOperationTypes.DisplayOutput); command.AddArgument(_id); command.AddArgument((byte)_dataType); command.AddArgument(dataString); var result = _cmdMessenger.SendCommand(command); Log.Info("Sending display update. Data Type: " + _dataType.ToString() + ", Value: " + _data + ", Result: " + result.Ok); }
// *** Benchmark 1 *** private void SetupReceiveSeries() { Common.StartTest("Calculating speed in receiving series of float data"); WaitAndClear(); _receiveSeriesFinished = false; _receivedItemsCount = 0; _receivedBytesCount = 0; _minimalBps = _systemSettings.MinReceiveSpeed; // Send command requesting a series of 100 float values send in plain text form var commandPlainText = new SendCommand(_command["RequestSeries"]); commandPlainText.AddArgument(SeriesLength); commandPlainText.AddArgument(SeriesBase); // Send command _cmdMessenger.SendCommand(commandPlainText); // Now wait until all values have arrived while (!_receiveSeriesFinished) { Thread.Sleep(100); } }
private void ValuePingPongFloat(float value) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.Float); pingCommand.AddArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var result = pongCommand.ReadFloatArg(); var difference = Math.Abs(result - value); var accuracy = Math.Abs(value * 2e-7); if (difference <= accuracy) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
private void SetPinMode(NamedPin pin, PinMode mode) { Logger.Log("Sending SetPinMode( Pin={0}, PinMode={1} )", pin, mode); var c = new SendCommand((int)Command.SetPinMode); c.AddArgument(pin.Pin); c.AddArgument((int)mode); _messenger.SendCommand(c); }
public void SetPin(NamedPin pin, bool state) { Logger.Log("Sending SetPin( Pin={0}, State={1} )", pin, state); var c = new SendCommand((int)Command.SetPin); c.AddArgument(pin.Pin); c.AddArgument(state); _messenger.SendCommand(c); }
public void SetTarget(byte stepperMotorIndex, ushort targetSteps) { var command = new SendCommand((int)Command.SetTarget); command.AddArgument(stepperMotorIndex); command.AddArgument(targetSteps); 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); }
public void ToggleLED(int index, bool signal) { // Create command var command = new SendCommand((int)Command.setLED); // Send command command.AddArgument(index); command.AddArgument(signal); _cmdMessenger.SendCommand(command); }
public void ExecuteKettleCommand(KettleCommand kettleCommand) { Debug.WriteLine($"ExecuteKettleCommand: {kettleCommand.Index} {kettleCommand.Percentage}"); Task.Run(() => { var kettleRequest = new SendCommand((int)Command.KettleRequest, (int)Command.KettleResult, 2000); kettleRequest.AddArgument(kettleCommand.Index); kettleRequest.AddArgument(kettleCommand.Percentage); var kettleResultCommand = _cmdMessenger.SendCommand(kettleRequest); var success = kettleRequest.Ok; }); }
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) { } }
/// <summary> /// Sets the state of the pin. /// </summary> /// <param name="nr">Nr.</param> /// <param name="state">State.</param> public static void SetPinState(uint nr, DPinState state) { var command = new SendCommand((int)Command.SetPinState, (int)Command.SetPinState, 50); command.AddArgument(nr); command.AddArgument((Int16)state); var ret = _cmdMessenger.SendCommand(command); if (!ret.Ok) { Console.Error.WriteLine("SetPinState " + nr + " " + state + " failed"); LastCommunication = DateTime.Now; } }
// 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"); }
// *** Benchmark 2 *** // Setup queued send series private void SetupQueuedSendSeries() { Common.StartTest("Calculating speed in sending queued series of float data"); WaitAndClear(); _minimalBps = _systemSettings.MinSendSpeed; _sendSeriesFinished = false; var prepareSendSeries = new SendCommand(_command["PrepareSendSeries"]); prepareSendSeries.AddArgument(SeriesLength); _cmdMessenger.SendCommand(prepareSendSeries, SendQueue.WaitForEmptyQueue, ReceiveQueue.WaitForEmptyQueue); // Prepare _receivedBytesCount = 0; _cmdMessenger.PrintLfCr = true; _beginTime = Millis; // Now queue all commands for (var sendItemsCount = 0; sendItemsCount < SeriesLength; sendItemsCount++) { var sendSeries = new SendCommand(_command["SendSeries"]); sendSeries.AddArgument(sendItemsCount * SeriesBase); _receivedBytesCount += CountBytesInCommand(sendSeries, _cmdMessenger.PrintLfCr); _cmdMessenger.QueueCommand(sendSeries); if (sendItemsCount % (SeriesLength / 10) == 0) Common.WriteLine("Send value: " + sendItemsCount * SeriesBase); } // Now wait until receiving party acknowledges that values have arrived while (!_sendSeriesFinished) { Thread.Sleep(10); } }
private void ValuePingPongEscString(string value) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((int)DataType.EscString); pingCommand.AddBinArgument(value); // Adding a string as binary command will escape it var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var result = pongCommand.ReadBinStringArg(); if (value == result) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
private void ValuePingPongBinDouble(double value) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.BDouble); pingCommand.AddBinArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) { Common.TestNotOk("No response on ValuePing command"); return; } var result = pongCommand.ReadBinDoubleArg(); var difference = Math.Abs(result - value); // // For 16bit, because of double-float-float-double casting a small error is introduced var accuracy = (_systemSettings.BoardType == BoardType.Bit32) ? double.Epsilon : Math.Abs(value * 1e-6); if (difference <= accuracy) { Common.TestOk("Value as expected"); } else { Common.TestNotOk("unexpected value received: " + result + " instead of " + value); } }
/// <summary> /// Sets the analog pin. /// </summary> /// <param name="Pin">Pin.</param> /// <param name="Val">Value.</param> public static void SetAnalogPin(int Pin, int Val) { var command = new SendCommand((int)Command.SetAnalogPin, Pin); command.AddArgument(Val); _cmdMessenger.SendCommand(command); }
/// <summary> /// Sets the analog reference voltage. /// </summary> /// <param name="AnalogReference">Analog reference.</param> public static void SetAnalogReference(int AnalogReference) { _board.AnalogReferenceVoltage = AnalogReference; var command = new SendCommand((int)Command.SetAnalogReference); command.AddArgument(AnalogReference); _cmdMessenger.SendCommand(command); }
/// <summary> /// Sets the pin mode. /// </summary> /// <param name="nr">Nr.</param> /// <param name="mode">Mode.</param> public static void SetPinMode(uint nr, PinMode mode) { #if !FAKESERIAL var command = new SendCommand((int)Command.SetPinMode, nr); command.AddArgument((Int16)mode); _cmdMessenger.SendCommand(command); #endif }
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 void SendPrintCommand(UInt16 box, UInt16 to_feed) { Console.WriteLine("Sending Print command to box {0} and feed {1}", box, to_feed); // Send the command, wait for 1s for the the acknowledge command SendCommand command = new SendCommand((int)Command.Print, (int)Command.AcknowledgePrint, 1000); command.AddArgument(box); command.AddArgument(to_feed); ReceivedCommand rec_command = _cmdMessenger.SendCommand(command); var unique_id = rec_command.ReadUInt16Arg(); box = rec_command.ReadUInt16Arg(); to_feed = rec_command.ReadUInt16Arg(); Console.WriteLine("Acknowledge Print command with unique id {0} to box {1} and feed {2}", unique_id, box, to_feed); }
/// <summary> /// Sets the pin. /// </summary> /// <param name="nr">Nr.</param> /// <param name="mode">Mode.</param> /// <param name="state">State.</param> public static void SetPin(uint nr, PinMode mode, DPinState state) { #if !FAKESERIAL var command = new SendCommand((int)Command.SetPin, (int)Command.SetPin, 100); command.AddArgument((UInt16)nr); command.AddArgument((Int16)mode); command.AddArgument((Int16)state); var ret = _cmdMessenger.SendCommand(command); if (ret.Ok) { if (!(nr == (uint)ret.ReadInt32Arg() && (Int16)mode == ret.ReadInt16Arg() && (Int16)state == ret.ReadInt16Arg())) { Console.Error.WriteLine(DateTime.Now.ToString("HH:mm:ss tt zz") + "\t" + nr + "\t" + mode + "\t" + state); } LastCommunication = DateTime.Now; } #endif }
public void Update(int data) { if (_data == data) { return; } _data = (byte)data; // Prepare command var command = new SendCommand((int)EnumOperationTypes.LedOutput); command.AddArgument(_id); command.AddArgument(_data); ReceivedCommand resultCommand = _cmdMessenger.SendCommand(command); if (!resultCommand.Ok) { // Log } }
/// <summary> /// Set the digital output pins. /// </summary> /// <param name="conditions">The conditions are packed as follows: conditions[0] == pins[0-15], conditions[1] = pins[16,23],...</param> public static void SetDigitalOutputPins(UInt16[] conditions) { var command = new SendCommand((int)Command.SetDigitalOutputPins); for (int i = 0; i < conditions.Length; i++) { command.AddArgument(conditions [i]); } _cmdMessenger.SendCommand(command); }
public void SetButtonColor(int index, Color color) { if (cmdMessenger == null) { return; } short r = (short)Mathf.RoundToInt(color.r * intensity); short g = (short)Mathf.RoundToInt(color.g * intensity); short b = (short)Mathf.RoundToInt(color.b * intensity); var command = new SendCommand((int)Command.SetButtonColorRequest); command.AddArgument((short)index); command.AddArgument(r); command.AddArgument(g); command.AddArgument(b); cmdMessenger.SendCommand(command); }
public void DoButton(int buttonIndex, int buttonState) { var command = new SendCommand((int)Command.kButton, (int)Command.kButtonState, 1000); command.AddArgument(buttonIndex); command.AddArgument(buttonState); // Send command var okcmd = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (okcmd.Ok) { Thread.Sleep(100); int index = okcmd.ReadInt16Arg(); int state = okcmd.ReadInt16Arg(); } else { throw new ApplicationException("No response from controller!"); } }
/// <summary> /// Reads DPin state. /// </summary> /// <returns>The pin.</returns> /// <param name="nr">Nr.</param> public static DPinState ReadDPinState(uint nr) { var command = new SendCommand((int)Command.ReadPin, (int)Command.ReadPin, 500); command.AddArgument(nr); var result = _cmdMessenger.SendCommand(command); if (result.Ok) { LastCommunication = DateTime.Now; return((result.ReadBinInt16Arg() == (int)DPinState.HIGH) ? DPinState.HIGH : DPinState.LOW); } return(DPinState.LOW); }