C++ 使用Linux与Arduino通信

C++ 使用Linux与Arduino通信,c++,c,linux,file,arduino,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 = f

这是我第一次用电脑与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);
}

请原谅我的无知。我试着研究它。不能让它工作。提前感谢您的帮助。

您没有对fopen/dev/ttyACM0,w;的返回值进行任何成功检查;。若fopen失败,则进一步使用文件是未定义的行为,导致分段错误。做点像

file = fopen("/dev/ttyACM0","w");  //Opening device file
if (file)
{
        //do something with file
}
else
     return 0;
#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;
}

另外,在结束main之前添加一个返回0。

您没有对fopen/dev/ttyACM0,w;的返回值进行任何成功检查;。若fopen失败,则进一步使用文件是未定义的行为,导致分段错误。做点像

file = fopen("/dev/ttyACM0","w");  //Opening device file
if (file)
{
        //do something with file
}
else
     return 0;
#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;
}
另外,在结束main之前添加一个返回0。

您将从fopen获得一个NULL返回,该NULL将被传递给fprintf,该fprintf需要一个有效的文件*并导致SEGV混乱

如果您使用fopen,您应该检查它返回的内容,以便为用户提供比分段错误更有用的信息

fopen故障的可能原因是您没有使用串行端口的权限

通常,您需要组拨号才能访问串行端口

作为root用户:

usermod-a-G拨号您的用户名

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

考虑在其他任何串行终端程序上使用minicom或microcom来访问串行端口,而不是自己编写

我还建议您让Arduino在启动时发送一条hello消息,这样您就可以确保您的波特率正确等等。

您从fopen收到一个NULL返回,NULL正在传递给fprintf,fprintf需要一个有效的文件*并导致SEGV混乱

如果您使用fopen,您应该检查它返回的内容,以便为用户提供比分段错误更有用的信息

fopen故障的可能原因是您没有使用串行端口的权限

通常,您需要组拨号才能访问串行端口

作为root用户:

usermod-a-G拨号您的用户名

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

考虑在其他任何串行终端程序上使用minicom或microcom来访问串行端口,而不是自己编写

我还建议您让Arduino在启动时发送一条hello消息,以便确保您的波特率正确等等。

失败时,fopen返回NULL,因此您可能会取消引用NULL指针,正确的方法是检查fopen的结果。然而,我会建议对类似这样的事情使用低级别IO

file = fopen("/dev/ttyACM0","w");  //Opening device file
if (file)
{
        //do something with file
}
else
     return 0;
#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;
}
on error open返回一个特殊值-1,因此您应该中止对它的写入

我很确定在您的情况下会出现权限拒绝错误,因为通常情况下/dev/tty*属于组拨号,并且默认情况下它们具有组写入权限,但由于您的用户可能不属于该组,因此您没有对/dev/ttyACM0的写入权限。

失败时,fopen返回NULL,因此,您可能要取消对空指针的引用,正确的方法是检查fopen的结果。然而,我会建议对类似这样的事情使用低级别IO

file = fopen("/dev/ttyACM0","w");  //Opening device file
if (file)
{
        //do something with file
}
else
     return 0;
#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;
}
on error open返回一个特殊值-1,因此您应该中止对它的写入


我很肯定,在你的情况下会有一个允许拒绝的错误,因为通常/DEV/TTY属于组拨号,默认情况下它们具有组写权限,但是由于你的用户可能不属于那个组,所以你没有写访问/DEV/TyACACM.< /P>它看起来不像C++代码。那么为什么要标记呢?这里有一个想法——你可以试着调试它。它看起来不像C++代码。为什么要标记呢?这里有一个想法-你可以试着调试它。还有什么用?为什么不返回一个错误代码而不是0?@iharob else部分表示文件打开失败。不要使用文件指针。关于返回值,没有任何标准,您可以使用对您有意义的任何值:-但是从main返回0意味着没有发生错误,并且不需要else,如果file!=默认情况下为NULL和一些值。@iharob好的,这取决于。大多数情况下,对于API,您是非常正确的。但是,在国际海事组织,未能fopen通常并不意味着main的失败。一条错误信息就足够了。我不同意你的看法。还有什么其他原因?为什么不返回一个错误代码而不是0?@iharob else部分表示文件打开失败。不要使用文件指针。关于返回值,没有任何标准,您可以使用对您有意义的任何值:-但是从main返回0意味着没有发生错误,并且不需要else


,如果文件!=默认情况下为NULL和一些值。@iharob好的,这取决于。大多数情况下,对于API,您是非常正确的。但是,在国际海事组织,未能fopen通常并不意味着main的失败。一条错误消息就足够了。我不同意你的观点。可能是fopen失败的原因,但不是SIGSEGV的原因。因此你还应该提到忽略fopen的结果是错误的。可能是fopen失败的原因,但不是SIGSEGV的原因。所以你还应该提到忽略fopen的结果是错误的。这通过正确的错误检查,代码应该通过stderr告诉OP问题的根源。ttyACM0的正确fopen失败:拒绝访问此代码通过正确的错误检查,应该通过stderr告诉OP问题的根源。ttyACM0的正确fopen失败:拒绝访问