继续获得垃圾价值

时间:2013-04-21 17:32:03

标签: c joystick

我正在研究一个从USB操纵杆获取轴值并通过TCP发送它的程序。我所做的是首先让TCP程序工作,这样我就可以发送按预期工作的消息。然后我编写了另一个程序来从操纵杆中获取值并将它们打印到屏幕上,该屏幕也按预期工作。

我最近尝试的是将这两个单独的程序合并为一个,因此我有一个server程序和一个client程序。这就是我的问题已经开始的地方,我在编译期间没有遇到任何错误。似乎程序不再从操纵杆获取值并将它们存储在变量axis[0]中。

现在我的过程是在连接了操纵杆的计算机上启动server.c文件。然后我将在应该接收值的计算机上启动client.c文件。我一路上有几个printf语句告诉我目前脚本到底在哪里。当我执行server.c脚本时,它告诉我检测到操纵杆并且有6个轴和12个按钮。然后它将在循环中等待,直到尝试从客户端建立连接。

执行client.c程序后,它将打印出The value is -1093458243。在它之下它将打印出Incoming connection from 192.168.254.15 - sending welcome.客户端程序然后退出,这就是发生的一切。

The value is:的行应该是介于-32768和32767之间的数字。所以它从某个地方获取垃圾值。我迷路了,并且一直试图解决这一问题一整天都没有运气。我已经包含了几个代码示例,工作的TCP serverclient脚本以及工作joystick脚本和我尝试将两者结合起来的代码。任何人都可以发现我的代码中的任何错误,或者给我一些指示吗?

以下是server.c的组合代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

//joysick libraries
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/joystick.h>

//Joysick device
#define JOY_DEV "/dev/input/js0"

//TCP Port number
#define PORTNUM 2343

int main(int argc, char *argv[])
{

//TCP connect
    struct sockaddr_in dest; /* socket info about the machine connecting to us */
    struct sockaddr_in serv; /* socket info about our server */
    int mysocket;            /* socket used to listen for incoming connections */
    socklen_t socksize = sizeof(struct sockaddr_in);

    memset(&serv, 0, sizeof(serv));           /* zero the struct before filling the fields */
    serv.sin_family = AF_INET;                /* set the type of connection to TCP/IP */
    serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
    serv.sin_port = htons(PORTNUM);           /* set the server port number */    

    mysocket = socket(AF_INET, SOCK_STREAM, 0);

    /* bind serv information to mysocket */
    bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));

    /* start listening, allowing a queue of up to 1 pending connection */
    listen(mysocket, 1);
    int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);

//TCP variable
         char msg1[10];


//Joystick variables
        int xAxis;
    int yAxis;
        int xSpeed;
        int ySpeed;

//Joystick initialize
    int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
    char *button=NULL, name_of_joystick[80];
    struct js_event js;

    if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
    {
        printf( "Couldn't open joystick\n" );
        return -1;
    }

//Get number of axes, buttons and name of joystick. Print results to screen
        ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
    ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
    ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );

    axis = (int *) calloc( num_of_axis, sizeof( int ) );
    button = (char *) calloc( num_of_buttons, sizeof( char ) );

    printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
        , name_of_joystick
        , num_of_axis
        , num_of_buttons );

//Use non blocking mode for joystick
    fcntl( joy_fd, F_SETFL, O_NONBLOCK );   /* use non-blocking mode */


    while(1) {
//Test to read joystick state
            /* read the joystick state */
        read(joy_fd, &js, sizeof(struct js_event));

            /* see what to do with the event */
        switch (js.type & ~JS_EVENT_INIT)
        {
            case JS_EVENT_AXIS:
                axis   [ js.number ] = js.value;
                break;
            case JS_EVENT_BUTTON:
                button [ js.number ] = js.value;
                break;
        }
//Give msg1 variable the value of axis[0]
    sprintf(msg1, "%d", axis[0]);

    //char msg1 = axis[0];
//TCP send
    while(consocket)
    {
    printf("Value is: %6d\n", axis[0]);
        printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
        send(consocket, msg1, strlen(msg1), 0); 
        consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
    }

    close(consocket);
    close(mysocket);
    return EXIT_SUCCESS;

//Print joystick state to screen
    printf("Value is: %d", axis[0]);
    //printf(" \r");
    //fflush(stdout);   
    }


//Joystick close
    close( joy_fd );    /* too bad we never get here */
    return 0;

//TCP message
    //printf("Please enter info\n");
    //fgets(msg1, 20, stdin);

}

