LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Detecting connection break on serial port listener on linux (http://www.linuxquestions.org/questions/programming-9/detecting-connection-break-on-serial-port-listener-on-linux-120675/)

neelc20 11-28-2003 03:48 AM

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;
}

david_ross 11-29-2003 11:23 AM

Moved to Programming your more likely to get a resonse there.

dorian33 11-29-2003 12:09 PM

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:

/*--------------------
 get DCD status
--------------------*/
int get_DCD(int fd) {
 int stat;
 int res;
    res= ioctl(fd, TIOCMGET, &stat);
    if(stat & TIOCM_CD) return(1);
    else return(0);
}


neelc20 12-02-2003 11:39 PM

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?

dorian33 12-03-2003 11:30 AM

Quote:

Would you like to let me know your email id?
What is email id?

neelc20 12-03-2003 09:08 PM

Dorian, I meant to say your email address.
Thanks

dorian33 12-04-2003 01:08 AM

dorian33@o2.pl


All times are GMT -5. The time now is 08:49 PM.