我采用的是轮询的方式,如果检测到以b开头以*结尾的数据就接收,但是波特率只能到2400,设为9600时接收几次后就会出现几比特的丢失,波特率设的越大丢数的情况出现的就越频繁,这是为什么,谢谢。
#include "vxWorks.h"
#include "stdio.h"
#include "stdLib.h"
#include "hostLib.h"
#include "sysLib.h"
#include "semLib.h"
#include "taskLib.h"
#include "fioLib.h"
#include "iosLib.h"
#include "nfsLib.h"
#include "selectLib.h"
#include "tyLib.h"
#include "logLib.h"
#include "sioLib.h"
#define SEIRAL_MAX_BUFFER 2048
struct fd_set readFds;
struct fd_set writeFds;
int serialFd;
int real_len;
char buffer1[1024];
int width;
int nBytesUnread=0;
BOOL flag_R;
BOOL flag_T;
STATUS seriInit()
{
if((serialFd=open("/tyCo/1",O_RDWR,0))==ERROR)
{
printf("Error open serial device\n");
return(ERROR);
}
else
printf("Open serial device sucess\n");
ioctl(serialFd,FIOSETOPTIONS,OPT_TERMINAL&OPT_RAW);
ioctl(serialFd,FIOBAUDRATE,9600);
ioctl(serialFd,SIO_HW_OPTS_SET,CS8);
ioctl(serialFd,FIORBUFSET,SEIRAL_MAX_BUFFER );
ioctl(serialFd,FIOWBUFSET,SEIRAL_MAX_BUFFER );
ioctl(serialFd,FIOFLUSH,0);
}
STATUS seriRecv()
{
int i;
int len;
int nBytesUnread;
char temp;
int rev_flg=0;
real_len=0;
while(1)
{
FD_ZERO(&readFds);
FD_SET(serialFd,&readFds);
width=serialFd+1;
if(select(width,&readFds,NULL,NULL,NULL)==ERROR)
{
perror("the select of serials is error\n");
return(ERROR);
}
ioctl(serialFd,FIONREAD,&nBytesUnread);
if(nBytesUnread==0)
{
continue;
}
if(read(serialFd,&temp,1)==ERROR)
{
perror("the read error\n");
continue;
}
printf("%c",temp);
if(temp=='b')
{
rev_flg=1;
real_len=0;
continue;
}
if(rev_flg!=1)
continue;
if(temp!='*')
{
buffer1[real_len]=temp;
real_len++;
continue;
}
else if(temp=='*')
{
rev_flg=0;
for(i=0;i<real_len;i++)
{
printf("%c",buffer1[i]);
}
printf("\n");
printf("reak_len=%d\n",real_len);
flag_R=1;
if(ioctl(serialFd,FIORFLUSH,0)==ERROR)
{
return ERROR;
}
printf("\n");
}
else
continue;
printf("\n");
}
}
STATUS serial()
{
seriInit();
seriRecv();
}
void seriStop()
{
if(serialFd)
close(serialFd);
}