0

我用c编写了一个用于读写串口的程序。但问题是我正在使用while循环,它不断地通过串口发送命令。

  1. 我想在串口上写一些命令
  2. 等待答复
  3. 写另一个命令
  4. 等待一些答案等等

我的代码是这样的: -

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
#include <time.h>

 int main()

  {   
   printf("\n");
   printf("Please wait till serial Port is  being Initialized .... \n");
   sleep(2);
   printf("\n................. Initializing ................\n");
   sleep(2);
   printf("\n");
   printf("\n\n");
   static int fd = 0;
       int len;
       char res[100];
       char s[100] = " Hellow World";


     struct termios options;

    //==================================================================
    //                  hard coaded port ttyO3
    //==================================================================

      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); 
          fcntl(fd, F_SETFL, O_NONBLOCK);

     //==================================================================
     //                             Error Handling
     //==================================================================

            if (fd < 0)
      {
      printf("Serial open error %d %s\n", errno, strerror(errno));
          }
      printf("\n");

    printf("\n==================================================\n");

   //==================================================================
   //                 Get the current options for the port...
   //==================================================================

    tcgetattr(fd, &options);


         //==================================================================
    //                Set the baud rates to 115200...
   //==================================================================


    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);

    //=================================================================
   // Enable the receiver and set local mode..
  //==================================================================

    options.c_cflag |= (CLOCAL | CREAD);

//==================================================================    
// Set the new options for the port...
//==================================================================

     tcsetattr(fd, TCSANOW,&options);

      while(1)
        {  



      write(fd,s,strlen((char*)s));   
      sleep(1);

      len = read(fd,res,100);  
      if (len < 0)
       {   
         if (errno == EAGAIN)
         {         
               continue;
         }
         else
         { 
             printf("read error %d %s\n", errno, strerror(errno));
         } 
       }
     else
      {                   
        res[len < 100 ? len:100] ='\0';
        printf("read %d chars: %s\n",len, res);
      } 
    }
     close(fd);
    } 

我在哪里错了。

谢谢并恭祝安康

4

2 回答 2

1

看起来文件描述符以非阻塞 ( fcntl(fd, F_SETFL, O_NONBLOCK);) 的形式打开。如果你想等待数据被读取,你必须有一些阻塞操作。

您可以使文件描述符阻塞,或使用一些异步管理器,如select.

于 2013-08-27T05:43:25.747 回答
1

由于您使文件描述符非阻塞(两次),因此您必须作为系统在步骤之间等待。执行此操作的一种常见方法是select系统调用,您可以使用它来等待文件描述符中可以读取的内容。

可以这样做:

write(...);

fd_set poll_set;
FD_ZERO(&poll_set);
FD_SET(fd, &poll_set);

/* Wait, with no timeout, for something to be readable */
int rc = select(fd + 1, &poll_set, NULL, NULL, NULL);
if (rc == -1)
{
    perror("select");
}
else
{
    /* Continue to next step, where you read */
}

另请注意,读取调用实际上可能不会在第一次尝试时收到完整的消息。您可能需要多次阅读才能获得完整的信息。如果您的消息是固定大小的,则在循环中读取,同时减少要读取的字节数,直到您收到完整的消息。如果您的消息有消息结束标记,则逐字节读取,直到您收到为止。如果您的消息包含具有消息大小的标头,则使用第一个固定大小的读取方法两次,一次获取标头,一次获取数据。

于 2013-08-27T05:44:59.990 回答