2012-11-09 40 views
0

我試圖通過串口使用gcc(我使用mac)訪問機器人。通過串口訪問機器人

我已經作出了計劃,以發送簡單的命令:

HOME(進入),並有從機器人的一些反饋:這是
歸位序列命令。機器人將在每個軸上尋找它的尋找開關
並且將執行該位置上的尋找功能。 DO
你想要進行(是/否)?

我送

Y(輸入)

和機器人假設舉動。

現在訪問機器人正在使用調制解調器/終端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); 
    } 
+1

請使用空格來使您的代碼可讀,否則您將得到「TL; DR」註釋(如本文所示)。 – 2012-11-09 05:47:24

+1

當你得到像'buf'和'buff'這樣的名字時,我喜歡這樣的情況。 – vard

+0

你確定你能夠正確地配置你的串口。也許你的機器人在開機時通過串口有一些測試輸出? – vard

回答

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或淺黃色,直到您叫close(fd)後。

閱讀良好的C風格。你正在做一些不是錯誤的事情,但會讓你的生活更難。

0

有代碼(如緩衝區溢出等),使許多錯誤,但最明顯的一個我合作ULD點是在這裏:

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); 

如果你不想惹自己手動設置這一切,盡我helper function.