这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » select串口通讯容易丢失数据

共4条 1/1 1 跳转至

select串口通讯容易丢失数据

菜鸟
2006-02-15 22:46:52     打赏

串口设置:

ioctl(SerialDevFd, FIOBAUDRATE, 115200);
/* set to 'line‘ mode */
ioctl(SerialDevFd, FIOSETOPTIONS, OPT_TERMINAL & OPT_LINE );
/* set to data bits*/
ioctl(SerialDevFd, SIO_HW_OPTS_SET,8);

接受:

FD_ZERO (&readFds);
/* initialize bit mask */
FD_SET (SerialDevFd, &readFds);
width = SerialDevFd + 1;

/* loop forever reading data and servicing clients */
FOREVER
{
/* pend, waiting for one or more fds to become ready */
if ((numbit=select (width, &readFds, NULL, NULL, NULL)) == ERROR)
printf("typereadr: ERROR\n");
printf("typereadr: number=%d\n",numbit);

/* check if this fd has data to read */
if (FD_ISSET (SerialDevFd, &readFds))
{
FOREVER
{
bzero (buffer, MAX_DATA);
/* typically read from fd now that it is ready */
if ( (DataLen = read (SerialDevFd, buffer, MAX_DATA))==ERROR )
{
printf("typereadr: ERROR Reading\n");
}
/* normally service request, for this example print it */
printf ("typereadr: Reading...DataLen=%d %s\n",DataLen,(char*)buffer );
if(DataLen == 0)
break;
}
printf("typereadr: Read OK!");
}
}

出现问题:接受打印的数据很容易丢失?谢谢关注!!1




关键词: select     串口     通讯     容易     丢失     数据    

菜鸟
2006-02-17 00:53:00     打赏
2楼
我也用过确实存在丢失数据的现象,后来就改用中断接收了

菜鸟
2006-02-17 00:59:00     打赏
3楼
能给个中断接受的例子么?

菜鸟
2007-07-22 02:02:00     打赏
4楼

#include "config.h"
#include "msgQLib.h" /* 消息队列头文件 */

#define COM1ADDR 0x3F8 /* COM1基地址 */

#define COM1_INT_LVL 0x04 /* IRQ4 */
#define INT_NUM_COM1 (INT_NUM_IRQ0 + COM1_INT_LVL)

void COM1ISRHandler(int ctrl);

MSG_Q_ID SerialMsgQId; /* 串口接收消息队列ID定义 */

void Com1Init(void)
{
unsigned char old_state;

old_state = sysInByte(0x21);
sysOutByte(0x21, old_state|0x10);

sysOutByte(COM1ADDR+3, 0x80);
sysOutByte(COM1ADDR+0, 0x0c);
sysOutByte(COM1ADDR+1, 0x00);
sysOutByte(COM1ADDR+3, 0x03);
sysOutByte(COM1ADDR+4, 0x0b);
sysOutByte(COM1ADDR+1, 0x01);

(void)intConnect(INUM_TO_IVEC(INT_NUM_COM1), (VOIDFUNCPTR)COM1ISRHandler, 0);
sysIntEnablePIC(COM1_INT_LVL); /* 中断使能 */

sysOutByte(0x21, old_state&0xEF);
}

void COM1ISRHandler(int ctrl)
{
static unsigned char SerialState = 0;
unsigned char dat;
unsigned char msgbuf[5];

sysIntDisablePIC(COM1_INT_LVL);
if(sysInByte(COM1ADDR+2) == 0x04)
{
dat = sysInByte(COM1ADDR);
/*logMsg("Com1 Interrupt: %c\n", dat, 0, 0, 0, 0, 0);*/
if(SerialState == 0)
{
if(dat == 'M')
{
SerialState = 1;
}
}
else if(SerialState == 1)
{
if(dat == 'i' || dat == 'O' || dat == 'S' || dat == 'N' || dat == 'B' || dat == 'T' || dat == 'f')
{
SerialState = 0;
msgbuf[0] = 'M';
msgbuf[1] = dat;
msgbuf[2] = '\0';
if(msgQSend(SerialMsgQId, msgbuf, 3, NO_WAIT, MSG_PRI_NORMAL) == ERROR)
{
logMsg("Com1 Message Send Error.\n", 0, 0, 0, 0, 0, 0);
}
}
else if(dat == 'P')
{
SerialState = 2;
}
else
{
SerialState = 0;
}
}
else if(SerialState == 2)
{
SerialState = 3;
msgbuf[2] = dat;
}
else if(SerialState == 3)
{
SerialState = 0;
msgbuf[0] = 'M';
msgbuf[1] = 'P';
msgbuf[3] = dat;
msgbuf[4] = '\0';
if(msgQSend(SerialMsgQId, msgbuf, 5, NO_WAIT, MSG_PRI_NORMAL) == ERROR)
{
logMsg("Com1 Message Send Error.\n", 0, 0, 0, 0, 0, 0);
}
}
}
sysOutByte(0x20, 0x20);
sysIntEnablePIC(COM1_INT_LVL);
}

STATUS SerialSend(char ComWrBuffer[], UINT DataLen)
{
UINT i, j;
for(i=0; i<DataLen; i++)
{
j = 0;
while(!(sysInByte(COM1ADDR+5)&0x20) && j<65535)
{
j++;
}
if(j == 65535)
return ERROR;
sysOutByte(COM1ADDR, ComWrBuffer[i]);
}
return OK;
}


共4条 1/1 1 跳转至

回复

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