c - 串行端口输入缓冲区的字节数

标签 c linux serial-port

这里是 C 编程的新手,请多多包涵。我编写了一个程序来向串口写入/读取数据。一切似乎都在工作,除非我尝试读取已接收端口上可用的字节数。这是我的代码(参见 read_port 函数):

#include <stdio.h>       /* Standard input/output definitions */
#include <string.h>      /* String function definitions */
#include <unistd.h>      /* UNIX standard function definitions */
#include <fcntl.h>       /* File control definitions */
#include <errno.h>       /* Error number definitions */
#include <termios.h>     /* POSIX terminal control definitions */
#include <sys/ioctl.h>   /* Serial Port IO Controls */

int fd; /* File descriptor for the port */
struct termios options_original;  /* Original Serial Port Options */

int main()
{
  fd = open_port();
  flush_port();
  write_port();
  printf("FIONBIO value %d\n", FIONBIO);
  usleep(2);
  printf("FIONREAD value %d\n", FIONREAD);
  read_port();
  close_port();
}

/*
 * open_port() - Open serial port 1.
 *
 * Returns the file descriptor on success or -1 on error
 */
int open_port(void)
{
  struct termios options;

  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
  if (fd != -1)
  {
    printf("Serial Port Open\n");
    fcntl(fd, F_SETFL, 0);
    tcgetattr(fd, &options_original);
    tcgetattr(fd, &options);
    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);
    options.c_cflag |= (CLOCAL | CREAD); /* Enable the receiver and set local mode */
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  /* Raw Input Mode */
    tcsetattr(fd, TCSANOW, &options);    /* Set the new options for the port */
  }
  else
  {
    /* Could not open the port */
    perror("open_port: Unable to open /dev/ttyUSB0 - ");
  }

  return (fd);
}

int close_port(void)
{
  tcsetattr(fd, TCSANOW, &options_original);
  printf("Serial Port Closed\n");
  close(fd);
}

int flush_port(void)
{
  usleep(2); // required to make flush work, for some reason
  printf("Flushing IO Buffers\n");
  tcflush(fd, TCIOFLUSH);
}

int write_port(void)
{
  int n = write(fd, "DSC", 3);
  if (n < 0)
    fputs("write() of 1 byte failed!\n", stderr);
  else
    printf("Wrote %0d bytes to serial port\n", n);
}

int read_port(void)
{
  int  chars_read = 3;
  int  bytes;
  char read_buffer[3] = {0};
  int  i;

  fcntl(fd, F_SETFL, 0);
  ioctl(fd, FIONBIO, &bytes);
  printf("Number of bytes = %d\n", bytes);
  int n = read(fd, read_buffer, chars_read);
  printf("Character at Port: %s\n", read_buffer);
  printf("Number of chars read = %0d\n", n);
}

这是输出:

Serial Port Open
Flushing IO Buffers
Wrote 3 bytes to serial port
FIONBIO value 21537
FIONREAD value 21531
Number of bytes = 0
Character at Port: DSC
Number of chars read = 3
Serial Port Closed

出于某种原因,“字节数”总是等于 0。我不知道为什么。这样做有什么问题吗?

  int  bytes;
  ioctl(fd, FIONBIO, &bytes);
  printf("Number of bytes = %d\n", bytes);

这几乎是来自这个网站的逐字记录: http://www.cmrr.umn.edu/~strupp/serial.html#config

我是否遗漏或不理解某些东西?

顺便说一句,我只是在这里做一个简单的环回测试。

最佳答案

ioctl(fd, FIONBIO, ...) 将文件描述符(例如套接字)设置为阻塞或 非阻塞模式。你的意思可能是

ioctl(fd, FIONREAD, &bytes);

获取输入缓冲区中可用的字节数(即字节数 可以无阻塞地读取。)

关于c - 串行端口输入缓冲区的字节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23332908/

相关文章:

c - 如何使用 strtok 进行标记,无论该行中是否有字符串 - C

c - 发送带有修改报头的 TCP 段

c - 解释 gdb 回溯

linux - 在 Linux 上使用信号量来保证单个应用程序实例有什么问题?

c++ - 在 Windows 中使用 VMIN 和 VTIME

c# - 在C#中检测Arduino端口

c++ - 正确发送消息 UART

c - 如何在RTOS中为stm32f746NG Discovery配置TIM_BASE中的TIMER?

C - 为什么这个函数不反转数组?

c - MPLAB 目标文件失败