这是旧的server.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORTNUM 2343

int main(int argc, char *argv[])
{
    char msg1[] = "Hello World !\n";

    struct sockaddr_in dest; /* socket info about the machine connecting to us */
    struct sockaddr_in serv; /* socket info about our server */
    int mysocket;            /* socket used to listen for incoming connections */
    socklen_t socksize = sizeof(struct sockaddr_in);

    memset(&serv, 0, sizeof(serv));           /* zero the struct before filling the fields */
    serv.sin_family = AF_INET;                /* set the type of connection to TCP/IP */
    serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
    serv.sin_port = htons(PORTNUM);           /* set the server port number */    

    mysocket = socket(AF_INET, SOCK_STREAM, 0);

    /* bind serv information to mysocket */
    bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));

    /* start listening, allowing a queue of up to 1 pending connection */
    listen(mysocket, 1);
    int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);

    while(consocket)
    {
        printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
        send(consocket, msg1, strlen(msg1), 0); 
        consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
    }

    close(consocket);
    close(mysocket);
    return EXIT_SUCCESS;
}

以下是client.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXRCVLEN 500
#define PORTNUM 2343

int main(int argc, char *argv[])
{
   char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */
   int len, mysocket;
   struct sockaddr_in dest; 

   mysocket = socket(AF_INET, SOCK_STREAM, 0);

   memset(&dest, 0, sizeof(dest));                /* zero the struct */
   dest.sin_family = AF_INET;
   dest.sin_addr.s_addr = inet_addr("192.168.254.16"); /* set destination IP number */ 
   dest.sin_port = htons(PORTNUM);                /* set destination port number */

   connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));

   len = recv(mysocket, buffer, MAXRCVLEN, 0);

   /* We have to null terminate the received data ourselves */
   buffer[len] = '\0';

   printf("Received %s (%d bytes).\n", buffer, len);

   close(mysocket);
   return EXIT_SUCCESS;
}

这是获取值并将其打印到屏幕的joystick文件:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
#include <wiringPi.h>
#define JOY_DEV "/dev/input/js0"

int main()
{
    int xAxis;
    int yAxis;
        int xSpeed;
        int ySpeed;

    FILE *fp;
        fp = fopen("/dev/servoblaster","w");
        if (fp == NULL) {
        printf("Error opening file");
        exit(0);
    }

    int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
    char *button=NULL, name_of_joystick[80];
    struct js_event js;

    if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
    {
        printf( "Couldn't open joystick\n" );
        return -1;
    }

    ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
    ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
    ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );

    axis = (int *) calloc( num_of_axis, sizeof( int ) );
    button = (char *) calloc( num_of_buttons, sizeof( char ) );

    printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
        , name_of_joystick
        , num_of_axis
        , num_of_buttons );

    fcntl( joy_fd, F_SETFL, O_NONBLOCK );   /* use non-blocking mode */

    while( 1 )  /* infinite loop */
    {

            /* read the joystick state */
        read(joy_fd, &js, sizeof(struct js_event));

            /* see what to do with the event */
        switch (js.type & ~JS_EVENT_INIT)
        {
            case JS_EVENT_AXIS:
                axis   [ js.number ] = js.value;
                break;
            case JS_EVENT_BUTTON:
                button [ js.number ] = js.value;
                break;
        }

            /* print the results */

        //printf( "X: %6d  Y: %6d  ", axis[0], axis[1] );


        int xAxis = axis[0] / 327;
        int yAxis = axis[1] / 327;
        int ySpeed = 150-0.3*yAxis;
        int xSpeed = 150-0.3*xAxis;
        printf( "X is equal to: %d  Y is equal to: %d  Y speed is: %d ", xAxis, yAxis, ySpeed);

        fprintf(fp, "2=%d\n", ySpeed);
        fprintf(fp, "1=%d\n", xSpeed);
            printf("  \r");
        fflush(stdout);
    }

    close( joy_fd );    /* too bad we never get here */
    return 0;
}

1 个答案:

答案 0 :(得分:1)

对我来说有点奇怪(我认为这可能是你遇到问题的原因)

fcntl( joy_fd, F_SETFL, O_NONBLOCK );   /* use non-blocking mode */

while( 1 )  /* infinite loop */
{

        /* read the joystick state */
    read(joy_fd, &js, sizeof(struct js_event));

您正在使用非阻止模式,但没有检查 read 调用实际上是否正在读取任何内容。由于它没有阻塞,即使操纵杆设备尚未提供数据,它也会返回。