使用Linux套接字通信(通过wifi)时套接字CAN不起作用

时间:2017-07-04 19:26:57

标签: c can-bus socketcan

这是服务器端代码(raspberry pi)

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

#include <unistd.h>
#include <wiringPi.h>
#include <softPwm.h>
#include <pthread.h>

void error( char *msg ) {
  perror(  msg );
  exit(1);
}

int func( int a ) {
   return 2 * a;
}

void sendData( int sockfd, int x ) {
  int n;

  char buffer[32];
  sprintf( buffer, "%dn", x );
  if ( (n = write( sockfd, buffer, strlen(buffer) ) ) < 0 )
    error( const_cast<char *>( "ERROR writing to socket") );
  buffer[n] = '';
}

int getData( int sockfd ) {
  char buffer[32];
  int n;

  if ( (n = read(sockfd,buffer,31) ) < 0 )
    error( const_cast<char *>( "ERROR reading from socket") );
  buffer[n] = '';

  printf("buffer: %s n",buffer);



  return atoi( buffer );
}

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

    char buf[30];
     int sockfd, newsockfd, portno = 1219, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     int data;

     printf( "using port #%dn", portno );

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
         error( const_cast<char *>("ERROR opening socket") );
     bzero((char *) &serv_addr, sizeof(serv_addr));

     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons( portno );
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
       error( const_cast<char *>( "ERROR on binding" ) );
     listen(sockfd,5);
     clilen = sizeof(cli_addr);

     //--- infinite wait on a connection ---
     while ( 1 ) {
        printf( "waiting for new client.....n" );

        if ( ( newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen) ) < 0 )
            error( const_cast<char *>("ERROR on accept") );

        recv(newsockfd,buf,31,0);
        printf( "opened new communication with clientn" );
        printf( "sockfd: %dn",sockfd );
        printf( "newsockfd: %d n ",newsockfd );

        int res = strcmp(buf,"go");
        printf("res: %dn",res);
        write(newsockfd,"message received! [ from raspi server ! ]",50);

        while ( 1 ) {

             //---- wait for a number from client ---
            data = getData( newsockfd );
            //recv(newsockfd,buf,31,0);
            // printf("%s n",buf);

             printf( "got %dn", data );
             if ( data < 0 )
                break;

             data = func( data );

             //--- send new data back ---
             printf( "sending back %dn", data );
             sendData( newsockfd, data );


        }

        close( newsockfd );

        //--- if -2 sent by client, we can quit ---
        if ( data == -2 )
          break;
     }


     return 0;
}

一旦开始使用这个linux套接字通信程序(当mylaptop,即客户端,连接到linux机器),基于socket-CAN的程序(基于CAN通信开源库的mcp 2515 ..)不起作用那 &#34;写:没有可用的缓冲空间&#34;当我试图做candump时 查看下面的图片

Image

我想这与Linux套接字程序有关,我在上面用于linux机器(raspberry,linux)和我的笔记本电脑(窗口)之间的套接字通信代码。但我不知道如何避免与问题相关的缓冲空间&#39;是什么造成的。也许每次我得到套接字帧时我应该刷新缓冲区? 任何人都知道我应该做什么同时让Socket-CAN和Linux套接字通信工作?

1 个答案:

答案 0 :(得分:0)

尝试在调用accept()和write()函数的第一个while循环中初始化clilen。

while ( 1 ) {
    printf( "waiting for new client.....n" );

    clilen = sizeof(struct sockaddr_in);
    if ( ( newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen) ) < 0 )
        error( const_cast<char *>("ERROR on accept") );