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; } |
Moved to Programming your more likely to get a resonse there.
|
Some doubts:
1. you are setting IXON , IXOFF & IXANY bits in 'c_lflag' athought you should use 'c_iflag' instead 2. same concerns OPOST & ONLCR - both ones should correspond to 'c_oflag'. Regarding your question. In the simplest case of RS232 connection (3-wire cable: Tx, Rx, Gnd) there is no way to detect is if the remote is "alive" or not. I am not sure (I don't want to analyze it too deeply) but it looks like your program is able to use only 3-wire cable for transmission. So you have no chance to detect that the client is or is not connected. For that purposes you need to use additional line. Usually it is DCD. You can check the state with the following code: Code:
/*-------------------- |
Hi Dorian,
There were some typo errors that raised your doubts. The code is taking care of your suggestions/doubts. I verified the serial cable and as per your suggestion, I incorporated the API to find the connection state of the other end of the serial port. I have been able to detect the states properly. Thank a lot for your timely help. Would you like to let me know your email id? |
Quote:
|
Dorian, I meant to say your email address.
Thanks |
|
All times are GMT -5. The time now is 05:58 PM. |