【问题标题】:USB to Serial adapter - receive works OK for a limited timeUSB 转串口适配器 - 在有限的时间内接收正常
【发布时间】:2018-11-11 04:55:59
【问题描述】:

我从 Linux Serial HOWTO 页面中借用了以下大部分代码来创建我的程序,以从串行端口无休止地读取字符。我正在使用 USB 转串口适配器(由微创新制造),所以我可以获得一个串口。 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;
}

【问题讨论】:

    标签: string io serial-port usb byte


    【解决方案1】:

    经典比赛条件:

    signal_handler_IO() 可以在您的代码集wait_flag = TRUE; 之前调用,例如在那些printf() 通话期间。

    此时您的程序将停止。

    补救措施可能是在 if 之后立即设置 wait_flag - 但我建议查看 linux poll() 异步 I/O 调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      相关资源
      最近更新 更多