tcsetattr() in termios not working correctly


Results 1 to 5 of 5

Thread: tcsetattr() in termios not working correctly

  1. #1
    Join Date
    Apr 2003
    Posts
    122

    Thumbs down tcsetattr() in termios not working correctly

    basically, here is what im using to initialize a serial port to 9600 baud, and 8 N 1.

    int fd;
    struct termios options;
    fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd==-1)
    return (-1);
    fcntl(fd, F_SETFL, 0);
    tcgetattr(fd,&options);
    options.c_ispeed=9600;
    options.c_ospeed=9600;
    printf("speed = %i bps \n",(int)options.c_ispeed);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_cflag &= ~CSTOPB;
    options.c_cflag |= CS8;
    options.c_cc[VMIN] = 0;
    options.c_cc[VTIME] =10;
    tcsetattr(fd,TCSANOW,&options);

    while this program runs i do a stty -F /dev/ttyS0 and here is what i get:

    speed 0 baud; line = 0;
    min = 0; time = 10;
    ignbrk -brkint -icrnl -imaxbel
    -opost -onlcr
    -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

    (and of course no data flow on the port)

    notice the 'speed 0 baud', i know this serial port functions correctly as i've used minicom on it many times, perhaps someone can shed some light on this little dilemma for me.

    thnxs
    "Did you ever notice how fast Windows runs? Neither did I."

  2. #2
    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228
    You might not use these same flags, but here's how to setup serial termios from serial programming how-to

    Code:
            termios oldtio, newtio;
            tcgetattr(sfd, &oldtio);
    	bzero(&newtio, sizeof(newtio));
    
    	newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
    CREAD;
    	newtio.c_iflag = IGNPAR | ICRNL;
    	newtio.c_oflag = 0;
    	newtio.c_lflag = 0;//ICANON; //0;
    
            newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */
            newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
            newtio.c_cc[VERASE]   = 0;     /* del */
            newtio.c_cc[VKILL]    = 0;     /* @ */
            newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
            newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
            newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
            newtio.c_cc[VSWTC]    = 0;     /* '\0' */
            newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */
            newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
            newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
            newtio.c_cc[VEOL]     = 0;     /* '\0' */
            newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
            newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
            newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
            newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
            newtio.c_cc[VEOL2]    = 0;     /* '\0' */
    
    	tcflush(sfd, TCIFLUSH);
    	tcsetattr(sfd, TCSANOW, &newtio);
    if (i_forgot && this_is_about_code)
    language = c++;

  3. #3
    Join Date
    May 2001
    Location
    Uh, I'm somewhere where I don't know where I am.
    Posts
    1,228

    Re: tcsetattr() in termios not working correctly

    Originally posted by ev8r

    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~CSTOPB;

    thnxs
    add B9600 to c_cflag, should fix it.
    if (i_forgot && this_is_about_code)
    language = c++;

  4. #4
    Join Date
    Apr 2003
    Posts
    122
    works great!!!!

    THANKS !!!!!!!!!!!!
    "Did you ever notice how fast Windows runs? Neither did I."

  5. #5
    Join Date
    Feb 2025
    Posts
    1
    Quote Originally Posted by tecknophreak View Post
    You might not use these same flags, but here's how to setup serial termios from serial programming how-to

    Code:
            termios oldtio, newtio;
            tcgetattr(sfd, &oldtio);
    	bzero(&newtio, sizeof(newtio));
    
    	newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
    CREAD;
    	newtio.c_iflag = IGNPAR | ICRNL;
    	newtio.c_oflag = 0;
    	newtio.c_lflag = 0;//ICANON; //0;
    
            newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */
            newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
            newtio.c_cc[VERASE]   = 0;     /* del */
            newtio.c_cc[VKILL]    = 0;     /* @ */
            newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
            newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
            newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
            newtio.c_cc[VSWTC]    = 0;     /* '\0' */
            newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */
            newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
            newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
            newtio.c_cc[VEOL]     = 0;     /* '\0' */
            newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
            newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
            newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
            newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
            newtio.c_cc[VEOL2]    = 0;     /* '\0' */
    
    	tcflush(sfd, TCIFLUSH);
    	tcsetattr(sfd, TCSANOW, &newtio);
    Yes... you are right... even 20 years later, adding the baud rate to the c_cflag works for me:

    options.c_cflag = B921600 | CS8 | CLOCAL | CREAD; // 8N1 mode

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •