1、问题综述
在一款车载记录仪中,车载仪即可以通过无线传输的方式,也可以用有线(J1939中的Transport Protecol)传输的方式与办公室软件联系(上传记录的数据、下传命令、修改参数等)。因此,车载仪中的通讯软件模块将同时与两个以上通讯介质联络。ZRTOS 中 Select()API 将很好地handle这种情况。以下是C代码:
#define COM_J1939 0
#define COM_WIRELESS 1
#define _SEL_TIME_OUT 3000 //30s time out
static fd_set com_ch; //container for select
static RTOS_FD_TYPE com_in[2];
void com_init(void) //communication initialization
{
current_COM = COM_DISPATCH_LOGON;
RTOS_FD_ZERO(&com_ch); //zero file descriptor - select() usage container
com_in[COM_J1939] = open(J1939_COM_CH); //open J1939 Transport protecol com channel;
RTOS_FD_SET(getFds(com_in[COM_J1939]), &com_ch);//put J1939 com channel into select() usage container
com_in[COM_WIRELESS] = open(WIRELESS_COM_CH);//open wireless com channel;
RTOS_FD_SET(getFds(com_in[COM_WIRELESS]), &com_ch);//put wireless com channel into select usage container
com_process.sem = rtos_semBCreate(0, SEM_EMPTY); //greate a semaphone
rtos_taskSpawn(COM_TASK_PRORITY-2, (RTOS_FUNCPTR) com_tasks, 0);//spawn a task to handle communication channels
rtos_taskSpawn(COM_TASK_PRORITY-2, (RTOS_FUNCPTR) process_com_message, 0);//spawn a task to process communication messages
}
static void com_tasks(void) //the task to handle communication channels
{
long sel, size;
sel = select(&com_ch, _SEL_TIME_OUT); //select() with 30 s time out
#define _5MIN_TIME_OUT (10) //if a cmd did not finish in 5 min. it was a fault cmd, get out any way
//if the connection was a j1939, the time out is 5 sec.
if (sel == 0) { //time out
//do nothing or may use as a counter timer, when the number of the timeout reach a critical value, do something...
} else { //COM available
if (RTOS_FD_ISSET(getFds(com_in[COM_J1939]), &com_ch)) { //J1939 chaneel active
size = read(com_in[COM_J1939], (char *) com_receive, COM_RCV_BUFFER_SIZE);
} else { //wireless
size = read(com_in[COM_WIRELESS], (char *) com_receive, COM_RCV_BUFFER_SIZE);
}
if (size > 0) {
rtos_semGive(com_process.sem); //wake up process-communication-message task
}
}
RTOS_FD_ZERO(&com_ch);//zero the select() container
}
static void process_com_message(void)//process-communication-message task
{
rtos_semTake(com_process.sem, WAIT_FOREVER);
//your process code here ...
//...
}
2、结论
ZRTOS中的select() 函数的使用方式和效果与vxWorks、UNIX基本相同。
用户461316 2008-9-6 09:49
用户3266 2008-9-5 14:00