本文整理汇总了C#中CommandMessenger.SendCommand类的典型用法代码示例。如果您正苦于以下问题:C# SendCommand类的具体用法?C# SendCommand怎么用?C# SendCommand使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SendCommand类属于CommandMessenger命名空间,在下文中一共展示了SendCommand类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Loop
// 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;
}
示例2: Setup
// Setup function
public void Setup()
{
_transport = new SerialTransport {CurrentSerialSettings = {DtrEnable = false}};
// some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true.
// We do not need to set serial port and baud rate: it will be found by the connection manager
// 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(_transport, BoardType.Bit16)
{
PrintLfCr = false // Do not print newLine at end of command, to reduce data being sent
};
// The Connection manager is capable or storing connection settings, in order to reconnect more quickly
// the next time the application is run. You can determine yourself where and how to store the settings
// by supplying a class, that implements ISerialConnectionStorer. For convenience, CmdMessenger provides
// simple binary file storage functionality
var serialConnectionStorer = new SerialConnectionStorer("SerialConnectionManagerSettings.cfg");
// We don't need to provide a handler for the Identify command - this is a job for Connection Manager.
_connectionManager = new SerialConnectionManager(
_transport as SerialTransport,
_cmdMessenger,
(int) Command.Identify,
CommunicationIdentifier,
serialConnectionStorer)
{
// Enable watchdog functionality.
WatchdogEnabled = true,
// Instead of scanning for the connected port, you can disable scanning and only try the port set in CurrentSerialSettings
//DeviceScanEnabled = false
};
// Show all connection progress on command line
_connectionManager.Progress += (sender, eventArgs) =>
{
// If you want to reduce verbosity, you can only show events of level <=2 or ==1
if (eventArgs.Level <= 3) Console.WriteLine(eventArgs.Description);
};
// If connection found, tell the arduino to turn the (internal) led on
_connectionManager.ConnectionFound += (sender, eventArgs) =>
{
// Create command
var command = new SendCommand((int)Command.TurnLedOn);
// Send command
_cmdMessenger.SendCommand(command);
};
//You can also do something when the connection is lost
_connectionManager.ConnectionTimeout += (sender, eventArgs) =>
{
//Do something
};
// Finally - activate connection manager
_connectionManager.StartConnectionManager();
}
示例3: SetLedState
// 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));
}
示例4: SetLedFrequency
public void SetLedFrequency(double ledFrequency)
{
// Send command to start sending data
var command = new SendCommand((int)Command.SetLedFrequency,ledFrequency);
// Send command
_cmdMessenger.SendCommand(command);
}
示例5: Loop
// 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
}
示例6: Send
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()));
}
}
示例7: SetLedFrequency
// Sent command to change led blinking frequency
public void SetLedFrequency(double ledFrequency)
{
// Create command to start sending data
var command = new SendCommand((int)Command.SetLedFrequency,ledFrequency);
// Put the command on the queue and wrap it in a collapse command strategy
// This strategy will avoid duplicates of this certain command on the queue: if a SetLedFrequency command is
// already on the queue when a new one is added, it will be replaced at its current queue-position.
// Otherwise the command will be added to the back of the queue.
//
// This will make sure that when the slider raises a lot of events that each send a new blink frequency, the
// embedded controller will not start lagging.
_cmdMessenger.QueueCommand(new CollapseCommandStrategy(command));
}
示例8: Loop
// 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
}
示例9: ExecuteSendCommand
/// <summary> Directly executes the send command operation. </summary>
/// <param name="sendCommand"> The command to sent. </param>
/// <param name="sendQueueState"> Property to optionally clear the send and receive queues. </param>
/// <returns> A received command. The received command will only be valid if the ReqAc of the command is true. </returns>
public ReceivedCommand ExecuteSendCommand(SendCommand sendCommand, SendQueue sendQueueState)
{
// Disable listening, all callbacks are disabled until after command was sent
ReceivedCommand ackCommand;
lock (_sendCommandDataLock)
{
if (PrintLfCr)
_communicationManager.WriteLine(sendCommand.CommandString());
else
_communicationManager.Write(sendCommand.CommandString());
ackCommand = sendCommand.ReqAc ? BlockedTillReply(sendCommand.AckCmdId, sendCommand.Timeout, sendQueueState) : new ReceivedCommand();
}
return ackCommand;
}
示例10: Loop
// 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
if (_count > 100) RunLoop = false; // Stop loop after 100 rounds
}
示例11: Setup
// ------------------ MAIN ----------------------
// Setup function
public void Setup(ChartForm chartForm)
{
// getting the chart control on top of the chart form.
_chartForm = chartForm;
// Set up chart
_chartForm.SetupChart();
// 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);
// Tell CmdMessenger to "Invoke" commands on the thread running the WinForms UI
_cmdMessenger.ControlToInvokeOn = chartForm;
// Set Received command strategy that removes commands that are older than 1 sec
_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.Connect();
// Send command to start sending data
var command = new SendCommand((int)Command.StartLogging);
// Send command
_cmdMessenger.SendCommand(command);
}
示例12: Setup
// ------------------ MAIN ----------------------
// Setup function
public void Setup(ChartForm chartForm)
{
// getting the chart control on top of the chart form.
_chartForm = chartForm;
// Set up chart
_chartForm.SetupChart();
// 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 Received command strategy that removes commands that are older than 1 sec
_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);
}
示例13: Setup
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) { }
}
示例14: SetupQueuedSendSeries
// *** 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);
}
}
示例15: DirectSendSeries
// *** 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);
}
}