void PorSampleTaskA(W stacd,VP exinf);
void PorSampleTaskB(W stacd,VP exinf);
void PorSamplePutErcd(ER ercd);
void PorSamplePutPor(void);
UW ltostr(UW Val,B *pString,UB Base,UW Length);
void Delay(void);
static ID TaskID_A;
static ID TaskID_B;
static ID PorID;
EXPORT ER PorSample( void )
{
T_CPOR cpor;
T_CTSK ctskA;
T_CTSK ctskB;
tm_putstring((UB*)"Rendevons port sample create step 1-Create Rendevons port, "); //输出:Rendevons port sample create step 1-Create Rendevons port,
cpor.poratr = TA_TFIFO; //任务按照FIFO进行排队;
cpor.maxcmsz = 80; //最大调用消息的大小;
cpor.maxrmsz = 80; //最大回应消息的大小;
PorID = tk_cre_por(&cpor); //创建集合点端口;
if(PorID > E_OK) {
tm_putstring((UB*)"ID is "); //输出:ID is 1!
tm_putchar(PorID+48);
tm_putstring((UB*)" !\n");
} else {
tm_putstring((UB*)"Error code is ");
tm_putchar(PorID+48);
tm_putstring((UB*)" !\n");
}
tm_putstring((UB*)"Rendevons port sample create step 2-Create Task A, "); //输出:Rendevons port sample create step 2-Create Task A,
ctskA.exinf = (VP)NULL; /*创建任务A*/
ctskA.tskatr = TA_HLNG | TA_RNG0;
ctskA.task = PorSampleTaskA;
ctskA.itskpri = 10;
ctskA.stksz = 512*4;
TaskID_A = tk_cre_tsk(&ctskA);
if(TaskID_A > E_OK) {
tm_putstring((UB*)"ID is "); //输出:ID is 2!
tm_putchar(TaskID_A+48);
tm_putstring((UB*)" !\n");
} else {
tm_putstring((UB*)"Error code is ");
tm_putchar(TaskID_A+48);
tm_putstring((UB*)" !\n");
}
tm_putstring((UB*)"Rendevons port sample create step 3-Create Task B, "); //输出:Rendevons port sample create step 3-Create Task B,
ctskB.exinf = (VP)NULL;
ctskB.tskatr = TA_HLNG | TA_RNG0; /*创建任务B*/
ctskB.task = PorSampleTaskB;
ctskB.itskpri = 10;
ctskB.stksz = 512*4;
TaskID_B = tk_cre_tsk(&ctskB);
if(TaskID_B>0) {
tm_putstring((UB*)"ID is "); //输出:ID is 3!
tm_putchar(TaskID_B+48);
tm_putstring((UB*)" !\n");
} else {
tm_putstring((UB*)"Error code is ");
tm_putchar(TaskID_B+48);
tm_putstring((UB*)" !\n");
}
tm_putstring((UB*)"Rendevons port sample create step 4-Start Task B;\n\n"); //输出:Rendevons port sample create step 4-Start Task B;
tk_sta_tsk(TaskID_B,0);
return E_OK;
}
void PorSampleTaskA(W stacd,VP exinf) //任务A的服务函数;
{
ER ercd;
UINT i;
RNO rdvno,cmsgsz;
B calmsg[80],rplmsg[80];
rplmsg[0] = '\0';
rplmsg[1] = '\0';
calmsg[1] = '\0';
for(i=0;;i++) {
cmsgsz = tk_acp_por(PorID,0x1,&rdvno ,(VP)calmsg,-1); //接受集合点端口;接受模式为0x1, 返回集合点编号, 返回调用消息的大小;
tm_putstring((UB*)"Task A is now accept and reply rendevons port "); //输出:Task A is now accept and reply rendevons port
tm_putchar(i+48+1);
tm_putstring((UB*)" times\n");
if(cmsgsz > E_OK){
tm_putstring((UB*)"Task A accept rendevons port message:");
tm_putstring((UB*)calmsg);
} else {
tm_putstring((UB*)"Task A cannot accept rendevons port: ");
PorSamplePutErcd(cmsgsz);
}
tm_putstring((UB*)"\n");
switch(calmsg[0]){
case '0':
rplmsg[0] = 'a';
break;
case '1':
rplmsg[0] = 'b';
break;
case '2':
rplmsg[0] = 'c';
break;
case '3':
rplmsg[0] = 'd';
break;
case '4':
rplmsg[0] = 'e';
break;
case '5':
rplmsg[0] = 'f';
break;
case '6':
rplmsg[0] = 'g';
break;
case '7':
rplmsg[0] = 'h';
break;
case '8':
rplmsg[0] = 'i';
break;
case '9':
rplmsg[0] = 'j';
break;
default:
rplmsg[0] = 'k';
break;
}
ercd = tk_rpl_rdv(rdvno ,(VP)rplmsg,strlen(rplmsg)); // 回应集合点,集合点编号,回应消息的数据包的地址, 回应消息的大小;
if(ercd == E_OK){
tm_putstring((UB*)"Task A reply rendevons port message:");
tm_putstring((UB*)rplmsg);
} else {
tm_putstring((UB*)"Task A cannot reply rendevons port: ");
PorSamplePutErcd(ercd);
}
tm_putstring((UB*)"\n");
PorSamplePutPor();
}
}
void PorSampleTaskB(W stacd,VP exinf) //任务B的服务函数
{B c;
ER ercd;
UINT i,rmsgsz;
B calmsg[80];
tm_putstring((UB*)"Task B will start task A;\n"); //输出:Task B will start task A;
tk_sta_tsk(TaskID_A,0);
calmsg[1]='\0';
for(i=0;i<=9;i++) {
tm_putstring((UB*)"Task B is running,input Command(e=exit):\n"); //输出:Task B is running,input Command(e=exit):
c = tm_getchar(0);
if(c=='e') {
break;
}
tm_putstring((UB*)"Task B is now calling rendevons port "); //输出:Task B is now calling rendevons port
tm_putchar(i+48+1);
tm_putstring((UB*)" times\n");
calmsg[0]=(B)(i+48);
tm_putstring((UB*)"Task B send rendevons port message:"); //输出:Task B send rendevons port message:
tm_putstring((UB*)calmsg);
tm_putstring((UB*)"\n");
rmsgsz = tk_cal_por(PorID,0x1,(VP)calmsg,strlen(calmsg),-1); // 调用集合点端口,调用位模式,消息数据包地址,返回调用消息的大小;
if(rmsgsz > E_OK){
tm_putstring((UB*)"Task B can receive rendevons port message:"); //输出:Task B can receive rendevons port message:
tm_putstring((UB*)calmsg);
} else {
tm_putstring((UB*)"Task B cannot receive rendevons port message:");
PorSamplePutErcd(rmsgsz);
}
tm_putstring((UB*)"\n");
PorSamplePutPor();
}
tm_putstring((UB*)"Task B will delete rendevons port;\n"); //输出:Task B will delete rendevons port;
tk_del_por(PorID);
tm_putstring((UB*)"Task B will terminate&delete task A;\n"); 输出:Task B will terminate&delete task A;
tk_ter_tsk(TaskID_A);
tk_del_tsk(TaskID_A);
tm_putstring((UB*)"Task B will terminate&delete self;\n"); //输出:Task B will terminate&delete self;
tk_exd_tsk();
}
void PorSamplePutErcd(ER ercd)
{
B ercds[10];
tm_putstring((UB*)"Error code is ");
switch(ercd){
case E_OK:
tm_putstring((UB*)"0:E_OK");
break;
case E_SYS:
tm_putstring((UB*)"-5:E_SYS");
break;
case E_NOSPT:
tm_putstring((UB*)"-9:E_NOSPT");
break;
case E_RSATR:
tm_putstring((UB*)"-11:E_LIMIT");
break;
case E_PAR:
tm_putstring((UB*)"-17:E_PAR");
break;
case E_ID:
tm_putstring((UB*)"-18:E_ID");
break;
case E_NOMEM:
tm_putstring((UB*)"-33:E_NOMEM");
break;
case E_LIMIT:
tm_putstring((UB*)"-34:E_LIMIT");
break;
case E_NOEXS:
tm_putstring((UB*)"-42:E_NOEXS");
break;
case E_TMOUT:
tm_putstring((UB*)"-50:E_TMOUT");
break;
default:
if(ercd>0){
tm_putstring((UB*)">0,UNKNOWN:");
ltostr((UW)ercd,ercds,16,10);
tm_putstring((UB*)ercds);
} else{
if(ercd<-67){
tm_putstring((UB*)"<-67,UNKNOWN");
} else {
tm_putstring((UB*)"0,-67,UNKNOWN");
}
}
break;
}
tm_putstring((UB*)"\n");
}
void PorSamplePutPor(void)
{
B info[10];
T_RPOR rpor;
tm_putstring((UB*)"Now rendevons port infor is wtsk atsk maxcmsz maxrmsz:"); //输出: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*)"\n");
}
UW ltostr(UW Val,B *pString,UB Base,UW Length)
{
B c, *pInit;
UB i,j;
UW Len,SwapLen;
*pString='\0';
pInit=pString;
if(Val == 0){
*pString++='0';
*pString='\0';
}
switch(Base){
/* Base is 10,convert to 10 base string */
case 10:
for(;Val>0;){
i=Val%Base;
Val=Val/Base;
*pString++=(B)((UB)'0'+i);
}
*pString='\0';
break;
/* Base is 16,convert to 16 base string */
case 16:
for(;Val>0;){
i=Val%Base;
Val=Val/Base;
if(i<=9){
*pString++=(B)((UB)'0'+i);
} else {
*pString++=(B)((UB)'A'+i-10);
}
}
*pString='\0';
break;
default:
break;
}
Len=strlen(pInit);
SwapLen=Len;
/* fill '\0' from string end to right */
if(Length>Len) {
for(i=Len;i<Length;i++){
*(pInit+i)='\0';
}
*(pInit+Length)='\0';
}
/* swap high and low order */
if(SwapLen>0){
for(i=0,j=SwapLen - 1;i<j;i++,j--){
c=*(pInit+i);
*(pInit+i)=*(pInit+j);
*(pInit+j)=c;
}
}
return SwapLen;
}
void Delay(void)
{
UINT i=0x1000000;
for(;i>0;i--);
}
运行结果:
Rendevons port sample create step 1-Create Rendevons port, ID is 1!
Rendevons port sample create step 2-Create Task A, ID is 2!
Rendevons port sample create step 3-Create Task B, ID is 3!
Rendevons port sample create step 4-Start Task B;
Task B will start task A;
Task B is running,input Command(e=exit):
若输入不是e:
Task B is now calling rendevons port 1times
Task B send rendevons port message: 0
Task A is now accept and reply rendevons port 1times
Task A reply rendevons port message: a
Now rendevons port infor is wtsk atsk maxcmsz maxrmsz: 0 0 80 80
Task B can receive rendevons port message: a
Now rendevons port infor is wtsk atsk maxcmsz maxrmsz: 0 2 80 80
若输入e:
Task B will delete rendevons port;
Task B will terminate&delete task A;
Task B will terminate&delete self;
创建任务A,B, 创建一个集合点,先启动任务B, 在任务B里面启动任务A,
任务B向消息数据包里写入0,然后任务调用集合端口,读取到0, 然后通过
回应集合点端口, 让任务B接收到字符a,这样一直循环执行下去;