Detecting connection break on serial port listener on linux
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#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);
// 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;
}
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);
}
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.