Myo 集线器初始化期间未触发调度程序

本文关键字:调度程序 集线器 初始化 Myo | 更新日期: 2023-09-27 18:36:51

我正在将 Myo 臂带的初始化代码移植到 WPF 应用程序,该应用程序使用 C# 包装器,http://goo.gl/HfwqQe 与设备交互。

但是,当我在用户控件代码后面的InitializeComponent();下添加初始化代码时,永远不会触发使用连接状态更新文本框的行,this.Dispatcher.Invoke((Action)(() =>

我通过在调度程序代码之前的行上设置断点来调试它,这被称为hub.MyoConnected += (sender, e) =>这意味着 Myo 已连接,但更新后的以下dispatcher行永远不会调用和跳过statusTbx

有人知道这里可能导致这种情况的原因吗?

我不确定为什么它不会将连接状态输出到文本框。以前的代码相同,但这是我正在使用的 C# 包装器的新版本。

控制台示例工作正常,http://goo.gl/RFHLym 并输出与控制台的连接,但我无法让它将连接输出到文本框。

这是获取 Myo 臂带连接状态的完整代码:

using MyoSharp.Communication;
using MyoSharp.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
namespace MyoTestv4
{
    /// <summary>
    /// Interaction logic for ProductsView.xaml
    /// </summary>
    public partial class AdductionAbductionFlexionView : UserControl
    {
        public AdductionAbductionFlexionView()
        {
            InitializeComponent();

            // create a hub that will manage Myo devices for us
            using (var channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create())))
            using (var hub = Hub.Create(channel))
            {
                 //set a bpoint here, gets triggered
                // listen for when the Myo connects
                hub.MyoConnected += (sender, e) =>
                {
                     //set a bpoint here, doesn't get triggered
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
                        //Console.WriteLine("Myo {0} has connected!", e.Myo.Handle);
                        e.Myo.Vibrate(VibrationType.Short);
                    }));
                };
                // listen for when the Myo disconnects
                hub.MyoDisconnected += (sender, e) =>
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        statusTbx.Text = "Myo has disconnected!";
                        //Console.WriteLine("Oh no! It looks like {0} arm Myo has disconnected!", e.Myo.Arm);
                        e.Myo.Vibrate(VibrationType.Medium);
                    }));
                };
                // start listening for Myo data
                channel.StartListening();
            }
        }
    }
}

Myo 集线器初始化期间未触发调度程序

您会收到此错误,因为调用后会立即释放channelhub

channel.StartListening();

using 是为您处置对象的便捷方法,在这种情况下,这是不需要的。有关详细信息,请参阅使用语句(C# 参考)。

请尝试以下步骤来解决问题。1. 将通道和集线器声明为类的私有字段。 2.不要使用using关键字。3.切记处理hubAdductionAbductionFlexionView处置时channel

public partial class AdductionAbductionFlexionView : UserControl
{
    IChannel channel;
    IHub hub;
    public AdductionAbductionFlexionView()
    {
        InitializeComponent();
        // create a hub that will manage Myo devices for us
        channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create()));
        hub = Hub.Create(channel);
        //set a bpoint here, gets triggered
        // listen for when the Myo connects
        hub.MyoConnected += (sender, e) =>
        {
            //set a bpoint here, doesn't get triggered
            this.Dispatcher.Invoke((Action)(() =>
            {
                statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
                //Console.WriteLine("Myo {0} has connected!", e.Myo.Handle);
                e.Myo.Vibrate(VibrationType.Short);
            }));
        };
        // listen for when the Myo disconnects
        hub.MyoDisconnected += (sender, e) =>
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                statusTbx.Text = "Myo has disconnected!";
                //Console.WriteLine("Oh no! It looks like {0} arm Myo has disconnected!", e.Myo.Arm);
                e.Myo.Vibrate(VibrationType.Medium);
            }));
        };
        // start listening for Myo data
        channel.StartListening();
    }
}