后来用select来读四个串口,但是也遇到了同样的问题。
请看下面的源码:
//串口类的打开串口函数:m_hSerial相当于fd.
//此类的其它函数略。
BOOL CSerial::Open(int nPort, int nBaud)
{
char szPort[32];
if(m_hSerial != ERROR)
{
printf("serial %d already opened.\n", nPort);
return FALSE;
}
sprintf(szPort, "/tyCo/%d", nPort - 1);
if((m_hSerial = open(szPort, O_RDWR, 0)) == ERROR)
{
printf("open serial %d error.\n", nPort);
return FALSE;
}
ioctl(m_hSerial, FIOSETOPTIONS, OPT_RAW);
if((ioctl(m_hSerial, FIOBAUDRATE, nBaud)) == ERROR)
{
printf("set serial %d baud error.\n", nPort);
close(m_hSerial);
return FALSE;
}
if((ioctl(m_hSerial, FIORBUFSET, SERIAL_MAX_BUFFER)) == ERROR)
{
printf("set serial %d read buffer size error.\n", nPort);
close(m_hSerial);
return FALSE;
}
if((ioctl(m_hSerial, FIOWBUFSET, SERIAL_MAX_BUFFER)) == ERROR)
{
printf("set serial %d read buffer size error.\n", nPort);
close(m_hSerial);
return FALSE;
}
printf( "Serial %d open success!\n", nPort );
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//主程序;
简单描述:用select来读四个串口中的数据,然后打印出来。
#include <vxWorks.h>
#include <taskLib.h>
#include <selectLib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.>
#include "Serial.h"
#define MAX_FDS 4
typedef unsigned char byte;
CSerial g_Serial1;
CSerial g_Serial2;
CSerial g_Serial3;
CSerial g_Serial4;
/************************************************************************
* selServer - reads data as it becomes available from four different serials
*
* Opens four serials fds, reading from whichever becomes available.
*/
STATUS selSerials (void)
{
struct fd_set readFds; /* bit mask of fds to read from */
int fds[MAX_FDS]; /* array of fds on which to pend */
int width; /* number of fds on which to pend */
int i; /* index for fd array */
int len = 0;
byte buffer[SERIAL_MAX_BUFFER]; /* buffer for data that is read */
int nBytesUnread = 0;
/* open file descriptors */
if (g_Serial1.Open (1, 19200) == FALSE)
{
return (ERROR);
}
else
{
fds[0] = g_Serial1.m_hSerial;
}
if (g_Serial2.Open (2, 4800) == FALSE)
{
return (ERROR);
}
else
{
fds[1] = g_Serial2.m_hSerial;
}
if (g_Serial3.Open (3, 4800) == FALSE)
{
return (ERROR);
}
else
{
fds[2] = g_Serial3.m_hSerial;
}
if (g_Serial4.Open (4, 19200) == FALSE)
{
return (ERROR);
}
else
{
fds[3] = g_Serial4.m_hSerial;
}
/* Discards all the bytes currently in both the input and the output buffers */
for(i = 0; i < 4; i++)
{
ioctl(fds[i], FIOFLUSH, 0);
}
/* loop forever reading data and servicing clients */
while(true)
{
/* clear bits in read bit mask */
FD_ZERO (&readFds);
/* initialize read bit mask */
for(i = 0; i < 4; i++)
{
FD_SET (fds[i], &readFds);
}
/* initialize write bit mask */
FD_SET (fds[0], &writeFds);
width = fds[0];
for(int j = 1; j < MAX_FDS; j++)
{
if(fds[j] > width)
{
width = fds[j];
}
}
width++;
/* pend, waiting for one or more fds to become ready */
if (select (width, &readFds, NULL, NULL, NULL) == ERROR)
{
perror("The select of serials is ERROR\n");
return (ERROR);
}
/* step through array and read from fds that are ready */
for (i = 0; i < MAX_FDS; i++)
{
/* check if this fd has data to read */
if (FD_ISSET (fds[i], &readFds))
{
/* typically read from fd now that it is ready */
memset(buffer, 0, sizeof(buffer));
ioctl (fds[i], FIONREAD, int (&nBytesUnread));
if(nBytesUnread == 0)
{
continue;
}
len = read (fds[i], buffer, SERIAL_MAX_BUFFER);
if(ioctl(fds[i], FIORFLUSH, 0) == ERROR)
{
return ERROR;
}
if(len <= 0)
{
continue;
}
switch(i)
{
case 0:
printf("\n COM1 %d, %s\n", len, buffer);
break;
case 1:
printf("\n COM2 %d, %s\n", len, buffer);
break;
case 2:
printf("\n COM3 %d, %s\n", len, buffer);
break;
case 3:
printf("\n COM4 %d, %s\n", len, buffer);
break;
default:
break;
}
}
}
}
}
//程序入口:
extern "C"
{
int zmap()
{
if (taskSpawn("SERIAL_SEL", 90, VX_FP_TASK, 20000,
(FUNCPTR)selSerials, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) != ERROR)
{
return (0);
}
else
{
return (-1);
}
}
}