RTAI的用户空间编程(六)——管道通讯

10 篇文章 8 订阅

包含头文件#include “rtai_sched.h”
1. 发送函数

int rt_mbx_send (MBX* mbx, void* msg, int msg_size);
//无条件发送消息

int rt_mbx_send_wp (MBX* mbx, void* msg, int msg_size); 
//在不阻塞当期调用的任务的情况下,尽可能多的发送数据。消息长度是msg_size。

int rt_mbx_send_if (MBX* mbx, void* msg, int msg_size); 
//发送一个消息, 要求在不阻塞当前调用任务的情况下,整个消息可以完整传递。

int rt_mbx_send_until (MBX* mbx, void* msg, int msg_size, RTIME time);
int rt_mbx_send_timed (MBX* mbx, void* msg, int msg_size, RTIME delay);
//rt_mbx_send_until和rt_mbx_send_timed发送消息msg_size大小的msg到邮箱mbx。调用者将被阻塞,直到所有字节的入消息队列、超时过期或发生错误。

函数返回
成功,未寄出的字节的数量被返回;失败,返回负值,负值定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).

2. 接收函数

int rt_mbx_receive (MBX* mbx, void* msg, int msg_size);
//无条件接受消息

int rt_mbx_receive_wp (MBX* mbx, void* msg, int msg_size);
//在不阻塞当期调用的任务的情况下,尽可能多的接收数据。

int rt_mbx_receive_if (MBX* mbx, void* msg, int msg_size);
//整个msg_size大小的消息能立刻获取到时,接收消息。

int rt_mbx_receive_until (MBX* mbx, void* msg, int msg_size, RTIME time);
int rt_mbx_receive_timed (MBX* mbx, void* msg, int msg_size, RTIME delay);
//调用者将被阻塞,直到所有字节的入消息队列、超时过期或发生错误。time是绝对值,Delay是相对于当前时间的相对值.

函数返回
成功,返回收到的字节数量。
失败,返回一个负值,定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).

3. 示例代码

/* messqueue.c*/
/* ------------ headers ------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sched.h>
#include <linux/errno.h> /* error codes */
#include <signal.h>
#include <pthread.h>

#include <rtai_lxrt.h>
#include <rtai_mbx.h>
#include <rtai_msg.h>

/* ------------ globals ------------------------------------------- */

/* turn on(1) or off(0) debugging */
const int DEBUG=1;

// fix include warning
//#include <asm/system.h>
//#include <rtai_posix.h>
int pthread_cancel_rt(pthread_t thread);

// linux threads id's
static int thread1;
static int thread2;
// realtime task structures
static RT_TASK  *t1;
static RT_TASK  *t2;

// message queue
static MBX *mesgQueueId;
#define MAX_MESSAGES 100
#define MAX_MESSAGE_LENGTH 50

/* ------------ functions ------------------------------------------- */

void taskOne(void *arg)
{
    int retval;
    char message[] = "Received message from taskOne";

    /*  make this thread LXRT soft realtime */
    t1 = rt_task_init_schmod(nam2num("TASK1"), 0, 0, 0, SCHED_FIFO, 0xF);
    mlockall(MCL_CURRENT | MCL_FUTURE);

    // makes task hard real time (only when not developing/debugging)
    if ( !DEBUG )  rt_make_hard_real_time();

    rt_printk("Started taskOne\n");   
    /* send message */
    retval = rt_mbx_send(mesgQueueId, message, sizeof(message));
    if (0 != retval) {
        if (-EINVAL == retval) {
          rt_printk("mailbox is invalid\n");
        } else {
          /* unknown error */
          rt_printk("Unknown mailbox error\n");
        }
    }

}

void taskTwo(void *arg)
{
    int retval;
    char msgBuf[MAX_MESSAGE_LENGTH];

    /*  make this thread LXRT soft realtime */
    t2 = rt_task_init_schmod(nam2num("TASK2"), 0, 0, 0, SCHED_FIFO, 0xF);
    mlockall(MCL_CURRENT | MCL_FUTURE);

    // makes task hard real time (only when not developing/debugging)
    if ( !DEBUG )  rt_make_hard_real_time();

    rt_printk("Started taskTwo\n");

    /* receive message */
    retval = rt_mbx_receive_wp(mesgQueueId, msgBuf, 50);
    if (-EINVAL == retval) {
          rt_printk("mailbox is invalid\n");
    } else {
        rt_printk("message length=50-%d\n",retval);
        rt_printk("message : %s\n",msgBuf); 
    }
}

void cleanup(void)
{
    /* delete message queue */      
    rt_mbx_delete(mesgQueueId);

    // task end themselves -> not necesssary to delete them 
    return;
}

/* ------------ main ------------------------------------------- */

int main(void)
{   
    printf("Start of main\n");

    /*  make this thread LXRT soft realtime */
    rt_task_init_schmod(nam2num("TASK0"), 0, 0, 0, SCHED_FIFO, 0xF);
    mlockall(MCL_CURRENT | MCL_FUTURE);

    // set realtime timer to run in oneshot mode    
    rt_set_oneshot_mode();
    // start realtime timer and scheduler
    start_rt_timer(1);


    /* create message queue */    
    mesgQueueId = rt_typed_mbx_init (nam2num("MSGQUEUE"), MAX_MESSAGES, FIFO_Q);
    if (mesgQueueId  == 0) {
        printf("Error creating message queue\n");
        return 1;
    }

    // create the linux threads 
    thread1 = rt_thread_create(taskOne, NULL, 10000);
    thread2 = rt_thread_create(taskTwo, NULL, 10000);


    // wait for end of program
    printf("TYPE <ENTER> TO TERMINATE\n");
    getchar();

    // cleanup
    cleanup();

    printf("Finished\n");    
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值