使用超级终端启动用于串行通信白化的USB端口

时间:2014-05-25 12:45:21

标签: c serial-port libusb usbserial

我正尝试从下面的代码启动串行通信。然而问题是我必须使用相同的设置打开超级终端然后关闭它,然后在下面运行用C编写的另一个程序中的代码,以使代码按预期工作。我非常想跳过超级终端的第一步,但我还没有弄清楚是什么造成了这种奇怪的行为。 我已经发布了下面的启动代码以及我的kbhit()代码,这可能是导致此行为的原因。如果有人能告诉我如何解决这个问题,我将非常感激。

int kbhit(){ //Detects if a key has been pressed, 1 if true else 0 
  struct termios oldt, newt;
  int ch;
  int oldf;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0;
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF){
    ungetc(ch, stdin); //Puts back the pressed key to the stack
    return 1;
  }
  return 0;
}  

通过USB端口进行串行通信的启动代码如下所示:

#include "USB.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  //the desired baud rate
#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){
    device = modemdevice;
    fd = open (device, O_RDWR | O_NOCTTY|O_NONBLOCK );//|O_NONBLOCK has been added
    if (fd < 0){
      perror (device);
      exit(-1);
    }
    tcgetattr (fd, &oldtio);    /* save current settings */
    bzero (&newtio, sizeof (newtio));   /* clear struct for new port settings */
    /* 
     *BAUDRATE: Set bps rate. 
     *CS8     : 8n1 (8bit,no parity,1 stop bit)
     *CLOCAL  : local connection, no modem contol
     *CREAD   : enable receiving characters
     **/
    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | ICRNL;
    /*
     * 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] = 10; // 0 before
    newtio.c_cc[VMIN] = 0;  /* blocking read until 0 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' */
    tcflush (fd, TCIFLUSH);
    tcsetattr (fd, TCSANOW, &newtio);
    return fd;
}

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

0 个答案:

没有答案