【uTenux实验】集合点端口

这个是头一次接触的概念。比较不好理解。内核规范中的说明就要20页!

看了王总写的uTenux内核规范之后,有那么一点明白了但理解不深。

集合点端口就像每次工作前的收集情况会。首长下达收集情况指令,各个部门聆听。当给某个部门下达指令之后,这个部门开始工作。并返回信息。所有部门都会报完信息之后,首长心里就有数了:今天天气不错,昨天的机器故障排除了,原料到位。可以开工了!于是,一天的生产工作就此开始。各个部门反馈完情况,就自由活动了。没事唠嗑都行。。

这里的首长,就是创建这个集合点端口的任务。各个部门就是被集合点召集的任务。他们反馈的情况就是被调用任务的反馈。反馈完情况的任务,进入下一个循环周期。

先按照这个理解做了实验再说!

【实验描述】

创建两个任务一个集合点端口PorID。之后启动TaskA。

在TaskA中启动TaskB,进入循环。

循环体开始时候,TaskA调用集合点端口,准备创建集合点。由于条件不满足,TaskA进入休眠,TaskB开始执行

TaskB,首先接受集合点端口调用,然后回复调用信息。

此时TaskA创建集合点条件满足,继续执行。将接收到的信息输出。

【代码及输出】

#include "PorSample.h"

void PorSampleTaskA(W stacd,VP exinf);
void PorSampleTaskB(W stacd,VP exinf);
void PorSamplePutPor(void);
static ID TaskID_A;
static ID TaskID_B;
static ID PorID;
 
EXPORT ER PorSample( void )
{
  T_CPOR cpor;
  T_CTSK ctsk;
  
  //创建两个任务
  ctsk.bufptr = NULL;
  ctsk.exinf = (VP)NULL;
  ctsk.itskpri = 20;
  ctsk.stksz = 512;
  ctsk.task = PorSampleTaskA;
  ctsk.tskatr = TA_HLNG | TA_RNG0;
  TaskID_A = tk_cre_tsk(&ctsk);
  
  ctsk.task = PorSampleTaskB;
  TaskID_B = tk_cre_tsk(&ctsk);
  
  //创建集合点端口
  cpor.exinf = NULL;
  cpor.maxcmsz = 50;   //最大发送消息数目
  cpor.maxrmsz =50;    //最大接收消息数目
  cpor.poratr = TA_TFIFO;  //属性,FIFO模式排队
  PorID = tk_cre_por(&cpor);
  
  tk_sta_tsk(TaskID_A,5);
  return E_OK;
}

void PorSampleTaskA(W stacd,VP exinf)
{
  B calmsg[] = "I am from TaskA ,i am now calling rendevons port
";
  B len = strlen(calmsg);
  tk_sta_tsk(TaskID_B,0);
  while(1)
  {
    tm_putstring((UB*)"TaskA调用集合点端口
");
    tm_putstring((UB*)calmsg);
    tm_putstring((UB*)" 
");
    tk_cal_por(PorID,0x02,(VP)calmsg,strlen(calmsg),-1);
    tm_putstring((UB*)"I am in task a,the rpl msg is:
");
    tm_putstring((UB*)calmsg);
    tm_putstring((UB*)" 
");
    Delay(0x1000000);
  }
}
 
void PorSampleTaskB(W stacd,VP exinf)
{
  B rcvmsg[] = "I am from Task B ,i am now accept rendevons port
";
  B len = strlen(rcvmsg);
  RNO rdvno;
  B calmsg[50],rplmsg[50];
  calmsg[0] = '';
  rplmsg[0] = '';
  while(1)
  {
    tm_putstring((UB*)"i am in task b
 the cal msg is :
");
    tk_acp_por(PorID,0x02,&rdvno,(VP)calmsg,-1);
    tm_putstring((UB*)calmsg);
    tm_putstring((UB*)" 
");
    tk_rpl_rdv(rdvno,rcvmsg,strlen(rcvmsg));
  }
}

void PorSamplePutPor(void)
{
    B info[10];
    T_RPOR rpor;

    tm_putstring((UB*)"Now rendevons port infor is wtsk atsk maxcmsz maxrmsz:");
    tk_ref_por(PorID, &rpor);
    ltostr(rpor.wtsk,info,10,10);
    tm_putstring((UB*)info);
    tm_putstring((UB*)" ");
    ltostr(rpor.atsk,info,10,10);
    tm_putstring((UB*)info);
    tm_putstring((UB*)" ");
    ltostr(rpor.maxcmsz,info,10,10);
    tm_putstring((UB*)info);
    tm_putstring((UB*)" ");
    ltostr(rpor.maxrmsz,info,10,10);
    tm_putstring((UB*)info);
    tm_putstring((UB*)"
");
}

输出

----------------------------------------------------
        micro Tenux Version 1.6.00(build 0180)     
            Supported MCU is ST STM32F407VG        
  Copyright(c) 2008-2013 by Dalian uLoong Co.,Ltd. 
----------------------------------------------------

TaskA调用集合点端口
I am from TaskA ,i am now calling rendevons port
 
i am in task b
the cal msg is :
I am from TaskA ,i am now calling rendevons port
 
i am in task b
the cal msg is :
I am in task a,the rpl msg is:
I am from Task B ,i am now accept rendevons port
 
TaskA调用集合点端口
I am from Task B ,i am now accept rendevons port
 
I am from Task B ,i am now accept rendevons port
 
i am in task b
the cal msg is :
I am in task a,the rpl msg is:
I am from Task B ,i am now accept rendevons port
 
TaskA调用集合点端口
I am from Task B ,i am now accept rendevons port
 
I am from Task B ,i am now accept rendevons port
 
i am in task b
the cal msg is :
I am in task a,the rpl msg is:
I am from Task B ,i am now accept rendevons port
 
TaskA调用集合点端口
I am from Task B ,i am now accept rendevons port
 
I am from Task B ,i am now accept rendevons port
 
i am in task b
the cal msg is :
I am in task a,the rpl msg is:
I am from Task B ,i am now accept rendevons port
 
TaskA调用集合点端口
I am from Task B ,i am now accept rendevons port
 
I am from Task B ,i am now accept rendevons port
 
i am in task b
the cal msg is :
I am in task a,the rpl msg is:
I am from Task B ,i am now accept rendevons port
 
TaskA调用集合点端口
I am from Task B ,i am now accept rendevons port
 
I am from Task B ,i am now accept rendevons port
 
i am in task b
the cal msg is :
I am in task a,the rpl msg is:
I am from Task B ,i am now accept rendevons port
 

【实验不足处】

由于不太理解集合点端口中消息传输机制,接收消息到的消息最后部分会出现乱码。使用固定长度消息暂时避免了这个问题,即发送和接收消息长度相同,而且等于集合点端口接收和发送的最大消息数。有待改进

这个不足可以用memset解决。向内存块放得下之前,先将内存块填0

原文地址:https://www.cnblogs.com/zyqgold/p/3169141.html