我想要实现的是:我想为一些tty*-like UART-mapped终端设置自定义的-like值。
如何:到目前为止,我发现的唯一方法是使用位于<asm/termios>头中的<asm/termios>结构(正如前面提到的这里,第一个答案)。
到目前为止,我的解决方案工作得很好,但是现在我需要使用一些功能:
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 *);问题是在<asm/termios.h>中没有这样的函数,为了能够使用它们,我需要包括<termios.h>。
问题:如果我同时包含头(<asm/termios.h>和<termios.h>),编译器会尖叫有关函数和结构重新声明,他是对的。
如果不使用一些模糊的实践(比如在名称空间中包装一个标头,比如提到的这里),我如何解决这个问题?
发布于 2018-01-30 12:25:08
如果不使用一些模糊的实践(比如在名称空间中包装一个标头,比如提到的这里),我如何解决这个问题?
如果您发现名称空间晦涩难懂,我不知道您如何称呼它:
#define termios asmtermios
#include <asm/termios.h>
#undef termios
#include <termios.h>不管怎么说,这也让你摆脱了error: redefinition of 'struct termios'。
发布于 2019-05-07 21:29:04
我也有类似的问题--我想要自定义波特率支持,定义为termios2、TCGETS2和BOTHER,同时仍然使用传统的termios调用。我本能地将termios2包装在自己的编译单元中,没有问题。我的结构是这样的:
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
}对我来说很管用。
发布于 2022-04-28 18:47:16
我用一个旧的arm交叉编译器也遇到了同样的问题,但是找到了最新的一个,gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf,用不同的头文件解决了这个问题。这是我的代码:
[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
...
}https://stackoverflow.com/questions/37710525
复制相似问题