📄 exp3.c
字号:
#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "semLib.h"
#include "taskLib.h"
#include "sysLib.h"
#include "exp3.h"
#include "logLib.h"
/* 宏定义 */
#define STACK_SIZE 2000 /* 任务堆栈大小为2000bytes */
/* 全局变量 */
SEM_ID dataSemId; /* 同步信号量 */
int tidServer; /* Send Task任务id */
int tidClient[NUM_CLIENT]; /* Receive Task任务id */
MSG_Q_ID msgServer;
MSG_Q_ID msgClient[NUM_CLIENT];
/* 函数声明 */
STATUS progStart(void);
STATUS Server(void);
void Client(int id);
void ServerInit(void);
void ClientInit(int id);
void progStop(void);
void Console_In(void);
void Led(void);
void Console_Out(void);
/******************************************************************
* progStart - start程序
* 负责创建Send Task和Receive Task,并开始运行
* RETURNS: N/A
*/
STATUS progStart(void)
{
int id;
/* 创建信号量 */
dataSemId = semBCreate(SEM_Q_FIFO , SEM_EMPTY);
msgServer = msgQCreate(MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY);
if (msgServer == NULL)
{
return (ERROR);
}
for (id = 0; id < NUM_CLIENT; id++)
{
msgClient[id] = msgQCreate(MAX_MSGS, MAX_MSG_LEN, MSG_Q_PRIORITY);
if (msgClient[id] == NULL)
{
return (ERROR);
}
}
/* 创建任务 */
for (id = 0; id < NUM_CLIENT; id++)
{
char tempName[20];
sprintf(tempName, "tClient%d", id);
tidClient[id] = taskSpawn (tempName, 200, 0, STACK_SIZE,
(FUNCPTR)Client,
id, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
tidServer = taskSpawn ("tServer", 220, 0, STACK_SIZE, (FUNCPTR)Server,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
printf("Step1\n");
return (OK);
}
/******************************************************************
* taskSend - 发送数据任务
* 每两秒钟释放一次信号量dataSemId,用来模拟收到数据
*/
STATUS Server(void)
{
MESSAGE rxMsg; /* Reciept*/
MESSAGE txMsg; /*Send*/
/* Send Task的初始化 */
ServerInit();
/* 主循环 */
FOREVER
{
if (msgQReceive(msgServer, (char *)&rxMsg, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)
{
return (ERROR);
}
if (rxMsg.mRecvId != MID_SERVER)
{
return (ERROR);
}
if (rxMsg.mSendId < 0 || rxMsg.mSendId >2)
{
return (ERROR);
}
if (rxMsg.mData == SHUTDOWN)
progStop();
else
logMsg("Warning: From tClient%d\n",rxMsg.mSendId, 0, 0, 0, 0, 0);
}
return (OK);
}
/******************************************************************
* taskReceive - 接收数据任务
* 等待信号量dataSemId,用来判断是否收到数据
*/
void Client(int id)
{
/* Receive Task的初始化 */
ClientInit(id);
/* 主循环 */
FOREVER
{
/* 等待信号量(收数据) */
switch (id)
{
case 0:/*Task 2--Console_In*/
/*printf("\nTask 2\n");*/
Console_In();
/*taskDelay(10000);*/
break;
case 1:/*Task 3--Led*/
/*printf("\nTask 3\n");*/
Led();
/*taskDelay(10000);*/
break;
case 2:/*Task 4--Console_Out*/
/*printf("\nTask 4\n");*/
Console_Out();
/*taskDelay(10000);*/
break;
}
}
return;
}
/******************************************************************
* SendInit - 初始化Send Task
* 初始化Send Task。
*/
void ServerInit(void)
{
/*printf("S In\n");
taskDelay(1000);*/
/* 初始化代码 */
semFlush(dataSemId);
return;
}
/******************************************************************
* ClientInit - 初始化Client Task
* 初始化Client Task。
*/
void ClientInit(int id)
{
/*printf("C%d In\n",id);*/
/* 初始化代码 */
semTake(dataSemId, WAIT_FOREVER);
;
return;
}
void Console_In(void)
{
char aCommand[20];
int aCom;
MESSAGE txMsg;
txMsg.mSendId = MID_TASK2;
taskDelay(10);
printf("\nEnter your command:");
scanf("%s",aCommand);
if (strcmp(aCommand, "Led") == 0)
aCom = 0;
else if (strcmp(aCommand, "Out") == 0)
aCom = 1;
else if (strcmp(aCommand, "Shut") == 0)
aCom = 2;
else aCom =-1;
switch(aCom)
{
case 0:/*Task 3*/
txMsg.mRecvId = MID_TASK3;
txMsg.mData = LEDON;
if (msgQSend(msgClient[MID_TASK3], (char *)&txMsg,
sizeof(MESSAGE), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return;
break;
case 1:/*Task 4*/
txMsg.mRecvId = MID_TASK4;
txMsg.mData = OUT;
if (msgQSend(msgClient[MID_TASK4], (char *)&txMsg,
sizeof(MESSAGE), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return;
break;
case 2:/*End Whole */
txMsg.mRecvId = MID_SERVER;
txMsg.mData = SHUTDOWN;
if (msgQSend(msgServer, (char *)&txMsg,
sizeof(MESSAGE), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return;
break;
default:/*Warning*/
txMsg.mRecvId = MID_SERVER;
txMsg.mData = WARNING;
if (msgQSend(msgServer, (char *)&txMsg,
sizeof(MESSAGE), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return;
break;
}
}
void Led(void)
{
MESSAGE rxMsg;
if (msgQReceive(msgClient[MID_TASK3], (char *)&rxMsg, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)
{
return;
}
if (rxMsg.mData == LEDON)
printf("\nLed is on\n");
}
void Console_Out(void)
{
MESSAGE rxMsg;
if (msgQReceive(msgClient[MID_TASK4], (char *)&rxMsg, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)
{
return;
}
if (rxMsg.mData == OUT)
printf("\nOutput\n");
}
/******************************************************************
* progStop - 终止程序运行
* 删除信号量,删除所以任务,终止程序运行。
*/
void progStop(void)
{
int i;
/* 删除信号量 */
semDelete(dataSemId);
/* 删除所有信号量 */
for (i = 0; i < NUM_CLIENT; i++)
taskDelete(tidClient[i]);
printf("The End\n");
taskDelete(tidServer);
printf("The End\n");
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -