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 に答える