Reading multiple joysticks on C# using SlimDX.

Hello,

I am currently working on a scouting system for the upcoming FRC Season and I am having trouble getting more than one joystick to be read. The program receives data from the joysticks (like how many balls were attempted and made) then saves it to notepad. From notepad it can be read using Excel. The program works fine with one controller. Is there a way to do the same thing but with 6 different USB connected joysticks?

I’d be willing to look at your code. If you don’t want to post it publicly, PM me.

This should give you a starting place.


        public static Joystick] GetSticks()
        {
            var sticks = new List<SlimDX.DirectInput.Joystick>();
            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // create the device
                try
                {
                    var stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }

                    sticks.Add(stick);

                    Console.WriteLine(stick.Information.InstanceName);
                }
                catch (DirectInputException)
                {
                }
            }
            return sticks.ToArray();
        }

I will use that for the starting point thank you and here is the code if anyone is interested.



public Form1()
        {
            InitializeComponent();
            BootGamePort();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            BootGamePort();

        }

Joystick mainScouter; 
        JoystickState state = new JoystickState();

        void BootGamePort()
        {
            DirectInput dinput = new DirectInput();

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    if (count == 0)
                    {
                        mainScouter = new Joystick(dinput, device.InstanceGuid);
                    }

                    count = 1;
                    break;
                }

                catch (DirectInputException)
                {
                }

            }
            if (mainScouter == null)
            {
                return;
            }
            

            

            foreach (DeviceObjectInstance deviceObject in mainScouter.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    mainScouter.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
            }

            

            mainScouter.Acquire();
        }

        public void timer1_Tick(object sender, EventArgs e)
        {

            if (mainScouter.Acquire().IsFailure)
            {
                return;
            }

            if (mainScouter.Poll().IsFailure)
            {
                return;
            }

           

            state = mainScouter.GetCurrentState();
           

            if (Result.Last.IsFailure)
            {
                return;
            }

            bool] buttons = state.GetButtons();
           


            String strText = "";
            

            for (int b = 0; b < buttons.Length; b++)
            {
                if (buttons**)
                {
                    strText += b.ToString("00 ", CultureInfo.CurrentCulture);
                    lblbuttons.Text = strText;
                }

               
            }

**

I tested the code that EHaskins provided me and it can read every USB gamepad. I developed code so it can show which buttons are being pressed, but it only works if each gamepad is under a timer. The timer is used to refresh the labels which each button is assigned to change. The problem that I get in the code is in this line:

SlimDX.DirectInput.Joystick stick = new SlimDX.DirectInput.Joystick(Input. device.InstanceGuild);

I declared this line at the beginning of the code and it gives me an error with “device” and “Input”. The errors read: “A field initializer cannot the reference the non-static field, method, or property” for both. So my question is why isn’t Input and device being referenced?

First, shouldn’t that be a comma after Input you the code you posted. If that’s just a typo when copying to CD, there’s probably a problem on the line before or after it.

Second, you should do that with a single timer. Something like:


Joystick] sticks;
void init() {//pageload, or new, or something similar
  Sticks = GetSticks();//using my code from previous post
}
void TimerHandler (...){
  for (int i = 0; i < sticks.Length; i++){
    StickHandlingLogic(sticks*, i); //replace the identifier with something meaningful to your application.
  }
}
void StickhandlingLogic(Joystick stick, int id) { //pass the stick and some type of identifier, since you want to know which users stick this is
  //Process the joystick data here
}

Thanks for the help EHaskins. Now I am able to use all 6 gamepads that I need. Right now, whenever I press a certain button a counter on an integer goes up by one and is displayed on a label like this:

if (buttons[0])
{
    basketsAttempted++;
}

The problem I am facing now is that I want to be able to decrease the value of the integer by one when i push the left bumper and a certain button like this:

if (buttons[4] & buttons[0])
{
    basketsAttempted--;
}

If i try to use the same button and left bumper to decrease the value, it will not decrease. It only decreases if you use the left bumper and any other button but the one you used to increase it. Is there a way to use the same button and left bumper to decrease the value?

Try this. Either one is functionally the same, but I think the second may be more concise.


if (buttons[4] && buttons[0])
{
    basketsAttempted--;
}
**else** if (buttons[0])
{
    basketsAttempted++;
}

OR

if (buttons[0]){
  if (buttons4){
    basketsAttempted--;
  }
  else{
    basketsAttempted++;
  }
}

BTW, what was happening when you used the same button for both the add and subtract operation is that both were testing true, and canceling each other out. Effectively you were doing: x = x + 1 - 1;

Hey Eric Haskins,

i am trying to program the dance pad programmed for a Dancing Revolution Game, could any one tell how i can take the input from their button…?? because i tried using the serial port input but it didn’t worked out.

even i used this code but its not returning the data.

JoystickState state = new JoystickState();

// JoystickState state = joystick.CurrentJoystickState; ( If i use this and delete the above command then i get a error for Object reference not set to an instance of an object. ( NULL Reference Exception )

//Capture Buttons.
byte] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons* != 0)
{
info += “Button:” + i + " ";
label1.Text = info;

}
}*

Hey Eric whats app…??
i am trying to program the dance pad programmed for a Dancing Revolution Game, could any one tell how i can take the input from their button…?? because i tried using the serial port input but it didn’t worked out.

even i used this code but its not returning the data.

JoystickState state = new JoystickState();

// JoystickState state = joystick.CurrentJoystickState; ( If i use this and delete the above command then i get a error for Object reference not set to an instance of an object. ( NULL Reference Exception )

//Capture Buttons.
byte] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons* != 0)
{
info += “Button:” + i + " ";
label1.Text = info;

}
}*