connecting joystick to cBot

Created at 09 Mar 2023, 13:16
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
YA

yaghouti

Joined 19.12.2019

connecting joystick to cBot
09 Mar 2023, 13:16


hi

i want to read joystick inputs in cbot. i used SharpDX.DirectInput class and everything in compilation stage looks ok but unfortunately when i added this to chart i get this strange error:

09/03/2023 13:27:29.504 | CBot instance [Sample Hotkey Trading, AUDUSD, h1] crashed with error "Crashed in OnStart with SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

please help me how can i solve this problem or how can i read joystick in cbot in easier way.

thanks


@yaghouti
Replies

pick
09 Mar 2023, 13:20

Does your bot have full access rights?

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class YourBot : Robot
{

 


@pick

yaghouti
09 Mar 2023, 14:08 ( Updated at: 09 Mar 2023, 14:14 )

RE:

pick said:

Does your bot have full access rights?

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class YourBot : Robot
{

 

thanks alot, i changed the accessright to fullaccess and that problem solved but unfortunately i get new erro:

09/03/2023 15:36:36.527 | CBot instance [Sample Hotkey Trading, AUDUSD, h1] crashed with error "Crashed in OnStart with SharpDXException: HRESULT: [0x80040154], Module: [SharpDX.DirectInput], ApiCode: [DIERR_DEVICENOTREG/DeviceNotRegistered], Message: Class not registered "

 

 

error refers to this part of code:

 protected override void OnStart()
        {

            
            directInput = new DirectInput();
            var joystickGuid = Guid.Empty;
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
            }

            joystick = new Joystick(directInput, joystickGuid);/////////////////////<<<<<<<<<<here is the error
            joystick.Properties.BufferSize = 128;
            joystick. Acquire();

}


@yaghouti

pick
09 Mar 2023, 14:23

RE: RE:

yaghouti said:

pick said:

Does your bot have full access rights?

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class YourBot : Robot
{

 

thanks alot, i changed the accessright to fullaccess and that problem solved but unfortunately i get new erro:

09/03/2023 15:36:36.527 | CBot instance [Sample Hotkey Trading, AUDUSD, h1] crashed with error "Crashed in OnStart with SharpDXException: HRESULT: [0x80040154], Module: [SharpDX.DirectInput], ApiCode: [DIERR_DEVICENOTREG/DeviceNotRegistered], Message: Class not registered "

 

 

error refers to this part of code:

 protected override void OnStart()
        {

            
            directInput = new DirectInput();
            var joystickGuid = Guid.Empty;
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
            }

            joystick = new Joystick(directInput, joystickGuid);/////////////////////<<<<<<<<<<here is the error
            joystick.Properties.BufferSize = 128;
            joystick. Acquire();

}

Is your "joystickGuid" variable still empty after that for loop? I have not used this library before, but that's what I would check first.

A quick search found this, if you look here, they are searching for DeviceType.Joystick if there is no result for DeviceType.Gamepad


@pick

yaghouti
09 Mar 2023, 15:54

RE: RE: RE:

pick said:

yaghouti said:

pick said:

Does your bot have full access rights?

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class YourBot : Robot
{

 

thanks alot, i changed the accessright to fullaccess and that problem solved but unfortunately i get new erro:

09/03/2023 15:36:36.527 | CBot instance [Sample Hotkey Trading, AUDUSD, h1] crashed with error "Crashed in OnStart with SharpDXException: HRESULT: [0x80040154], Module: [SharpDX.DirectInput], ApiCode: [DIERR_DEVICENOTREG/DeviceNotRegistered], Message: Class not registered "

 

 

error refers to this part of code:

 protected override void OnStart()
        {

            
            directInput = new DirectInput();
            var joystickGuid = Guid.Empty;
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
            }

            joystick = new Joystick(directInput, joystickGuid);/////////////////////<<<<<<<<<<here is the error
            joystick.Properties.BufferSize = 128;
            joystick. Acquire();

}

Is your "joystickGuid" variable still empty after that for loop? I have not used this library before, but that's what I would check first.

A quick search found this, if you look here, they are searching for DeviceType.Joystick if there is no result for DeviceType.Gamepad

thank you very much for your guide.

the problem solved and i can read the joystick keys in the cbot but one last question, i use onTick to trigger the key press which is not suitable, i want to get trigger as soon as user press any key on the joystick, how can i solve this problem?


@yaghouti

pick
09 Mar 2023, 16:39

RE: RE: RE: RE:

yaghouti said:

pick said:

yaghouti said:

pick said:

Does your bot have full access rights?

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class YourBot : Robot
{

 

thanks alot, i changed the accessright to fullaccess and that problem solved but unfortunately i get new erro:

09/03/2023 15:36:36.527 | CBot instance [Sample Hotkey Trading, AUDUSD, h1] crashed with error "Crashed in OnStart with SharpDXException: HRESULT: [0x80040154], Module: [SharpDX.DirectInput], ApiCode: [DIERR_DEVICENOTREG/DeviceNotRegistered], Message: Class not registered "

 

 

error refers to this part of code:

 protected override void OnStart()
        {

            
            directInput = new DirectInput();
            var joystickGuid = Guid.Empty;
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
            }

            joystick = new Joystick(directInput, joystickGuid);/////////////////////<<<<<<<<<<here is the error
            joystick.Properties.BufferSize = 128;
            joystick. Acquire();

}

Is your "joystickGuid" variable still empty after that for loop? I have not used this library before, but that's what I would check first.

A quick search found this, if you look here, they are searching for DeviceType.Joystick if there is no result for DeviceType.Gamepad

thank you very much for your guide.

the problem solved and i can read the joystick keys in the cbot but one last question, i use onTick to trigger the key press which is not suitable, i want to get trigger as soon as user press any key on the joystick, how can i solve this problem?

I'm not familiar with the library - are there any events that you can consume for a key press? If not, you could always query it on a timer.


@pick