# Configuring a device for incoming connection using termios



## j4ck (Sep 4, 2013)

I'm trying to configure some ttyus for incoming connections. I have done this by changing /etc/ttys like:


```
cuau6   "/usr/libexec/getty std.115200" cons25  on secure
```

But I thought it would be better doing this by using termios, so I have written the following code to handle this:

```
#include <stdio.h>
#include <string.h>
#include <unistd.h>  //unix standard function definition
#include <fcntl.h>   //file control definition
#include <termios.h>   //POSIX terminal control definition
#include <errno.h>   //error number definition
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <signal.h>
#include <curses.h>
//
#define BAUDRATE 115200
#define MODEMDEVICE "/dev/ttyu6"

int main()
{
    int mainfd;
    struct termios oldtio,options;
    mainfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
    if (mainfd <0)
    {
        /* Could not open the port */
        fprintf(stderr, "open_port: Unable to open /dev/ttyu6 - %s\r\n",
        strerror(errno));
    }

    tcgetattr(mainfd,&oldtio); /* save current serial port settings */
    //
    int val = fcntl(mainfd, F_GETFL, 0);
    val |= O_NONBLOCK;
    fcntl(mainfd, F_SETFL, val);/* Configure port reading */
    tcgetattr(mainfd, &options);
//////////////////////////////////////////////////////////////set speed
    cfsetispeed(&options, BAUDRATE);
    cfsetospeed(&options, BAUDRATE);
///////////////////////////////////////////////////////////set speed-END
    /* Enable the receiver and set local mode */
    options.c_cflag |= (CLOCAL | CREAD);
///////////////////////////////////////////////////////set parity
    options.c_cflag &= ~PARENB;
//////////////////////////////////////////////////////////set parity-END

//////////////////////////////////////////////////////////////set stopbit
    options.c_cflag |= CSTOPB;
////////////////////////////////////////////////////set stopbit-END
    options.c_cflag &= ~CSIZE;
///////////////////////////////////////////////////////set databit
    options.c_cflag |=  CS8;
////////////////////////////////////////////set databit-END

///////////////////////////////////////////////////set flow-control
    options.c_cflag &= ~CRTSCTS;
///////////////////////////////////////////////////set flow-control-END

    /* Set the new options for the port */
    //
    options.c_iflag &= ~(ISTRIP|ICRNL);
    options.c_iflag &= ~IGNCR; // ignore CR
    options.c_iflag &= ~INLCR; // ignore CR
    options.c_oflag &= ~OPOST;
    options.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 0;

    /*
    now clean the modem line and activate the settings for the port
    */
    tcsetattr(mainfd,TCSANOW,&options);
    return 1;
}
```

I use cu on my system to connect to server, but I cannot see anything on the screen. It seems that the connection establishes but no data exchanges happens. Any ideas?


----------

