当我按下按钮时,遥控器程序就会关闭

本文关键字:程序 遥控器 按钮 | 更新日期: 2023-09-27 18:14:17

我正在做一个项目,试图用遥控器来控制机械臂。但首先我想测试一下c#中的遥控器。基本上我希望我的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);
}

当我按下按钮时,遥控器程序就会关闭

您会发现,在代码中添加异常处理后,找出错误要容易得多。查找try...catch

如果捕获的异常对您来说没有意义,您可以在这里询问有关异常的问题。

如果您使用的是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);
}