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


  return 0;
  • why are you not using the arduino IDE? – jsotola Jul 3 '19 at 4:20
  • 1
    @jsotola, because they write for PC – Juraj Jul 3 '19 at 5:28

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) {
    return 1;

I would expect you see something like

/dev/ttyACM0: No such file or directory


/dev/ttyACM0: Permission denied

that could help you pin down the real problem.

| improve this answer | |
  • 1
    Thank you so much. It wasn't working because i opened the serial monitor and because the message i could see it. When serial monitor is opened the serial is busy. – Shinforinpola Jul 3 '19 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.