int main()
{
//CONFIGURE COMM PORT
int CommPort;
struct termios oldtio, newtio;
char TheBuffer[4800];
char *TheCharacter;
int nbytes;
CommPort = open(MODEMDEVICE, O_RDONLY | O_NOCTTY | O_NDELAY);
if (CommPort <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(CommPort,&oldtio); /* save current serial port settings */
//set baud rate
cfsetispeed(&newtio, B1200);
cfsetospeed(&newtio, B1200);
//8N2
newtio.c_cflag &= ~PARENB;
newtio.c_cflag |= CSTOPB;
newtio.c_cflag &= ~CSIZE; /* Mask the character size bits */
newtio.c_cflag |= CS8; /* Select 8 data bits */
//enable flow control when using 8N2
newtio.c_cflag |= CRTSCTS;
//
newtio.c_iflag |= (IXON | IXOFF | IXANY);
//processed output
newtio.c_oflag |= OPOST;
newtio.c_oflag |= ONLCR;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSTART] = 17; /* Ctrl-q XON*/
newtio.c_cc[VSTOP] = 19; /* Ctrl-s XOFF*/
tcsetattr(CommPort,TCSANOW,&newtio);
/*The FNDELAY option causes the read function to return 0 if no characters are available on the port.*/
fcntl(CommPort, F_SETFL, FNDELAY);
ioctl (CommPort, FIONREAD, &nbytes); //get size of buffer waiting to be read
cout << "Buffer Size: " << nbytes << endl;
TheCharacter = TheBuffer;
while ((nbytes = read(CommPort, TheCharacter, TheBuffer + sizeof(TheBuffer) - TheCharacter - 1)) > 0)
{
TheCharacter += nbytes;
}
cout << "\nTheCharacter: " << TheCharacter << endl;
close(CommPort);
return (CommPort);
}
My output is:
Buffer Size: 0
TheCharacter:
In case it helps or matters, the RS232 device is the SMDR port of an AVAYA Partner ACS. My USB to RS232 adapter appears to have installed correctly in Kubuntu - no error for /dev/ttyUSB0. My cabling and adapter is the same as that used by my VB app in windows.