LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2003, 03:48 AM   #1
neelc20
LQ Newbie
 
Registered: Nov 2003
Distribution: RedHat 8.0
Posts: 4

Rep: Reputation: 0
Question 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;
}

Last edited by neelc20; 11-28-2003 at 03:58 AM.
 
Old 11-29-2003, 11:23 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Moved to Programming your more likely to get a resonse there.
 
Old 11-29-2003, 12:09 PM   #3
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
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);
}
 
Old 12-02-2003, 11:39 PM   #4
neelc20
LQ Newbie
 
Registered: Nov 2003
Distribution: RedHat 8.0
Posts: 4

Original Poster
Rep: Reputation: 0
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?
 
Old 12-03-2003, 11:30 AM   #5
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
Quote:
Would you like to let me know your email id?
What is email id?
 
Old 12-03-2003, 09:08 PM   #6
neelc20
LQ Newbie
 
Registered: Nov 2003
Distribution: RedHat 8.0
Posts: 4

Original Poster
Rep: Reputation: 0
Dorian, I meant to say your email address.
Thanks
 
Old 12-04-2003, 01:08 AM   #7
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
dorian33@o2.pl
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 09:32 PM
serial port network connection eantoranz Linux - Networking 2 10-31-2005 08:05 AM
How to probe if a serial port has a valid connection? RedDrake Linux - General 2 09-01-2005 07:05 PM
setting up com port for serial connection to monitor network switch help murf562 Linux - Hardware 1 10-30-2004 09:46 AM
Detecting connection break on serial port listener on linux neelc20 Linux - Newbie 3 11-29-2003 03:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration