serial programming using terios

Ted Reynard ted.reynard at
Sun Mar 17 08:37:50 UTC 2013

Hi fellas
I'm trying to connect to a CISCO router by a program which I developed using termios.
First I configure the local TTY to right settings :
    struct termios old = {0};
    /*get current TTY settings*/
    if (tcgetattr(STDIN_FILENO, &old) < 0)
        old.c_iflag &= ~(ISTRIP|ICRNL);
        old.c_oflag &= ~OPOST;
        old.c_lflag &= ~(ICANON|IEXTEN|ECHO|ECHONL);
        old.c_cc[VMIN] = 1;
        old.c_cc[VTIME] = 0;
        if (tcsetattr(0, TCSANOW/*TCSAFLUSH*/, &old) < 0)
                perror("tcsetattr ICANON");

And then the TTY which users want to use 
    struct termios dev;
static int port=atoi(argv[1]);

 int fd = open_port(port);

fcntl(fd, F_SETFL, FNDELAY); 
tcgetattr(mainfd, &dev);
cfsetspeed(&dev, speed);
dev.c_cflag |= (CLOCAL | CREAD);
dev.c_cflag &= ~PARENB;
dev.c_cflag |= CSTOPB;

dev.c_cflag &= ~CSIZE;
dev.c_cflag |=  CS8;
dev.c_cflag &= ~CRTSCTS;
dev.c_iflag &= ~(ISTRIP|ICRNL);
dev.c_oflag &= ~OPOST;
dev.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
dev.c_cc[VMIN] = 1;
dev.c_cc[VTIME] = 0;

And I have two threads to handle read and write simultaneously. The problem is I have to add at least 2 seconds delay 
after writing user input to the port, otherwise the output would not be what I expect. Without this sleep
command, every character I type, the output is shown with next character; not at time :

Router>abc // While I typed "abcd". If I continue with typing "e", then the output will be "abcd" and so on ...

Am I missing something?

More information about the freebsd-questions mailing list