如何使用c ++通过蓝牙将字节命令发送到lego mindstorms EV3的串口?

时间:2015-12-22 02:02:42

标签: c# bluetooth nxt lego mindstorms

使用C ++,我如何从没有运行lego软件的机器向EV3 / nxt发送命令?我想控制我心灵风暴机器人的向前,向后,向左和向右方向。我可以加载砖来响应某些字节命令,然后通过蓝牙从那台不同的机器发送那些字节命令吗?

任何建议都将不胜感激,谢谢!

以下是我在EV3上运行的一些c#代码。我只想弄清楚如何将相同的功能加载到我的砖块上,然后通过一些字节命令触发它,如documentation中的那些字节命令。

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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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();
        }
    }
}

0 个答案:

没有答案