2012-11-09 39 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.