user54217: I'm trying to send some data to my arduino mega 2560 using serial communication.
I'm using the exemple that I found here without success.
Every site I read say that I can send data to the arduino board like writing to any file on ubuntu.
But I alway get Segmentation fault
Here is my code
#include <stdio.h>
#include <string.h>

char arduinoPort[] = "/dev/ttyACM0";

int main() {

  char buffer[] = {'1'};

  FILE *usb_port;

  usb_port = fopen(arduinoPort, "rwb");

  fwrite(buffer, sizeof(char), sizeof(buffer), usb_port);

  fclose(usb_port);

  return 0;
}


user7508: Your program works flawlessly on my Ubuntu 18.04. I would guess that you
have a problem with your setup (flaky USB port, permission problems...)
rather than the program itself. Still, I would recommend you add some
error checking: right after the fopen() call,
if (!usb_port) {
    perror(arduinoPort);
    return 1;
}

I would expect you see something like
/dev/ttyACM0: No such file or directory

or
/dev/ttyACM0: Permission denied

that could help you pin down the real problem.

54217: <p>I'm trying to send some data to my arduino mega 2560 using serial communication.</p>

<p>I'm using the exemple that I found <a href="https://forum.arduino.cc/index.php?topic=178913.0" rel="nofollow noreferrer">here</a> without success.</p>

<p>Every site I read say that I can send data to the arduino board like writing to any file on ubuntu.</p>

<p>But I alway get <code>Segmentation fault</code></p>

<p><strong>Here is my code</strong></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

char arduinoPort[] = "/dev/ttyACM0";

int main() {

  char buffer[] = {'1'};

  FILE *usb_port;

  usb_port = fopen(arduinoPort, "rwb");

  fwrite(buffer, sizeof(char), sizeof(buffer), usb_port);

  fclose(usb_port);

  return 0;
}
</code></pre>
 7508: <p>Your program works flawlessly on my Ubuntu 18.04. I would guess that you
have a problem with your setup (flaky USB port, permission problems...)
rather than the program itself. Still, I would recommend you add some
error checking: right after the <code>fopen()</code> call,</p>

<pre class="lang-cpp prettyprint-override"><code>if (!usb_port) {
    perror(arduinoPort);
    return 1;
}
</code></pre>

<p>I would expect you see something like</p>

<pre class="lang-none prettyprint-override"><code>/dev/ttyACM0: No such file or directory
</code></pre>

<p>or</p>

<pre class="lang-none prettyprint-override"><code>/dev/ttyACM0: Permission denied
</code></pre>

<p>that could help you pin down the real problem.</p>
