Reading /dev/input/js0 file - USB Gamepad Connection

ChrisH
2016-08-15
2020-08-18
  • ChrisH - 2016-08-15

    Hi All,

    I'd like to connect a USB game-pad and use it to control things in my application.
    What I've done so far:
    1. Installed: ```

    sudo apt-get install joystick

    2.Tested if i can see any inputs: ```
    
    jstest /dev/input/js0
    
    ``` Everything looked good.
    Response:
    

    Driver version is 2.1.0.
    Joystick (Gasia Co.,Ltd PS(R) Gamepad) has 7 axes (X, Y, Z, Rz, Hat0X, Hat0Y, (null))
    and 12 buttons (Trigger, ThumbBtn, ThumbBtn2, TopBtn, TopBtn2, PinkieBtn, BaseBtn, BaseBtn2, BaseBtn3, BaseBtn4, BaseBtn5, BaseBtn6).
    Testing ... (interrupt to exit)
    Axes:  0:     0  1:     0  2:     0  3:     0  4:     0  5:     0  6:-32767 Buttons:  0:off  1:off  2:off  3:off  4:off  5:off  6:off  7:off  8:off  9:off 10:off 11:off

    3. Had a look into the device file. Whenever i press a button a new line with 16 Bytes gets written. ```
    
    cat /dev/input/js0 | hexdump
    

    Response, Note the first seven digits are the address then the actual 16 Bytes of data follow.

    0000120 0d36 05c1 376c 0202 0d36 05c1 7fff 0302
    0000130 0d54 05c1 72cd 0202 0d68 05c1 7966 0202
    0000140 0d72 05c1 7d5c 0202 0d86 05c1 7fff 0202
    0000150 0dc2 05c1 0000 0202 0dc2 05c1 0000 0302
    

    So this looked very good as well. The structure in the device file isn't very difficult to reverse engineer so I thought I should be able to read this file in Codesys.

    1. Then i tried to read this file in Codesys
      Variable declaration:
    szFilePath   : STRING := '/dev/input/js0';
    Buffer: STRING(16);
    

    Code:

    hFile := SysFile.SysFileOpen(szFile:=szFilePath, am:=SysFile.AM_READ, pResult:=ADR(FileOpenResult));
    dwRead := SysFile.SysFileRead(hFile:=hFile, pbyBuffer:=ADR(Buffer), ulSize:=SIZEOF(Buffer), pResult:=ADR(FileReadResult));
    SysFileClose(hFile:=hFile);
    

    However I get in the variable Buffer only some counter which is constantly counting up.

    Has anyone any idea how I can read this file?

    Thank you!

    Regards,
    Chris

     
  • yangkkokk - 2020-08-18

    同问

     
    • Ingo

      Ingo - 2020-08-18

      Hi yangkkokk!
      Welcome to CODESYS Talk!

      "同问" seems to translate to "The same question", but please use english when posting s.th. here. Without using a common language there will be no "Talk" 😉

       

Log in to post a comment.