0

我正在做一个项目,试图使用 wiimote 来控制机械臂。但首先我想在 c# 中测试 wiimote。基本上,我希望每次按下按钮时更新我的​​ wiimote,并有一个 GUI 界面显示在复选框列表中按下了哪个按钮。但是当我运行我的代码并按下一个按钮时,窗口窗体会关闭程序。我已经尝试了几个小时来解决这个问题,但我仍然卡住了。

Wiimote wm = new Wiimote();

public Wiimote1()
{
    InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel, true);
    wm.SetLEDs(true, false, false, false);
}

private void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs args)
{

    if (args.Inserted)
        wm.SetReportType(InputReport.IRExtensionAccel, true);
    else
        wm.SetReportType(InputReport.IRAccel, true);
}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    wm.Disconnect();
}

private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
{

    WiimoteState wmi = args.WiimoteState;

    buttonA = wmi.ButtonState.A;
    buttonB = wmi.ButtonState.B;
    buttonHome = wmi.ButtonState.Home;
    buttonOne = wmi.ButtonState.One;
    buttonTwo = wmi.ButtonState.Two;
    buttonPlus = wmi.ButtonState.Plus;
    buttonMinus = wmi.ButtonState.Minus;
    buttonUp = wmi.ButtonState.Up;
    buttonDown = wmi.ButtonState.Down;
    buttonLeft = wmi.ButtonState.Left;
    buttonRight = wmi.ButtonState.Right;
    AccelX = wmi.AccelState.Values.X.ToString();
    AccelY = wmi.AccelState.Values.Y.ToString();
    AccelZ = wmi.AccelState.Values.Z.ToString();

    switch (wmi.ExtensionType)
    {
        case ExtensionType.Nunchuk:
            nunC = wmi.NunchukState.C;
            nunZ = wmi.NunchukState.Z;
            nunAccelX = wmi.NunchukState.AccelState.Values.X.ToString();
            nunAccelY = wmi.NunchukState.AccelState.Values.Y.ToString();
            JoyStickX = wmi.NunchukState.Joystick.X.ToString();
            JoyStickY = wmi.NunchukState.Joystick.Y.ToString();
            break;

        case ExtensionType.ClassicController:
            clsA = wmi.ClassicControllerState.ButtonState.A;
            clsB = wmi.ClassicControllerState.ButtonState.B;
            clsX = wmi.ClassicControllerState.ButtonState.X;
            clsY = wmi.ClassicControllerState.ButtonState.Y;
            clsPlus = wmi.ClassicControllerState.ButtonState.Plus;
            clsMinus = wmi.ClassicControllerState.ButtonState.Minus;
            clsHome = wmi.ClassicControllerState.ButtonState.Home;
            clsUp = wmi.ClassicControllerState.ButtonState.Up;
            clsDown = wmi.ClassicControllerState.ButtonState.Down;
            clsLeft = wmi.ClassicControllerState.ButtonState.Left;
            clsRight = wmi.ClassicControllerState.ButtonState.Right;
            clsTriggerL = wmi.ClassicControllerState.ButtonState.TriggerL;
            clsTriggerR = wmi.ClassicControllerState.ButtonState.TriggerR;
            clsZL = wmi.ClassicControllerState.ButtonState.ZL;
            clsZR = wmi.ClassicControllerState.ButtonState.ZR;
            break;
    }

    checkList();

}


private void checkList()
{
    checkedListBox1.SetItemChecked(0, buttonA);
    checkedListBox1.SetItemChecked(1, buttonB);
    checkedListBox1.SetItemChecked(2, buttonMinus);
    checkedListBox1.SetItemChecked(3, buttonPlus);
    checkedListBox1.SetItemChecked(4, buttonHome);
    checkedListBox1.SetItemChecked(5, buttonOne);
    checkedListBox1.SetItemChecked(6, buttonTwo);
    checkedListBox1.SetItemChecked(7, buttonUp);
    checkedListBox1.SetItemChecked(8, buttonDown);
    checkedListBox1.SetItemChecked(9, buttonRight);
    checkedListBox1.SetItemChecked(10, buttonLeft);
}
4

2 回答 2

0

如果您使用的是 64 位 Windows,则在 Form_Load 事件中引发的异常会被无声地吞噬。也许将代码从 Form_Load 事件中移开是个好主意。这使得未来的调试更加容易。

public Wiimote1()
{
    InitializeComponent();
    InitWiimote();
}


private void InitWiimote()
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel, true);
    wm.SetLEDs(true, false, false, false);
}
于 2013-02-13T06:49:54.167 回答
0

您会发现在代码中添加异常处理时更容易找出错误。抬头try...catch

如果捕获的异常对您没有意义,您可以在此处提出相关问题。

于 2011-11-10T08:02:55.083 回答