这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » 在vxWorks下如何以中断方式读写串口?

共8条 1/1 1 跳转至

在vxWorks下如何以中断方式读写串口?

菜鸟
2005-06-15 17:45:54     打赏

我的目标机是pentium3,从串口设备(接COM1)读数据,使用串口调试精灵每秒钟发一次数,我想通过中断方式读取数据,每来一个数据我就中断一次。用了二进制信号量来同步中断和接受任务,中断服务程序中只有semGive(semRecv ),在tyRecv( )里面用semTake(semRecv )。问题是中断服务程序执行了,在接收任务中的信号量获取不成功。程序如下:

/*includes*/ #include "vxworks.h" #include "ioLib.h" #include "stdio.h" #include "selectLib.h" #include "errnoLib.h" #include "intLib.h" #include "taskLib.h" #include "logLib.h" #include "string.h" #include "arch/i86/ivI86.h" #include "semLib.h" /*function status*/ void interruptHandler(); int tyRecv(int,SEM_ID);

/* globals */ #define INT_NUM_IRQ0 0x20 #define COM1_INT_LVL 0x04 #define INT_NUM_COM1 (INT_NUM_IRQ0 + COM1_INT_LVL) #define RECV_PRIORITY 200 #define STACK_SIZE 1024*40 #define BUF_SIZE 1024 SEM_ID semRecv; /*main function*/ int gpsmain( ) { int taskId; STATUS connected; /*open COM1*/ int SerialDevFd=open("/tyCo/0",O_RDWR,0); if(ERROR==SerialDevFd) { logMsg("open_com1:Unable to open/tyCo/0",0,0,0,0,0,0); return 0; }

if(ERROR==ioctl(SerialDevFd,FIOSETOPTIONS,OPT_RAW)) { logMsg("can not set tty options!\n",0,0,0,0,0,0); return 0; }

/*Set baudrate*/ if(ERROR==ioctl(SerialDevFd,FIOBAUDRATE,38400)) { logMsg("can not set BAUDRATE!\n",0,0,0,0,0,0); return 0; } /*flush the buffer*/ if(ERROR==ioctl(SerialDevFd,FIOFLUSH,0)) { logMsg("can not flush the buffer!\n",0,0,0,0,0,0); return 0; } semRecv=semBCreate(SEM_Q_FIFO,SEM_EMPTY);

intConnect(INUM_TO_IVEC(INT_NUM_COM1),(VOIDFUNCPTR)interruptHandler,semRecv); sysIntEnablePIC(COM1_INT_LVL);

/*Start receiving task*/ if((taskId=taskSpawn("recv",RECV_PRIORITY,0,STACK_SIZE,(FUNCPTR)tyRecv,SerialDevFd,0,0,0,0,0,0,0,0,0))==ERROR) logMsg("taskSpawn tyRecv failed!",0,0,0,0,0,0); }

/*receive data routine*/ int tyRecv(int SerialDevFd,SEM_ID semRecv) { int length=0; char rece_buf[BUF_SIZE]; char destin_buff[BUF_SIZE]; for(length=0;length<1024;length++) { if(semTake(semRecv,WAIT_FOREVER)==ERROR) { logMsg("sem take failed\n",0,0,0,0,0,0); return; }

if (NULL!=read(SerialDevFd,rece_buf,1)) printf("%s",rece_buf); if (length==1023) { bcopyBytes(rece_buf,destin_buff,BUF_SIZE); length=0; }

/*taskDelay(WAIT_FOREVER);*/ } return; }

/*interrupt service routine*/ void interruptHandler( ) { int level=intLock(); /*logMsg("acknowledging interrupt!\n",0,0,0,0,0,0);*/ if(semGive(semRecv)==OK) {logMsg("give Recv sem OK!",0,0,0,0,0,0); return ; } intUnlock(level); }

向各位高手请教!小弟不胜感激




关键词: vxWorks     下如     何以     中断     方式     读写     串口    

菜鸟
2005-06-15 20:39:00     打赏
2楼

用select()也可以,它类似事件机制,只要串口有数据就读,但是不知道怎么用中断?


菜鸟
2005-06-16 01:40:00     打赏
3楼

/*interrupt service routine*/ void interruptHandler( ) { int level=intLock(); /*logMsg("acknowledging interrupt!\n",0,0,0,0,0,0);*/ if(semGive(semRecv)==OK) {logMsg("give Recv sem OK!",0,0,0,0,0,0); return ; } intUnlock(level); }

这段代码有点问题吧:

如果segGive() == OK 的话, 你直接return , 在return之前都没有intUnlock(level).

改称这样试试:

void interruptHandler( ) { int level=intLock(); if(semGive(semRecv)==OK) {

logMsg("give Recv sem OK!",0,0,0,0,0,0); } intUnlock(level); }


菜鸟
2005-06-16 07:34:00     打赏
4楼
好的,我试一试。谢谢提醒!

菜鸟
2005-06-17 20:48:00     打赏
5楼

semRecv是ISR带的参数。没有问题啊!


菜鸟
2005-10-26 01:23:00     打赏
6楼

可能内有释放中断资源


菜鸟
2005-10-27 19:02:00     打赏
7楼
ded

菜鸟
2005-10-27 20:59:00     打赏
8楼
select函数在哪个库里,有说明吗?

共8条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]