Click to See Complete Forum and Search --> : tcsetattr() in termios not working correctly


ev8r
08-01-2003, 09:36 AM
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

tecknophreak
08-01-2003, 10:35 AM
You might not use these same flags, but here's how to setup serial termios from serial programming how-to


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);

tecknophreak
08-01-2003, 10:38 AM
Originally posted by ev8r

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

thnxs

add B9600 to c_cflag, should fix it.

ev8r
08-01-2003, 10:49 AM
works great!!!!

THANKS !!!!!!!!!!!!