Linux c写入后刷新串行缓冲区
linux c flush serial buffer after writing
问题很简单。当我写入/dev/ttyS1
时,它不会立即刷新它。这可能与我的串口初始化有关…但是我想不出来!我的代码是这样的:
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
volatile int STOP = FALSE;
void signal_handler_IO(int status); /* definition of signal handler */
int wait_flag = TRUE; /* TRUE while no signal received */
int main()
{
int fd, c, res;
struct termios oldtio, newtio;
struct sigaction saio; /* definition of signal action */
char buf[255];
/* open the device to be non-blocking (read will return immediatly) */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) {
perror(MODEMDEVICE);
exit(-1);
}
/* install the signal handler before making the device asynchronous */
saio.sa_handler = signal_handler_IO;
//saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO, &saio, NULL);
/* allow the process to receive SIGIO */
fcntl(fd, F_SETOWN, getpid());
/* Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
fcntl(fd, F_SETFL, FASYNC);
tcgetattr(fd, &oldtio); /* save current port settings */
/* set new port settings for canonical input processing */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
int i = 0;
/* loop while waiting for input. normally we would do something
useful here */
while (1) {
printf(".n");
usleep(100000);
//tcflush(fd, TCOFLUSH);
//tcflush(fd, TCIOFLUSH);
i++;
if (i == 20) {
// ------------------ write to serial ----------------------
int n = write(fd, "ATZn", 4);
if (n < 0)
fputs("write() of 4 bytes failed!n", stderr);
}
/* after receiving SIGIO, wait_flag = FALSE, input is available
and can be read */
if (wait_flag == FALSE ) {
res = read(fd, buf, 255);
buf[res] = 0;
printf(":%s:%dn", buf, res);
wait_flag = TRUE; /* wait for new input */
}
}
/* restore old port settings */
tcsetattr(fd, TCSANOW, &oldtio);
}
void signal_handler_IO(int status) {
printf("received SIGIO signal.n");
wait_flag = FALSE;
}
任何想法?
已通过设置"CRTSCTS
"使能CTS硬件流控。这意味着如果输入到UART的CTS不是活动的,则UART应该避免发送。
您可能需要大致浏览一下术语手册页。
我不认为这是一个解决方案,但添加:
tcflush(fd, TCIOFLUSH); // clear buffer
用清除缓冲区似乎能达到目的。然而,应该有一些更优雅、更合适的解决方案。
Since "可以表示清除缓冲区或等待直到所有字节被发送,这里引用https://linux.die.net/man/3/tcdrain
tcdrain()等待所有写入fd所引用对象的输出都传输完毕。
tcflush()丢弃写入fd引用的对象但未发送的数据,或接收到但未读取的数据,具体取决于queue_selector
的值。
tcdrain(fd); // Wait until transmission ends
tcflush(fd, TCOFLUSH); // Clear write buffer
相关文章:
- C++字符*缓冲区的大小
- 如何让 cout 缓冲区在 ubuntu 上刷新
- 缓冲区刷新究竟是如何工作的(std::endl 和 之间的区别)?
- 换行符是否也会刷新缓冲区?
- 输出缓冲区无法刷新的情况?
- 如何刷新或清除 GetAsyncKeyState 的缓冲区
- 何时刷新输出缓冲区
- C++ 刷新缓冲区
- 如何在获得下一个字符后正确刷新输入缓冲区,而无需按两次回车键!C++
- 如何在使用FFmpeg写入mp4文件时将缓冲区数据刷新到磁盘
- 缓冲区刷新:"n" vs. 标准::endl
- 使编译器不自动刷新缓冲区
- 可以在程序退出时和请求输入时自动刷新自定义流缓冲区
- 我可以通过控制std::ofstream中的缓冲区刷新来获得性能吗
- boost::asio程序中的刷新缓冲区
- 将fseek函数在c++中刷新缓冲区中的数据
- 如何在不刷新缓冲区的情况下打印换行符
- 为什么我们刷新流而不是缓冲区
- 缓冲区何时刷新
- Linux c写入后刷新串行缓冲区