📄 task_test.cpp
字号:
/*head files*/
#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "semLib.h"
#include "taskLib.h"
#include "msgQLib.h"
#include "string.h"
#include "ioLib.h"
#include "sysLib.h"
#include "logLib.h"
/*类型定义*/
typedef struct _MESSAGE
{
int warn_num;
char * warn_text;
}WARNMESSAGE;
/*宏定义*/
#define STACK_SIZE (20000)
#define MAX_MSGS (10)
#define MAX_MSG_LEN1 sizeof(WARNMESSAGE)
#define MAX_MSG_LEN2 (100)
#define WARN 1
/*全局变量*/
int tidManage;
int tidReceive;
int tidSend;
MSG_Q_ID ManageQid;
MSG_Q_ID SendQid;
SEM_ID SemId;
/*函数声明*/
STATUS progstart(void);
STATUS manage(void);
STATUS receive(void);
STATUS send(void);
void getcommand (char * command);
void progstop(void);
/********************************************************/
STATUS progstart(void)
{
ManageQid=msgQCreate(MAX_MSGS,MAX_MSG_LEN1,MSG_Q_FIFO);/*创建消息队列*/
if(ManageQid==NULL)
{
return (ERROR);
}
SendQid=msgQCreate(MAX_MSGS,MAX_MSG_LEN2,MSG_Q_FIFO);
if(SendQid==NULL)
{
return (ERROR);
}
SemId=semBCreate(SEM_Q_FIFO,SEM_EMPTY); /*创建信号量*/
tidReceive=taskSpawn("receivetask",220,0,STACK_SIZE,FUNCPTR(receive),
0,0,0,0,0,0,0,0,0,0); /*创建任务*/
tidSend=taskSpawn("sendtask",200,0,STACK_SIZE,FUNCPTR(send),
0,0,0,0,0,0,0,0,0,0);
tidManage=taskSpawn("managetask",180,0,STACK_SIZE,FUNCPTR(manage),
0,0,0,0,0,0,0,0,0,0);
return OK;
}
/***************************************************************/
STATUS receive(void)
{
char inputstring[20]; //接受命令缓冲
char printmessage[100]; //发送打印消息缓冲
WARNMESSAGE warnmessage; //发送告警消息缓冲
char warn_exit[50];
char warn_undef[50];
int a,b,sum_result,sub_result,mul_result,div_result,sign;
semTake(SemId,WAIT_FOREVER);
while(1)
{
sign=0;
getcommand(inputstring);
if(strncmp(inputstring,"input a",7)==0)
{
int i=8,temp1=0,temp2=0;
while(inputstring[i]!=10)
{
temp1=temp1*10;
temp2=inputstring[i]-48;
temp1=temp1+temp2;
i++;
}
a=temp1;
sprintf(printmessage,"input successfully:a=%d\n",a);
}
else if(strncmp(inputstring,"input b",7)==0)
{
int i=8,temp1=0,temp2=0;
while(inputstring[i]!=10)
{
temp1=temp1*10;
temp2=inputstring[i]-48;
temp1=temp1+temp2;
i++;
}
b=temp1;
sprintf(printmessage,"input successfully:b=%d\n",b);
}
else if(strncmp(inputstring,"add",3)==0)
{
sum_result=a+b;
printf("%d,%d,%d\n",a,b,sum_result);
sprintf(printmessage,"operate successfully:a+b=%d\n",sum_result);
}
else if(strncmp(inputstring,"mul",3)==0)
{
mul_result=a*b;
sprintf(printmessage,"operate successfully:a*b=%d\n",mul_result);
}
else if(strncmp(inputstring,"sub",3)==0)
{
sub_result=a-b;
sprintf(printmessage,"operate successfully:a-b=%d\n",sub_result);
}
else if(strncmp(inputstring,"div",3)==0)
{
div_result=a/b;
sprintf(printmessage,"operate successfully:a/b=%d\n",div_result);
}
else if(strncmp(inputstring,"exit",4)==0)
{
sign=WARN;
sprintf(warn_exit,"the program will exit!GODBYE!\n");
warnmessage.warn_num=2;
warnmessage.warn_text=warn_exit;
}
else
{
sign=WARN;
sprintf(warn_undef,"the command is undefined!\n");
warnmessage.warn_num=1;
warnmessage.warn_text=warn_undef;
}
if(sign)
{
if(msgQSend(ManageQid,(char *)&warnmessage,MAX_MSG_LEN1,WAIT_FOREVER,MSG_PRI_NORMAL)==ERROR)
{return (ERROR);}
}
else if(msgQSend(SendQid,printmessage,MAX_MSG_LEN2,WAIT_FOREVER,MSG_PRI_NORMAL)==ERROR)
{return (ERROR);}
}
return OK;
}
/********************************************************************/
void getcommand (char * command)
{
int fd,num;
fd=open("/tyCo/0",O_RDWR,0);
num=read(fd,command,20);
close(fd);
}
/*********************************************************************/
STATUS manage(void)
{
int fd;
WARNMESSAGE warnmessage; //接受告警信息缓冲
char warn_temp[100]; //
fd=open("/tyCo/0",O_RDWR,0); //打印程序提示内容
write(fd,"this program can accept commands as follows:\n",45);
write(fd,"input a(b) const:inupt one number to a(b)\n",43);
write(fd,"add: get a result of a add b\n",29);
write(fd,"sub: get a result of a sub b\n",29);
write(fd,"mul: get a result of a mul b\n",29);
write(fd,"div: get a result of a div b\n",29);
write(fd,"exit: exit the program\n",23);
write(fd,"please input the command:",25);
close(fd);
//printf("1");
taskDelay(sysClkRateGet()); //等待接受命令任务与发送任务运行
semFlush(SemId); //同步系统中其他Task 的启动同步
while(1)
{
if(msgQReceive(ManageQid,(char*)&warnmessage,MAX_MSG_LEN1,WAIT_FOREVER)==ERROR)
{
return (ERROR);
}
sprintf(warn_temp,"warning%d:%s",warnmessage.warn_num,warnmessage.warn_text);
fd=open("/tyCo/0",2,0);
logFdSet(fd);
//write(fd,warn_temp,strlen(warn_temp));
logMsg(warn_temp,0,0,0,0,0,0);
if(warnmessage.warn_num==2)
progstop();
else
write(fd,"please input the command:",25);
// logMsg("please input the command:",0,0,0,0,0,0);
close(fd);
}
return OK;
}
/****************************************************************************/
STATUS send(void)
{
int fd;
WARNMESSAGE warnmessage;
char message[100];
char warn_temp[100];
semTake(SemId,WAIT_FOREVER);
while(1)
{
if(msgQReceive(SendQid,message,MAX_MSG_LEN2,WAIT_FOREVER)==ERROR)
{
return ERROR;
}
if(strlen(message)<1||strlen(message)>100)
{
warnmessage.warn_num=3;
sprintf(warn_temp,"the command is too short!\n");
warnmessage.warn_text=warn_temp;
if( msgQSend(ManageQid,(char *)&warnmessage,MAX_MSG_LEN1,WAIT_FOREVER,MSG_PRI_NORMAL)==ERROR)
{
return ERROR;
}
}
else
{
fd=open("/tyCo/0",O_RDWR,0);
write(fd,message,strlen(message));
write(fd,"please input the command:",25);
close(fd);
}
}
return OK;
}
/******************************************************************************/
void progstop(void)
{
int fd;
msgQDelete(ManageQid);
msgQDelete(SendQid);
semDelete(SemId);
taskDelete(tidReceive);
taskDelete(tidSend);
fd=open("/tyCo/0",O_RDWR,0);
write(fd,"the program has exit!\n",22);
close(fd);
taskDelete(tidManage);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -