串口输入缓冲区的字节数

时间:2014-04-28 05:10:21

标签: 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

我是否遗漏或不了解某事?

BTW我只是在这里做一个简单的环回测试。

1 个答案:

答案 0 :(得分:0)

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

ioctl(fd, FIONREAD, &bytes);

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

相关问题