Detecting connection break on serial port listener on linux
Hi,
Can anyone let me know how to detect the client connection break on a serial port listener on linux?
I have written an application that opens the serial port and is able to read and write data. My serial port listener is running on Linux and I am using HyperTerminal (WIN-NT/2K) for communication on client side.
The problem: Now when I DISCONNECT the client from the CALL menu in HyperTerminal, I am not able to detect the disconnection at the listener. Here is my sample piece of code. Please suggest something, if you have any idea or working on it.
Thanking you in advance.
Sample code follows:
--------------------------------------------------------------------
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main()
{
// open serial port for communication
int serialFd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
if (fd == -1)
{
//printf("Could not open the port\n\n");
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
//get the port options
tcgetattr(serialFd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
// setting for 8bits, No parity, 1 Stop bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// choosing raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// ignore input parity check
options.c_lflag &= ~INPCK;
// check parity errors and strip parity bits
options.c_lflag |= (IGNPAR | ISTRIP);
//diabling software flow control
options.c_lflag &= ~(IXON | IXOFF | IXANY);
// choosing processed output and map NL to CR-NL
options.c_lflag |= (OPOST | ONLCR);
// set the configured options of the port
tcsetattr(serialFd, TCSANOW, &options);
// set port mode to no delay
fcntl(serialFd, F_SETFL, FNDELAY);
write(serialFd, "Welcome to PORT COM1", 20 ) ;
int flag = 0;
char buf[256];
memset(buf, 0, 256);
while (flag == false) /* loop for input */
{
int res = read(serialFd, buf, 255);
buf[res]=0;
write(fd, "Buffer read :%s\n", buf);
if (buf[0]=='z')
flag = true;
}
return 0;
}
|