String USB到串行适配器-接收在有限时间内正常工作

String USB到串行适配器-接收在有限时间内正常工作,string,io,serial-port,usb,byte,String,Io,Serial Port,Usb,Byte,我从LinuxSerialHowto页面中借用了下面的大部分代码来创建我的程序,以便无休止地从串行端口读取字符。我正在使用一个USB到串行端口适配器(由micro innovations制造),这样我就可以得到一个串行端口。Linux将设备识别为/dev/ttyUSB0 当我在其他本机linux程序(如输出转储程序)中使用该设备时,适配器工作正常,数据不断输入。然而,在我的程序中,数据进入大约10秒钟,然后我没有收到更多的数据,但我知道适配器连接到的硬件有更多的数据要处理 这段代码中有没有什么东

我从LinuxSerialHowto页面中借用了下面的大部分代码来创建我的程序,以便无休止地从串行端口读取字符。我正在使用一个USB到串行端口适配器(由micro innovations制造),这样我就可以得到一个串行端口。Linux将设备识别为/dev/ttyUSB0

当我在其他本机linux程序(如输出转储程序)中使用该设备时,适配器工作正常,数据不断输入。然而,在我的程序中,数据进入大约10秒钟,然后我没有收到更多的数据,但我知道适配器连接到的硬件有更多的数据要处理

这段代码中有没有什么东西我可以修复,允许我在收到无限字符时接收它们,而不必重新启动程序来接收更多字符

我仍然希望使主线代码与串行例程异步,因为稍后我将添加更多代码

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>

#define BAUDRATE B57600
#define MODEMDEVICE "/dev/ttyUSB0" /* My USB to serial converter */
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

void signal_handler_IO ();   /* definition of signal handler */
int wait_flag=TRUE;          /* TRUE while no signal received */

int main(){
  int fd,res;
  struct termios oldtio,newtio;
  struct sigaction saio;           /* definition of signal action */
  char buf[255];

  /* open the device */
  fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
  if (fd <0) {perror(MODEMDEVICE); return -1; }

  /* install the signal handler before making the device asynchronous */
  saio.sa_handler = signal_handler_IO;
  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 */
  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);

  /* loop while waiting for input. */
  while (1) {
    usleep(100000);
    /* after receiving SIGIO, wait_flag = FALSE, 
    input is available and can be read */
    if (wait_flag==FALSE) { 
      res=0;
      /* Read up to 11 bytes which is remote packet length if file handle is valid */
      if (fd){
        res = read(fd,buf,11);
      }
      /* If there's at least 2 bytes (character plus ending null) then process it */
      if (res > 1){
        int n;
        /* Print each character as 2-digit hex  even if value is 0. */
        for (n=0;n<(res-1);n++){
          printf("%2X ",(unsigned char)buf[n]);
        }
        /* Print total number of characters received */
        printf(" = %d\n",res);
      }
      wait_flag = TRUE;      /* wait for new input */
    }
    /* Return to start of endless loop */
  }
  return 0;
}

/***************************************************************************
* signal handler. sets wait_flag to FALSE, to indicate above loop that     *
* characters have been received.                                           *
***************************************************************************/

void signal_handler_IO ()
{
wait_flag = FALSE;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义波特率B57600
#定义MODEMDEVICE“/dev/ttyUSB0”/*我的USB到串行转换器*/
#定义_POSIX_SOURCE 1/*兼容POSIX的源*/
#定义FALSE 0
#定义真1
无效信号处理器IO();/*信号处理器的定义*/
int wait_flag=TRUE;/*未接收到信号时为TRUE*/
int main(){
int-fd,res;
结构termios oldtio、NERTIO;
struct sigaction saio;/*信号动作的定义*/
char-buf[255];
/*打开设备*/
fd=打开(MODEMDEVICE,O|RDWR | O|NOCTTY);

如果(fd经典比赛条件:

在代码设置
wait\u flag=TRUE;
之前,可以调用
signal\u handler\u IO()
,例如在那些
printf()
调用期间

此时,您的程序将简单地停止


补救方法可以是在
if
之后立即设置
wait_标志
——但我建议查看linux
poll()
异步I/O调用。

经典竞态条件:

在代码设置
wait\u flag=TRUE;
之前,可以调用
signal\u handler\u IO()
,例如在那些
printf()
调用期间

此时,您的程序将简单地停止

一种补救方法是在
if
之后立即设置
wait_标志
——但我建议查看linux
poll()
异步I/O调用