Serial port Read Write
I need to implement a Serail port I/O.
what's the difference between with O_NONBLOCK and without it? Note that I have set my c_cc[VTIME] = 0 and c_cc[VMIN] = 0, which means read will return right away no matter if it's a non-blocking mode. The only difference I can see is write(). But it seems the STREAM have enough system buffer to allow write() return right away.
So what's the difference between with and without O_NONBLOCK in question line 1? Thanks
bool mstpInitSerialPort()
{
struct termios SerialIOSetting;
//if ( (fd_SerialPort=open (portname, O_RDWR|O_NOCTTY)) < 0)
if((fd_SerialPort=open(MSTPPORT, O_RDWR|O_NOCTTY/*| O_BLOCK*/))<0) //*****question 1, with and without*****
{
perror("Open Serial Port failed\n");
return false;
}
bzero(&SerialIOSetting, sizeof(SerialIOSetting));
SerialIOSetting.c_cflag = B9600| CRTSCTS| CS8 | CLOCAL | CREAD;
SerialIOSetting.c_iflag = 0; // if == IGNPAR; // ignore parity error
SerialIOSetting.c_oflag = 0; //raw output
SerialIOSetting.c_lflag = 0; // non-canonical, no echo
SerialIOSetting.c_cc[VEOF] = 0xFF;
SerialIOSetting.c_cc[VTIME] = 0; //****Note for question 1
SerialIOSetting.c_cc[VMIN] = 0; //****Note for question 1
tcflush(fd_SerialPort, TCIFLUSH);
if(tcsetattr(fd_SerialPort, TCSANOW, &SerialIOSetting) == -1)
{
perror(strerror(errno));
return false;
}
return true;
}
int SerialRx()
{
int Byte;
if ( read(fd_SerialPort, &Byte, 1) == 1)
return Byte;
else
return -1;
}
bool SerialTxBuf(byte far *txb, word txlen)
{
if( write(fd_SerialPort, txb, txlen) == txlen)
return true;
else
return false;
}
|