运行于T-Engine(使用T-Kernel操作系统)的实例源码

源代码在线查看: sample1.c

软件大小: 64 K
上传用户: SnowCat
关键词: T-Engine T-Kernel 运行 操作系统
下载地址: 免注册下载 普通下载 VIP

相关代码

				/*				 * sample1.c - a sample application which uses a serial port				 *				 * Copyright (C) YRP Ubiquitous Networking Laboratory, 2005.				 */								#include 				#include 				#include 				#include 				#include "calc.h"								#define EXIT_COMMAND    "EXIT"				#define ERROR_MESSAGE   "Error!\n"								LOCAL INT exit_flag;				#define RBUFLEN         256				#define SBUFLEN         256				LOCAL UB rbuf[RBUFLEN];				LOCAL UB sbuf[SBUFLEN];								LOCAL ER set_serial_params(ID dd, RsMode *mode, RsFlow *flow)				{				  INT asize;				  ER ercd;								  if (dd < 0 || mode == NULL || flow == NULL) {				    return E_PAR;				  }								  /* set communication mode */				  ercd = tk_swri_dev(dd, DN_RSMODE, mode, sizeof(RsMode), &asize);				  if (ercd < E_OK) { return ercd; }								  /* set flow control */				  ercd = tk_swri_dev(dd, DN_RSFLOW, flow, sizeof(RsFlow), &asize);				  if (ercd < E_OK) { return ercd; }								  return E_OK;				}								LOCAL ER read_command(ID dd, UB *buf, INT *len)				{				  ER ercd;				  INT i, asize;								  for (i = 0; i < *len; i++) {				    ercd = tk_srea_dev(dd, 0, buf+i, 1, &asize);				    if (ercd < E_OK) {				      return ercd;				    }				    /* check if input is complete */				    if (*(rbuf+i) == '\n') {				      *(rbuf+i) = '\0';         /* replace LF with a null character */				      break;				    }				  }				  if (i == *len) {				    /* buffer is full. force terminating the string. */				    *(rbuf+i-1) = '\0';				  }				  *len = i-1;								  return E_OK;				}								LOCAL ER send_reply(ID dd, UB *buf, INT len)				{				  ER ercd;				  INT rest, asize;								  rest = len;				  while (rest > 0) {				    ercd = tk_swri_dev(dd, 0, buf+len-rest, rest, &asize);				    if (ercd < E_OK) {				      return ercd;				    }				    rest -= asize;				  }								  return E_OK;				}								EXPORT void tet_exit(void)				{				  exit_flag = 1;				}								EXPORT void tet_task(INT stacd, VP exinf)				{				  UB devnm[8];				  ID ptid, dd_rs;				  RsMode mode;				  RsFlow flow;				  ER ercd;								  /* get the parent's task ID from an argument */				  ptid = (ID) stacd;								  /* open the RS (serial) driver */				  strcpy(devnm, "vrsa");				  dd_rs = tk_opn_dev(devnm, TD_UPDATE | TD_EXCL); /* open the device */				  if (dd_rs < E_OK) { goto EXIT; }      /* open error */								  /* port settings */				  mode.parity   = 0;            /* parity: none        */				  mode.datalen  = 3;            /* data length: 8 bit  */				  mode.stopbits = 0;            /* stop bits: 1        */				  mode.rsv      = 0;            /* reserved (not used) */				  mode.baud     = 38400;        /* baud rate: 38400    */								  flow.rsv     = 0;             /* reserved (not used)               */				  flow.rcvxoff = 0;             /* XOFF: no                          */				  flow.csflow  = 0;             /* CTS flow control: no              */				  flow.rsflow  = 0;             /* RTS flow control: no              */				  flow.xonany  = 0;             /* XON with any character: no        */				  flow.sxflow  = 0;             /* XON/XOFF control in sending: no   */				  flow.rxflow  = 0;             /* XON/XOFF control in receiving: no */								  ercd = set_serial_params(dd_rs, &mode, &flow);				  if (ercd != E_OK) { goto EXIT; }      /* error in setting parameters */								  /* main loop until exit_flag is on */				  exit_flag = 0;				  while (exit_flag == 0) {				    INT rlen, slen;								    rlen = RBUFLEN;				    ercd = read_command(dd_rs, rbuf, &rlen);				    if (ercd != E_OK) {				      continue;				    }								    if (strcmp(rbuf, EXIT_COMMAND) == 0) {				      tet_exit();				      continue;				    }								    ercd = tet_calc(rbuf, sbuf);				    if (ercd != E_OK) {				      /* set error message */				      strcpy(sbuf, ERROR_MESSAGE);				    }								    slen = strlen(sbuf);				    ercd = send_reply(dd_rs, sbuf, slen);				  }								  /* close the RS driver */				  ercd = tk_cls_dev(dd_rs, 0);				  if (ercd < E_OK) { goto EXIT; }       /* close error */								EXIT:				  /* wake up the parent */				  tk_wup_tsk(ptid);				  /* stop and remove this task */				  tk_exd_tsk();				}							

相关资源