当我按下按钮时,wiimote程序关闭

时间:2011-11-10 03:20:26

标签: c# wiimote

我正在尝试使用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);
}

2 个答案:

答案 0 :(得分:0)

您发现在向代码添加异常处理时更容易找出错误。查看try...catch

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

答案 1 :(得分: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);
}