使用Linux与Arduino通信

时间:2015-01-01 10:30:22

标签: c++ c linux file arduino

这是我第一次使用计算机与Arduino通信。我使用Ubuntu 14.04。这是用于写入文件的C程序。 Arduino出现了ttyACM0。

使用gcc编译时,编译器显示错误说:

  

分段错误(核心转储)

如何纠正此错误。

#include<unistd.h>
#include<stdio.h>
int main() {
  char data[] = {'f','b','r'};  //Random data we want to send
  FILE *file;
  file = fopen("/dev/ttyACM0","w");  //Opening device file
  int i = 0;
  for(i = 0 ; i < 3 ; i++) {
    fprintf(file,"%c",data[i]); //Writing to the file
    fprintf(file,"%c",','); //To separate digits
    sleep(1);
  }
  fclose(file);
}
请原谅我的无知。我尝试过研究它。无法使其发挥作用。在此先感谢您的帮助。

4 个答案:

答案 0 :(得分:3)

您从NULL获得了fopen()NULLfprintf()正在传递给SEGV,而fopen期待一个有效的FILE *并且搞乱导致{{ 1}}。

如果您使用fopen(),则应检查它返回的内容,以便为用户提供比“分段错误”更有用的功能。

dialout失败的可能原因是您无权使用串口。

通常,您需要组usermod -a -G dialout才能访问串行端口。

正如root所做的那样:

{{1}} yourusername

然后注销并重新登录,以便获得新组。

考虑使用minicom或microcom(在任何其他几个串行终端程序中)访问串口而不是自己编写。

我还建议您让Arduino在启动时发送一条问候消息,以确保您拥有正确的波特率等...

答案 1 :(得分:1)

您没有对fopen("/dev/ttyACM0","w");的返回值进行任何成功检查。如果fopen()失败,进一步使用file是未定义的行为,导致分段错误。做点什么

file = fopen("/dev/ttyACM0","w");  //Opening device file
if (file)
{
        //do something with file
}
else
     return 0;

另外,在结束return 0之前添加main()

答案 2 :(得分:0)

失败时fopen返回NULL,因此您可能会取消引用NULL指针,正确的方法是检查fopen的结果。但是我会建议像

那样的低级别IO
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main()
{
    char data[] = {'f','b','r'};  //Random data we want to send
    int fd;
    int i;

    fd = open("/dev/ttyACM0", O_WRONLY);  //Opening device file
    if (fd == -1)
    {
        perror("cannot open /dev/ttyACM0");
        return -1;
    }

    for(i = 0 ; i < 3 ; i++)
    {
        write(fd, &(data[i]), 1);
        write(fd, ",", 1);

        sleep(1);
    }
    close(fd);

    return 0;
}
错误open上的

会返回一个特殊值-1,因此您应该中止写入。

我非常确定在您的情况下会出现permission denied错误,因为通常/dev/tty*属于组dialout并且默认情况下他们具有群组写入权限,但是由于您的用户可能不属于该群组,因此您无法对/dev/ttyACM0具有写入权限。

答案 3 :(得分:0)

// the following code:
//  compiles cleanly
//  performs appropriate error checking
//  has proper return statement

#include <unistd.h> // sleep()
#include <stdio.h>  // fopen(), fclose(), fprintf(), perror()
#include <stdlib.h> // exit() and EXIT_FAILURE

int main()
{
    char data[] = {'f','b','r'};  //Random data we want to send
    FILE *file;
    if( NULL == (file = fopen("/dev/ttyACM0","w") ) )  //Opening device file
    { // then fopen failed
        perror("fopen failed for ttyACM0" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    int i = 0;
    for(i = 0 ; i < 3 ; i++)
    {
        if( 0 >= fprintf(file,"%c",data[i]) ) //Writing to the file
        { // fprintf failed
            perror("fprintf data failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, fprintf successful for data

        if( 0 >= fprintf(file,"%c",',') ) //To separate digits
        { // then, fprintf failed
            perror( "fprintf for comma failed");
            exit( EXIT_FAILURE );
        }

        // implied else, fprintf successful for comma

        sleep(1);
    } // end for
    fclose(file);
    return(0);
} // end function: main