0

gccを使用してシリアルポート経由でロボットにアクセスしようとしました(私はMacを使用しています)。

簡単なコマンドを送信するプログラムを作成しました。

HOME (Enter) とロボットからのフィードバックがあります。これは
ホーミング シーケンス コマンドです。ロボットは各軸でホーミング スイッチを探し、その位置
でホーミング機能を実行します。続行
しますか (Y/N) ?

そして私は送ります

はい(入力)

ロボットが動くと仮定します。

ロボットにアクセスするには、モデム/ターミナル Zterm を使用します。このモデムはボーレート 38400、8N1 を使用します

同じボーレートと 8n1 を使用します

これは私のコードです。コードがロボットを動かせない理由がわかりません。

ありがとうございました

ダニエル

    #include<stdio.h>   /* Standard input/output definitions */
    #include<stdlib.h>
    #include<string.h>  /* String function definitions */
    #include<unistd.h>  /* UNIX standard function definitions */
    #include<fcntl.h>   /* File control definitions */
    #include<errno.h>   /* Error number definitions */
    #include<termios.h> /* POSIX terminal control definitions */
    //#include<conio.h> 

    /*
     * 'open_port()' - Open serial port 1.
     *
     * Returns the file descriptor on success or -1 on error.
     */

    int buf_size;
    char *buf;
    char *buff;
    int fd; /* File descriptor for the port */

    int open_port(void)
    {

       fd = open("/dev/tty.USA28X1a2P2.2", O_RDWR | O_NOCTTY | O_NDELAY);  //      USA28X1a2P2.for keyspan 
    //fd = open("/dev/ttys000", O_RDWR | O_NOCTTY | O_NDELAY);   
    if (fd == -1) {
     /*
      * Could not open the port.
      */
      perror("cannot open");
   }
     else 
        fcntl(fd, F_SETFL, 0);
        struct termios options;
          /* 
           * Get the current options for the port...
           */
          tcgetattr(fd, &options);

          /*
           * Set the baud rates to 38400...
           */

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


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

         options.c_cflag |= (CLOCAL | CREAD);

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

         tcsetattr(fd, TCSANOW, &options);

         options.c_cflag &= ~CSIZE; /* Mask the character size bits */

         options.c_cflag &= ~PARENB;
         options.c_cflag &= ~CSTOPB;
         options.c_cflag &= ~CSIZE;
         options.c_cflag |= CS8;

     return (fd);
    }

    int main(int argc, char**argv) {
        buf =malloc(20);
        buff=malloc(20);

    //   strcpy(buf,"HOME");
    //    strcpy(buff,"Y");
        open_port();
        printf("type the command using Capital Letter : \n");
        scanf("%s",buf);
        write(fd, buf,20); // 
        write(fd," \r ",5);
        printf("Command = %s\n", buf);
        read(fd, buff,50);
        printf(" %s \n",buff);
        free(buf);
        free(buff);
        printf("type Y/N : \n");
        scanf("%s",buf);
        write(fd, buf,2);
        write(fd,"\r",2);

    //    free(buf);
    //  free (buff);
    //    printf("type Y/N : \n");
    //    write(fd, buf,20);
    //    printf("You choose %s \n",buff);
    //    free(buf);

        close(fd);
    }
4

2 に答える 2

1

あなたは一度に読みすぎています。バッファを20バイトとして割り当てますが、buff=malloc(20);ここでは50を読み取りますread(fd, buff,50);。これは、せいぜい奇妙な動作につながる可能性があります。使用するスペースと同じ量を割り当てるようにしてください。割り当てる必要はなく、配列を使用できます。

char buf[1024];
char buff[1024];

次に、それらを再度使用する前に、メモリを解放します。

 free(buf);
 free(buff);
 printf("type Y/N : \n");
 scanf("%s",buf);

に電話するまで、bufまたはbuffを解放しないでくださいclose(fd)

良いCスタイルも読んでください。あなたはバグではないいくつかのことをしていますが、後であなたの人生を難しくします。

于 2012-11-09T05:52:47.310 に答える
0

コードには非常に多くのエラー(バッファオーバーランなど)がありますが、私が見つけた最も明白なエラーは次のとおりです。

tcsetattr(fd, TCSANOW, &options);

options.c_cflag &= ~CSIZE; /* Mask the character size bits */

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options構造体のフィールドは、ファイル記述子のプロパティの設定に使用した後でのみ設定します。tcsetattr()このシーケンス全体の最後に呼び出しを配置する必要があります。

options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &options);

これらすべてを自分で手動で設定することを台無しにしたくない場合は、私のヘルパー関数を試してください。

于 2012-11-09T05:52:34.143 に答える