// 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) { } }
/// <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; }
public bool sendSamsung(String value) { if (!setupDone) { return false; } var command = new SendCommand((int)Command.SendSamsung, (int)Command.SendSamsungResponse, Globals.arguments().timeout); command.AddArgument (value); _cmdMessenger.SendCommand(command); var responseCommand = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (responseCommand.Ok) { OnSendSamsungResponse(responseCommand, value); return true; } else { listener.errorTimedOut(); return false; } }
public bool sendLEDBlink(int ledIndex, int blinkMillis, int repetitions) { if (!setupDone) { return false; } var command = new SendCommand((int)Command.LEDBlink, (int)Command.LEDBlinkResponse, Globals.arguments().timeout); command.AddArgument (ledIndex); command.AddArgument (blinkMillis); command.AddArgument (repetitions); _cmdMessenger.SendCommand(command); var responseCommand = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (responseCommand.Ok) { OnLEDBlinkResponse(responseCommand); return true; } else { listener.errorTimedOut(); return 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 = "COM15", BaudRate = 115200 } // 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 _cmdMessenger.Connect(); _receivedItemsCount = 0; _receivedBytesCount = 0; // Clear queues _cmdMessenger.ClearReceiveQueue(); _cmdMessenger.ClearSendQueue(); Thread.Sleep(100); // Send command requesting a series of 100 float values send in plain text form var commandPlainText = new SendCommand((int)Command.RequestPlainTextFloatSeries); commandPlainText.AddArgument((UInt16)SeriesLength); commandPlainText.AddArgument((float)SeriesBase); // Send command _cmdMessenger.SendCommand(commandPlainText); // Now wait until all values have arrived while (!_receivePlainTextFloatSeriesFinished) { Thread.Sleep(100); } // Clear queues _cmdMessenger.ClearReceiveQueue(); _cmdMessenger.ClearSendQueue(); _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) { Thread.Sleep(100); } }
public bool sendLEDOnRange(int ledStartIndex, int ledEndIndex, int millisToKeepOn) { if (!setupDone) { return false; } var command = new SendCommand((int)Command.LEDOnRange, (int)Command.LEDOnRangeResponse, Globals.arguments().timeout); command.AddArgument(ledStartIndex); command.AddArgument(ledEndIndex); command.AddArgument(millisToKeepOn); _cmdMessenger.SendCommand(command); var responseCommand = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (responseCommand.Ok) { OnLEDOnRangeResponse(responseCommand); return true; } else { listener.errorTimedOut(); return false; } }
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); }
// *** 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); } }
public bool MoveMotor(short Channel, short Speed) { if (!Initialized) throw new Exception("Not yet initialized"); var command = new SendCommand((int)Command.MotorCommand, (int)Command.Acknowledge, 1000); command.AddArgument(Channel); command.AddArgument(Speed); var response = _cmdMessenger.SendCommand(command); if (response.Ok) { var cn = response.ReadInt16Arg(); var cs = response.ReadInt16Arg(); Debug.WriteLine(String.Format("Command response - channel={0}; speed={1}", cn, cs)); if (Channel == cn && Speed == cs) { Debug.WriteLine("Alles gut"); //OnChannelStateConfirmation(new ChannelStatus(cn, cs)); return true; } } return false; }
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); }
/// <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> /// Reads analog pins. /// </summary> /// <returns>The analog pin.</returns> /// <param name="nr">Nr.</param> public static double[] ReadAnalogPin (uint[] nr) { var command = new SendCommand ((int)Command.ReadAnalogPin, (int)Command.ReadAnalogPin, 100); command.AddArgument (nr.Length); foreach (int i in nr) { command.AddArgument (i); } double[] results = new double[nr.Length]; var result = _cmdMessenger.SendCommand (command); if (result.Ok) { try { for (int i = 0; i < nr.Length; i++) { results [i] = Board.RAWToVolt (result.ReadFloatArg ()); } } catch (Exception ex) { Console.Error.WriteLine (ex); } LastCommunication = DateTime.Now; return results; } else { for (int i = 0; i < results.Length; i++) { results [i] = double.NaN; } return results; } }
/// <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. /// </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 }
/// <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; } }
/// <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); }
/// <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 }
public bool Switch(short ChannelName, bool state) { if (!Initialized) throw new Exception("Not yet initialized"); OnChannelStateRequest(new ChannelStatus(ChannelName, state)); var command = new SendCommand((int)Command.SwitchChannel,(int)Command.Acknowledge,1000); command.AddArgument(ChannelName); command.AddArgument(state); var response = _cmdMessenger.SendCommand(command); if (response.Ok) { var cn = response.ReadInt16Arg(); var cs = response.ReadBoolArg(); Debug.WriteLine(String.Format("Command response - channelNumber={0}; channelState={1}", cn,cs)); if (ChannelName == cn && state == cs) { OnChannelStateConfirmation(new ChannelStatus(cn, cs)); return true; } } return false; }
private void ValuePingPongBinInt16(Int16 value, Int16 accuracy) { var pingCommand = new SendCommand(_command["ValuePing"], _command["ValuePong"], 1000); pingCommand.AddArgument((Int16)DataType.BInt16); pingCommand.AddBinArgument(value); var pongCommand = _cmdMessenger.SendCommand(pingCommand); if (!pongCommand.Ok) Common.TestNotOk("No response on ValuePing command"); var result = pongCommand.ReadBinInt16Arg(); var difference = Math.Abs(result - value); if (difference <= accuracy) Common.TestOk("Value as expected"); else Common.TestNotOk("unexpected value received: " + result + " instead of " + value); }
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 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); }
// *** Benchmark 3 *** private void DirectSendSeries() { Common.StartTest("Calculating speed in individually sending a series of float data"); WaitAndClear(); _minimalBps = _systemSettings.MinDirectSendSpeed; _sendSeriesFinished = false; var prepareSendSeries = new SendCommand(_command["PrepareSendSeries"]); prepareSendSeries.AddArgument(SeriesLength); // We need to to send the prepareSendSeries by bypassing the queue or it might be sent after the directly send commands later on _cmdMessenger.SendCommand(prepareSendSeries, SendQueue.WaitForEmptyQueue, ReceiveQueue.WaitForEmptyQueue,UseQueue.BypassQueue); // Prepare _receivedBytesCount = 0; _cmdMessenger.PrintLfCr = true; _beginTime = Millis; // Now send all commands individually and bypass the queue for (var sendItemsCount = 0; sendItemsCount < SeriesLength; sendItemsCount++) { var sendSeries = new SendCommand(_command["SendSeries"]); sendSeries.AddArgument(sendItemsCount * SeriesBase); _receivedBytesCount += CountBytesInCommand(sendSeries, _cmdMessenger.PrintLfCr); _cmdMessenger.SendCommand(sendSeries, SendQueue.Default, ReceiveQueue.Default, UseQueue.BypassQueue); if (sendItemsCount%(SeriesLength/10) == 0) { Common.WriteLine("Send value: " + sendItemsCount*SeriesBase); } } _endTime = Millis; // 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); } }
// *** 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(10); } }
public bool sendLEDOnForMillis(int ledIndex, int specialcommandparameter) { if (!setupDone) { return false; } var command = new SendCommand((int)Command.LEDOnForMillis, (int)Command.LEDOnForMillisResponse, Globals.arguments().timeout); command.AddArgument (ledIndex); command.AddArgument (specialcommandparameter); _cmdMessenger.SendCommand(command); var responseCommand = _cmdMessenger.SendCommand(command); // Check if received a (valid) response if (responseCommand.Ok) { OnLEDOnForMillisResponse(responseCommand); return true; } else { listener.errorTimedOut(); return false; } }