如何避免阻塞读取功能?

时间:2014-02-15 16:14:29

标签: c serial-port

我正在尝试从cygwin命令中心的USB端口写入和读取数据。 我已设法在连接设备时写入和读取数据但我想要一种方法来检测其他设备是否不存在或无法发回任何数据。我目前的测试代码如下所示(我尝试过很多不同的东西,但似乎没什么用)。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include "USB_com.h"
#include "unistd.h"
void main()
{
  int fd, value, bytes_read, bytes_written, nbytes, i, j;
  char buffR[20];
  char buffS[20];
  fd = USB_init("/dev/com1");
  printf("enter a message (write exit to terminate the session): ");
  fgets(buffS, 19, stdin);
  while (strncmp("exit", buffS, 4) != 0)
  {
    bytes_written = write(fd, buffS, 19);
    sleep(1);
    bytes_read = read(fd, buffR, 19);
    printf("string recieved : %s\n", buffR);
    memset(buffS, '\0', 19);
    printf("enter a message (write exit to terminate the session): ");
    fgets(buffS, 19, stdin);
  }
  USB_cleanup(fd);
}

我的USB_init.c用于从USB设备进行写入和读取,如下所示。

#include "USB_init.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>


/* baudrate settings are defined in <asm/termbits.h>, which is
 * included by <termios.h> */
#ifndef BAUDRATE
#define BAUDRATE B9600
#endif

#define _POSIX_SOURCE 1     /* POSIX compliant source */

static int fd, c, res;
static struct termios oldtio, newtio;
static char *device;

int USB_init(char *modemdevice)
{
    /* 
     * Open modem device for reading and writing and not as controlling tty
     * because we don't want to get killed if linenoise sends CTRL-C.
     **/
    device = modemdevice;
    //fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY);
    fd = open (device, O_RDWR | O_NOCTTY );
    if (fd < 0)
      {
      perror (device);
      exit(-1);
      }

    tcgetattr (fd, &oldtio);    /* save current serial port settings */
    bzero (&newtio, sizeof (newtio));   /* clear struct for new port settings */

    /* 
     *BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
     *CRTSCTS : output hardware flow control (only used if the cable has
     *all necessary lines. )
     *CS8     : 8n1 (8bit,no parity,1 stopbit)
     *CLOCAL  : local connection, no modem contol
     *CREAD   : enable receiving characters
     **/
    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;

    /*
     *IGNPAR  : ignore bytes with parity errors
     *ICRNL   : map CR to NL (otherwise a CR input on the other computer
     *          will not terminate input)
     *          otherwise make device raw (no other input processing)
     **/
    newtio.c_iflag = IGNPAR | ICRNL;

    /*
     * Map NL to CR NL in output.
     *                  */
#if 0
    newtio.c_oflag = ONLCR;
#else
    newtio.c_oflag = 0;
#endif


    /*
     * ICANON  : enable canonical input
     *           disable all echo functionality, and don't send signals to calling program
     **/
#if 1
    newtio.c_lflag = ICANON;
#else
    newtio.c_lflag = 0;
#endif

    /* 
     * initialize all control characters 
     * default values can be found in /usr/include/termios.h, and are given
     * in the comments, but we don't need them here
     *                                       */
    newtio.c_cc[VINTR] = 0; /* Ctrl-c */
    newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
    newtio.c_cc[VERASE] = 0;    /* del */
    newtio.c_cc[VKILL] = 0; /* @ */
    newtio.c_cc[VEOF] = 4;  /* Ctrl-d */
    newtio.c_cc[VTIME] = 0; /* inter-character timer unused*/
    newtio.c_cc[VMIN] = 1;  /* blocking read until 1 character arrives*/
    newtio.c_cc[VSWTC] = 0; /* '\0' */
    newtio.c_cc[VSTART] = 0;    /* Ctrl-q */
    newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
    newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
    newtio.c_cc[VEOL] = 0;  /* '\0' */
    newtio.c_cc[VREPRINT] = 0;  /* Ctrl-r */
    newtio.c_cc[VDISCARD] = 0;  /* Ctrl-u */
    newtio.c_cc[VWERASE] = 0;   /* Ctrl-w */
    newtio.c_cc[VLNEXT] = 0;    /* Ctrl-v */
    newtio.c_cc[VEOL2] = 0; /* '\0' */

    /* 
     * now clean the modem line and activate the settings for the port
     **/
    tcflush (fd, TCIFLUSH);
    tcsetattr (fd, TCSANOW, &newtio);

    /*
     * terminal settings done, return file descriptor
     **/

    return fd;
}

void USB_cleanup(int ifd){
    if(ifd != fd) {
        fprintf(stderr, "WARNING! file descriptor != the one returned by serial_init()\n");
    }
    /* restore the old port settings */
    tcsetattr (ifd, TCSANOW, &oldtio);
}

有人可以告诉我如何做一些像read(fd,buffR,19)这样的东西,但是如果我没有收到任何数据并打印出像 printf(“不与设备联系”)?

我非常感谢有关如何解决这个问题的任何建议!

2 个答案:

答案 0 :(得分:3)

使用序列号时,您有两种选择:使用select / poll超时,或使用termios配置端口。

对于selectpoll,方法首先等待描述符上的事件,然后在成功结果后读取。这种方法的专业是它也适用于网络套接字和一些其他描述符类型:

int fd = ...
fd_set fds;
stuct timeval timeout;

timeout.tv_sec = 10; /* timeout in secs */
timeout.tv_usec = 0;
FD_ZERO(&fds)
FD_SET(fd, &fds)
if (select(fd, fds, NULL, NULL, &timeout) > 0)
  read(...)
else ... timeout

poll示例与上述非常类似。

但是,当使用任何类型的串行设备时,还有另一种方法可以使读取正确地超时。您需要使用VMINVTIME属性:

newtio.c_cc[VTIME] = timeout; /* timeout in 1/10 of second  1==100ms, 10==1sec*/
newtio.c_cc[VMIN] = 0;

就是这样。

答案 1 :(得分:2)

您可能希望查看select()来监视文件描述符是否准备好从中读取。它可以通过超时调用。

或者使用non-blocking when open()ing创建文件描述符fcntl(),如果没有可用的数据,它将从read() with EAGAIN or EWOULDBLOCK返回,这将允许您实现on逻辑来决定何时重试或取消阅读。