'Including <termios.h> and <asm/termios.h> in the same project
What I want to achieve: I want to set custom baud rate
values for some tty*
-like UART
-mapped terminals.
How: The only way I found by far is to use the struct termios2
structure which is located in<asm/termios>
header (as mentioned here, first answer).
My solution works very well by far, but now I need to use some functions:
speed_t cfgetispeed(const struct termios *);
int tcdrain(int);
int tcflow(int, int);
int tcflush(int, int);
int tcgetattr(int, struct termios *);
pid_t tcgetsid(int);
int tcsendbreak(int, int);
int tcsetattr(int, int, struct termios *);
The problem is that in <asm/termios.h>
there are no such functions, and I need to include <termios.h>
for being able to use them.
Problem: If I include both headers (<asm/termios.h>
and <termios.h>
) the compiler will scream about functions and structure re-declaration, and he's right.
How can I solve this without using some obscure practice (like wrapping one of headers in a namespace, like mentioned here)?
Solution 1:[1]
How can I solve this without using some obscure practice (like wrapping one of headers in a namespace, like mentioned here)?
If you find namespaces obscure, I don't know how you'd call this:
#define termios asmtermios
#include <asm/termios.h>
#undef termios
#include <termios.h>
Anyway, this too gets you rid of the error: redefinition of 'struct termios'
.
Solution 2:[2]
I had a similar issue - wanted custom baud rate support with definitions like termios2
, TCGETS2
and BOTHER
, while still making use of the traditional termios calls. I instinctively wrapped the termios2 stuff in its own compilation unit and had no problems. So my structure looks like this:
serial_driver.c/.h
#include <termios.h>
#include <fcntl.h>
#include <dirent.h>
int open_port(int fd, int baudrate, eParitySetting parity, int numStopBits)
{
if(baudrate_is_non_standard(baudrate)
setNonStandardBaudRateTermios(fd, baudrate, parity, numStopBits);
else
//all the normal tcgetattr/cfsetospeed/tcsetattr
}
int do_other_things(void)
{
//all the normal tcsendbreak/tcflush/etc things
}
serial_driver_abr.c/.h
#include <asm/termios.h> /* asm gives us the all important BOTHER and TCGETS2 */
#include <fcntl.h>
#include <dirent.h>
#include <stropts.h> /* Oddly, for ioctl, because ioctl.h causes include dramas */
setNonStandardBaudRateTermios(int fd, int baudrate, eParitySetting parity, int numStopBits)
{
//All the termios2/ioctl/TCGETS2/BOTHER things
}
Works well for me.
Solution 3:[3]
I hit the same problem with an old arm cross compiler, but found the latest one, gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf, solved the problem with a different header file. Here is my code:
[uart_config.c]
#include <asm/termbits.h>
#include <sys/ioctl.h>
/* functions */
int uart_config_baudrate(int fd)
{
struct termios2 tio;
ioctl(fd, TCGETS2, &tio);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = MY_SPECIAL_BAUDRATE_NUMBER;
tio.c_ospeed = MY_SPECIAL_BAUDRATE_NUMBER;
return ioctl(fd, TCSETS2, &tio);
}
[main.c]
static int init_uart(void)
{
struct termios tp;
int rc;
memset(&tp, 0, sizeof(tp));
tp.c_cflag = MY_SPECIAL_BAUDRATE | CS8 | CLOCAL | CREAD | PARENB | PARODD;
tp.c_iflag = IGNPAR;
tp.c_oflag = 0;
tp.c_lflag = 0; /* set input mode to non-canonical */
tp.c_cc[VTIME] = 0; /* inter-character timer unused */
tp.c_cc[VMIN] = 1; /* blocking read until 5 chars received */
tcflush(fd, TCIFLUSH);
rc = tcsetattr(fd, TCSANOW, &tp);
return rc;
}
int main()
{
int fd;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
init_uart(fd); // add your error handling
uart_config_baudrate(fd); // add your error handling
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Armali |
Solution 2 | Heath Raftery |
Solution 3 | D.Liu |