读取串口数据失败

时间:2015-04-27 14:29:58

标签: c++ linux

我目前正在研究linux嵌入式主板(sbc6000x,如果你很好奇),我正在尝试使用串口创建通信方式。 我似乎没有打开端口和写入它的问题,但阅读似乎是一个问题。在阅读时,我在循环中有一个小的printf,我在任何地方都看不到它(在终端中使用ctrl + C我不能强制执行应用程序,所以我认为某处存在问题。)

我发送的PC代码如下所示:

int open_port()
{
speed_t baud = B115200; /* baud rate */
int n, fd; /* File descriptor for the port */
std::cout << "Ouverture du port" << std::endl;
  fd = open("/dev/ttyS0", O_RDWR);
std::cout << "Configuration du port" << std::endl;

struct termios settings;
tcgetattr(fd, &settings);

cfsetospeed(&settings, baud); /* baud rate */
settings.c_cflag &= ~PARENB; /* no parity */
settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
settings.c_lflag = ICANON; /* canonical mode */
settings.c_oflag &= ~OPOST; /* raw output */

tcsetattr(fd, TCSANOW, &settings); /* apply the settings */
tcflush(fd, TCOFLUSH);

  if (fd == -1)
  {
   /* Could not open the port. */
    perror("open_port: Unable to open /dev/ttyS0 - ");
    std::cout << "Echec de l'ouverture du port" << std::endl;
  }
  else
  {
    fcntl(fd, F_SETFL, 0);
std::cout << "Ecriture sur le port" << std::endl;
  n = write(fd, "TEST\r\n", 4);
  if (n < 0){

    std::cout << "Echec de l'écriture sur le port" << std::endl; //failed to write
  }
  else
  {
      std::cout << "Ecriture terminée" << std::endl;
  }
  }

  close(fd);  /* cleanup */
  return (fd);

}

运行时,在我的ubuntu pc上我没有错误。

目标是这样的:

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

int res;
char buffer[20];
int tampon=0;
std::string data="";
res = system("printf \"Ecoute serial \n \" > /dev/tty1"); //sometext

speed_t baud = B115200; /* baud rate */
int n, fd; /* File descriptor for the port */
//std::cout << "Ouverture du port" << std::endl;
res = system("printf \"Ouverture du port \n \" > /dev/tty1"); //opening the port
fd = open("/dev/ttyS0", O_RDWR);
//std::cout << "Configuration du port" << std::endl;
res = system("printf \"Configuration du port \n \" > /dev/tty1"); //configuration of the port

struct termios settings;
tcgetattr(fd, &settings);

cfsetospeed(&settings, baud); /* baud rate */
settings.c_cflag &= ~PARENB; /* no parity */
settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
settings.c_lflag = ICANON; /* canonical mode */
settings.c_oflag &= ~OPOST; /* raw output */

tcsetattr(fd, TCSANOW, &settings); /* apply the settings */
tcflush(fd, TCOFLUSH);

  if (fd == -1)
  {
   /* Could not open the port. */
    perror("open_port: Unable to open /dev/ttyS0 - ");
    //std::cout << "Echec de l'ouverture du port" << std::endl;
    res = system("printf \"Echec de l'ouverture du port \n \" > /dev/tty1"); //failed to open
  }
  else
  {
      res = system("printf \"Port serie OK \n \" > /dev/tty1"); //opening OK
  }
  while (fd!=-1) {


      //read the port
      do
                  {
                      tampon= read(fd,buffer,1);

                      if(buffer[0]=='\r')
                      {
                          read(fd,buffer,1);//read the following char which should be \n
                          break;
                      }
                      else
                      {
                          data.append(1,buffer[0]);
                          printf ("data = %i \n",buffer[0]);
                      }
                  }while(tampon>0);
}

  close(fd);  /* cleanup */

return a.exec();
}

我知道while循环有点脏,因为我不明白为什么我的printf不在这里,为什么我似乎失去了对应用程序的控制(不能ctrl + c它)。这是我第一次进行串行通信,所以也许我错过了一些东西,这并不奇怪。

0 个答案:

没有答案