title: How can I send byte commands via bluetooth to the serial port of a lego mindstorms EV3 using c++?
body: <p>Using C++, how would I go about sending commands to my EV3/nxt from a machine that isn't running lego software? I'd like to control the forward, backward, left, and right directions of my mindstorms robot. Can I load the brick to respond to certain byte commands and then just send those byte commands via bluetooth from that different machine?</p>
  
  <p>Any advice would be appreciated, thanks!</p>
  
  <p>Here is some c# code I'm running on my EV3. I'm just trying to figure out how I could load this same functionality onto my brick and then trigger it via some byte commands like those in this <a href="http://ev3.fantastic.computer/doxygen/directcommands.html" rel="nofollow">documentation</a>.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using Lego.Ev3.Core;
  using Lego.Ev3.Desktop;
  using System.Diagnostics;
  
  
  namespace legoAPIDemo
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          Brick _brick;
          int _forward = 40;
          int _backward = -30;
          uint _time = 300;
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private async void Window_Loaded(object sender, RoutedEventArgs e)
          {
              _brick = new Brick(new BluetoothCommunication("COM3"));
              _brick.BrickChanged += _brick_BrickChanged;
              await _brick.ConnectAsync();
              await _brick.DirectCommand.PlayToneAsync(100, 1000, 300);
              await _brick.DirectCommand.SetMotorPolarity(OutputPort.B | OutputPort.C, Polarity.Backward);
              await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
          }
  
          private void _brick_BrickChanged(object sender, BrickChangedEventArgs e)
          {
              Debug.WriteLine("BRICK CHANGED!"); 
          }
  
          private async void ForwardButton_Click(object sender, RoutedEventArgs e)
          {
              await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _forward, _time, false);
          }
  
          private async void BackButton_Click(object sender, RoutedEventArgs e)
          {
              await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _backward, _time, false);
          }
  
          private async void LeftButton_Click(object sender, RoutedEventArgs e)
          {
              _brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.B, _backward, _time, false);
              _brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, _forward, _time, false);
              await _brick.BatchCommand.SendCommandAsync();
          }
  
          private async void RightButton_Click(object sender, RoutedEventArgs e)
          { 
              _brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, _forward, _time, false);
              _brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.B, _backward, _time, false);
              await _brick.BatchCommand.SendCommandAsync();
          }
      }
  }
  </code></pre>