求大侠指点,小弟现在需要用到2440的串口,但是串口在接收的的时候,总是不能连续接收,接收几次就不接收了,其做过的大侠指点一下。测试程序如下:
# include <stdio.h>
# include <stdlib.h>
# include <termio.h>
# include <unistd.h>
# include <fcntl.h>
# include <getopt.h>
# include <time.h>
# include <errno.h>
# include <string.h>
# define max(x,y) ( ((x) >= (y)) ? (x) : (y) )
static void Error(const char *Msg)
{
fprintf (stderr, "%s\n", Msg);
fprintf (stderr, "strerror() is %s\n", strerror(errno));
exit(1);
}
static void Warning(const char *Msg)
{
fprintf (stderr, "Warning: %s\n", Msg);
}
static inline void WaitFdWriteable(int Fd)
{
fd_set WriteSetFD;
FD_ZERO(&WriteSetFD);
FD_SET(Fd, &WriteSetFD);
if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) < 0) {
Error(strerror(errno));
}
}
unsigned char Char = 0;
int OutputHex = 0;
void OutputStdChar(FILE *File)
{
char Buffer[10];
int Len = sprintf(Buffer, OutputHex ? "%.2X " : "%c", Char);
fwrite(Buffer, 1, Len, File);
}
int main(int argc, char **argv)
{
int CommFd, TtyFd;
struct termios TtyAttr;
struct termios BackupTtyAttr;
int DeviceSpeed = B115200;
int TtySpeed = B115200;
int ByteBits = CS8;
const char *DeviceName ;
const char *TtyName;
int OutputToStdout;
DeviceName = "/dev/ttySAC1";
TtyName = "/dev/tty";
OutputToStdout = 1;
//----------------------------------------------------------------------------------------------------------------------------------open the serialport
CommFd = open(DeviceName, O_RDWR, 0);
if (CommFd < 0)
Error("Unable to open device");
if (fcntl(CommFd, F_SETFL, O_NONBLOCK) < 0)
Error("Unable set to NONBLOCK mode");
memset(&TtyAttr, 0, sizeof(struct termios));
TtyAttr.c_iflag = IGNPAR;
TtyAttr.c_cflag = DeviceSpeed | HUPCL | ByteBits | CREAD | CLOCAL;
TtyAttr.c_cc[VMIN] = 0;
if (tcsetattr(CommFd, TCSANOW, &TtyAttr) < 0)
Warning("Unable to set comm port");
//----------------------------------------------------------------------------------------------------------------------------------open the tty
TtyFd = open(TtyName, O_RDWR | O_NDELAY, 0);
if (TtyFd < 0)
Error("Unable to open tty");
TtyAttr.c_cflag = TtySpeed | HUPCL | ByteBits | CREAD | CLOCAL;
if (tcgetattr(TtyFd, &BackupTtyAttr) < 0)
Error("Unable to get tty");
if (tcsetattr(TtyFd, TCSANOW, &TtyAttr) < 0)
Error("Unable to set tty");
unsigned char TestCode[] = "TestSerialPort";
if (write(CommFd, &TestCode, 15) < 0)
{
Error(strerror(errno));
}
//-----------------------------------------------------------------------------------------------------------------------------------
for (;;)
{
fd_set ReadSetFD;
FD_ZERO(&ReadSetFD);
FD_SET(CommFd, &ReadSetFD);
FD_SET( TtyFd, &ReadSetFD);
if (select(max(CommFd, TtyFd) + 1, &ReadSetFD, NULL, NULL, NULL) < 0)
{
Error(strerror(errno));
}
if (FD_ISSET(CommFd, &ReadSetFD))
{
while (read(CommFd, &Char, 1) == 1)
{
WaitFdWriteable(TtyFd);
if (write(TtyFd, &Char, 1) < 0)
{
Error(strerror(errno));
}
if (OutputToStdout)
{
OutputStdChar(stdout);
fflush(stdout);
}
}
}
if (FD_ISSET(TtyFd, &ReadSetFD))
{
while (read(TtyFd, &Char, 1) == 1)
{
static int EscKeyCount = 0;
WaitFdWriteable(CommFd);
if (write(CommFd, &Char, 1) < 0)
{
Error(strerror(errno));
}
if (OutputToStdout)
{
OutputStdChar(stderr);
fflush(stderr);
}
if (Char == '\x1b')
{
EscKeyCount ++;
if (EscKeyCount >= 3)
goto ExitLabel;
}
else
EscKeyCount = 0;
}
}
}
ExitLabel:
if (tcsetattr(TtyFd, TCSANOW, &BackupTtyAttr) < 0)
Error("Unable to set tty");
return 0;
}