TCGETS, TCSETS, TCSETSW, TCSETSF, TCGETS2, TCSETS2, TCSETSW2, TCSETSF2, TCGETA, TCSETA, TCSETAW, TCSETAF
Contents
Caveats
structtermios from <asm/termbits.h> is different and incompatible with structtermios from <termios.h>.
These ioctl calls require structtermios from <asm/termbits.h>.
Description
TCGETS Equivalent to tcgetattr(fd,argp).
Get the current serial port settings.
TCSETS Equivalent to tcsetattr(fd,TCSANOW,argp).
Set the current serial port settings.
TCSETSW
Equivalent to tcsetattr(fd,TCSADRAIN,argp).
Allow the output buffer to drain, and set the current serial port settings.
TCSETSF
Equivalent to tcsetattr(fd,TCSAFLUSH,argp).
Allow the output buffer to drain, discard pending input, and set the current serial port settings.
The following four ioctls are just like TCGETS, TCSETS, TCSETSW, TCSETSF, except that they take a structtermios2* instead of a structtermios*. If the structure member c_cflag contains the flag BOTHER, then
the baud rate is stored in the structure members c_ispeed and c_ospeed as integer values. These ioctls
are not supported on all architectures.
TCGETS2TCSETS2TCSETSW2TCSETSF2
The following four ioctls are just like TCGETS, TCSETS, TCSETSW, TCSETSF, except that they take a structtermio* instead of a structtermios*.
TCGETATCSETATCSETAWTCSETAF
Errors
EPERM Insufficient permission.
Examples
Get or set arbitrary baudrate on the serial port.
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <asm/termbits.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
#if !defined BOTHER
fprintf(stderr, "BOTHER is unsupported\n");
/* Program may fallback to TCGETS/TCSETS with Bnnn constants */
exit(EXIT_FAILURE);
#else
/* Declare tio structure, its type depends on supported ioctl */
# if defined TCGETS2
struct termios2 tio;
# else
struct termios tio;
# endif
int fd, rc;
if (argc != 2 && argc != 3 && argc != 4) {
fprintf(stderr, "Usage: %s device [output [input] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
/* Get the current serial port settings via supported ioctl */
# if defined TCGETS2
rc = ioctl(fd, TCGETS2, &tio);
# else
rc = ioctl(fd, TCGETS, &tio);
# endif
if (rc) {
perror("TCGETS");
close(fd);
exit(EXIT_FAILURE);
}
/* Change baud rate when more arguments were provided */
if (argc == 3 || argc == 4) {
/* Clear the current output baud rate and fill a new value */
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ospeed = atoi(argv[2]);
/* Clear the current input baud rate and fill a new value */
tio.c_cflag &= ~(CBAUD << IBSHIFT);
tio.c_cflag |= BOTHER << IBSHIFT;
/* When 4th argument is not provided reuse output baud rate */
tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
/* Set new serial port settings via supported ioctl */
# if defined TCSETS2
rc = ioctl(fd, TCSETS2, &tio);
# else
rc = ioctl(fd, TCSETS, &tio);
# endif
if (rc) {
perror("TCSETS");
close(fd);
exit(EXIT_FAILURE);
}
/* And get new values which were really configured */
# if defined TCGETS2
rc = ioctl(fd, TCGETS2, &tio);
# else
rc = ioctl(fd, TCGETS, &tio);
# endif
if (rc) {
perror("TCGETS");
close(fd);
exit(EXIT_FAILURE);
}
}
close(fd);
printf("output baud rate: %u\n", tio.c_ospeed);
printf("input baud rate: %u\n", tio.c_ispeed);
exit(EXIT_SUCCESS);
#endif
}
History
TCGETS2TCSETS2TCSETSW2TCSETSF2
Linux 2.6.20.
Library
Standard C library (libc, -lc)
Name
TCGETS, TCSETS, TCSETSW, TCSETSF, TCGETS2, TCSETS2, TCSETSW2, TCSETSF2, TCGETA, TCSETA, TCSETAW, TCSETAF
- get and set terminal attributes
Return Value
On success, 0 is returned. On error, -1 is returned and errno is set to indicate the error.
See Also
ioctl(2), ioctl_tty(2), termios(3)
Linux man-pages 6.9.1 2024-06-15 TCSETS(2const)
Synopsis
#include<asm/termbits.h> /* Definition of TC* constants */
#include<sys/ioctl.h>intioctl(intfd,TCGETS,structtermios*argp);intioctl(intfd,TCSETS,conststructtermios*argp);intioctl(intfd,TCSETSW,conststructtermios*argp);intioctl(intfd,TCSETSF,conststructtermios*argp);intioctl(intfd,TCGETS2,structtermios2*argp);intioctl(intfd,TCSETS2,conststructtermios2*argp);intioctl(intfd,TCSETSW2,conststructtermios2*argp);intioctl(intfd,TCSETSF2,conststructtermios2*argp);intioctl(intfd,TCGETA,structtermio*argp);intioctl(intfd,TCSETA,conststructtermio*argp);intioctl(intfd,TCSETAW,conststructtermio*argp);intioctl(intfd,TCSETAF,conststructtermio*argp);#include<asm/termbits.h>structtermios;structtermios2;structtermio;
