如何使用Xenomai RTnet创建套接字?

时间:2019-05-29 13:11:21

标签: sockets real-time native beagleboneblack xenomai

我想在我的Beaglebone Black上使用Xenomai / RTnet创建一个套接字。我正在使用可运行的Makefile编译程序。一切看起来不错,但是当我想运行程序时,会发生以下错误:

  

错误:无法创建套接字。

谁能告诉我为什么会这样。我已经阅读了许多有关“无法创建套接字...”的问题,但是没有人将Xenomai与RTnet一起使用。所以我不明白这里的问题在哪里。没有RTnet,一切都会很好。

这是我关于往返的完整代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>

/*XENOMAI*/
#include <native/task.h>
#include <rtdm/rtdm.h>
#include <asm/ioctl.h>

#define SERVER "192.168.127.10"
#define BUFLEN 512
#define PORT 8888

void die(char *s)
{
    perror(s);
    exit(1);
}

void udprec_rt_thread(void *arg)
{
    struct sockaddr_in si_other; 
    int i, slen=sizeof(si_other);
    char buf[BUFLEN];
    char message[BUFLEN];
    struct timeval tv1;
    struct timeval tv2;
    double rtt;
    double avg=0, best=0, worst=0;
    printf("3");    //Debug
    RT_TASK* curTask = rt_task_self();      
    RT_TASK_INFO curTaskInfo;               
    rt_task_inquire(curTask,&curTaskInfo);  
    printf("4");    

/* Create Socket */
    int socks_IN = rt_dev_socket(AF_INET, SOCK_DGRAM, 0);   // Create RT-Socket for receiving
    if (socks_IN<0)
    {
        fprintf(stderr,"ERROR: Failed to create socket");
        return; 
    }
    int socks_OUT = rt_dev_socket(AF_INET, SOCK_DGRAM, 0);  // Create RT-Socket for transmitting
    if (socks_OUT<0)
    {
        fprintf(stderr,"ERROR: Failed to create socket");
        rt_dev_close(socks_IN);
        return; 
    }

    /* Socket Options */
    /*
    int enabled = 1;
    struct timeval timeout;
    memset(&timeout, 0, sizeof(timeout)); // zero timeout struct before use
    timeout.tv_sec = 0;
    timeout.tv_usec = 10000;
    if (setsockopt(socks_IN, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))==-1)
    {
        printf("Failed with RCV Time [Input]\n");
        rt_dev_close(socks_IN);
        rt_dev_close(socks_OUT);
        return;
    }*/


    /* Bind Socket */
    printf("5");    
    //memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;      
    si_other.sin_port = htons(PORT);    
    inet_pton(AF_INET, SERVER, &(si_other.sin_addr));
    /*if (inet_aton(AF_INET, &si_other.sin_addr) == 0)
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }*/
    printf("6");    //Debug
    if (rt_dev_bind(socks_IN, (struct sockaddr *)&si_other, sizeof(si_other))<0)    
    {
        fprintf(stderr,"ERROR: Failed to bind socket");
        rt_dev_close(socks_IN);
        rt_dev_close(socks_OUT);
        return; 
    }
    printf("7");    //Debug
    rt_printf("Start Runtime\n");

    for(i=0;i<100;i++)
    {
    // Just code for Roundtrip
    }

    return;
}
int main(void)
{   
    RT_TASK udp_rt_thread;      
    //pthread_t udp_thread; only for non real-time threads
    printf("1");                //Debug
    int err = rt_task_create(&udp_rt_thread,"",0,99,0); //returns zero upon success 
    printf("2");                //Debug
    if(!err)  rt_task_start(&udp_rt_thread,&udprec_rt_thread,0); // start RT-task
}

这是我的Makefile:

# Allow overriding xeno-config on make command line
XENO_CONFIG := xeno-config

prefix := $(shell $(XENO_CONFIG) --prefix)

ifeq ($(prefix),)
$(error Please add <xenomai-install-path>/bin to your PATH variable)
endif

CC := $(shell $(XENO_CONFIG) --skin=native --cc)
STD_CFLAGS  := $(shell $(XENO_CONFIG) --skin=native --cflags) -g
STD_LDFLAGS := $(shell $(XENO_CONFIG) --skin=native --ldflags) -g -lrtdm

STD_TARGETS := rtt_rt

all: $(STD_TARGETS)

$(STD_TARGETS): $(STD_TARGETS:%=%.c)
    $(CC) -o $@ $< $(STD_CFLAGS) $(STD_LDFLAGS)

clean:
    $(RM) -f *.o *~ $(STD_TARGETS) 

0 个答案:

没有答案