How do I read a USB joystick? - SUN

This is a discussion on How do I read a USB joystick? - SUN ; I need to read a USB joystick on a Solaris system from an application program. The vendor has given me a copy of the 7-byte report package format, but my test program is not giving me correct values. Can someone ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: How do I read a USB joystick?

  1. How do I read a USB joystick?

    I need to read a USB joystick on a Solaris system from an application
    program. The vendor has given me a copy of the 7-byte report package
    format, but my test program is not giving me correct values. Can
    someone either tell me what I'm doing wrong or just tell me the correct
    method? Thanks.

    My test program is:
    #include
    #include
    #include
    #include

    void main()
    {
    signed char data[8];
    int moreFlag;
    int fd;

    fd = open("/dev/usb/hid6",O_RDONLY);
    if (fd == -1)
    {
    printf("\n OPEN FAILED\n");
    exit(0);
    }
    printf("\n OPEN PASSED\n");
    moreFlag = 1;
    while (moreFlag == 1)
    {
    moreFlag = read(fd, &data[0], 7);
    if (moreFlag < 1)
    printf("\n read returned %d\n", moreFlag);
    else
    {
    printf("read %d bytes (in reverse): ", moreFlag);
    while (moreFlag > 1)
    {
    moreFlag--;
    printf("%d, ", (int)data[moreFlag]);
    }
    printf("%d\n enter 1 to repeat, anything else to stop: ",
    (int)data[0]);
    scanf("%d", &moreFlag);
    }

    }
    printf("\n\ngood-bye\n");
    close(fd);
    }


    Thanks.

    Scott B. Klein
    Senior Engineer
    Northrop Grumman Electronic Sensors
    Baltimore, Maryland 21203-0746
    Telephone (410) 765-0910
    s.klein@ngc.com


  2. Re: How do I read a USB joystick?

    s.klein@ngc.com writes:

    >I need to read a USB joystick on a Solaris system from an application
    >program. The vendor has given me a copy of the 7-byte report package
    >format, but my test program is not giving me correct values. Can
    >someone either tell me what I'm doing wrong or just tell me the correct
    >method? Thanks.


    >My test program is:
    >#include
    >#include
    >#include
    >#include


    >void main()
    >{
    > signed char data[8];
    > int moreFlag;
    > int fd;


    > fd = open("/dev/usb/hid6",O_RDONLY);
    > if (fd == -1)
    > {
    > printf("\n OPEN FAILED\n");
    > exit(0);
    > }
    > printf("\n OPEN PASSED\n");
    > moreFlag = 1;
    > while (moreFlag == 1)
    > {
    > moreFlag = read(fd, &data[0], 7);



    If you want to read 7 bytes, you may need to do things like:


    toread = 7;
    while (toread > 0) {
    nread = read(fd, &data[7-toread], toread);
    if (nread <= 0) {
    /* Exceptional circumstances? */
    break;
    }
    toread -= nread;
    }

    /* Done reading packet */
    if (toread == 0) {
    /* Read all 7 bytes */

    }

    You may also want to read unsigned bytes and you may want to
    look at possible ways to synchronize unsynchronized data
    streams.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.

  3. Re: How do I read a USB joystick?


    Thanks for your reply, but I guess that I wasn't clear. The read
    successfully read the correct 7 bytes, but the values were not correct.
    Not matter how I moved the joystick or pressed the buttions, the values
    were either 0, 127, or -128. Even without touching the joystick, values
    whould change.

    An additional note is that I connected the joystick to my PC, the
    Windows Joystick Properties Test tool graphically displays the correct
    values for the joystick functions, so I know that it is not a hardware
    problem. Thanks.

    Scott


+ Reply to Thread