0

I'm trying to communicate my PC with an Arduino Mega via serial and after send the message via NRF to another Arduino.

But I don't know if the message is send correctly or if it even send the message. Because I can't debug using Serial Monitor. If it is opened the serial port appear as busy.

When I execute the code the TX led on the Arduino blinks, but nothing is send via NRF.

I made some tests using only the NRF between the Arduinos and it's working, but when I try using the PC - Arduino serial communication, nothing happens.

C Code

#include <stdio.h>
#include <string.h>

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

int main() {

  char buffer[] = {'1'};
  int error = 10;
  FILE *usb_port;

  usb_port = fopen(arduinoPort, "rwb");

  if(!usb_port) {
    perror(arduinoPort);
    return 1;
  }

  //memcpy(buffer, &error, sizeof(error));
  fwrite(buffer, sizeof(char), sizeof(buffer), usb_port);

  fclose(usb_port);

  return 0;
}

Arduino Code

#include <nRF24L01.h>
#include <RF24.h>
#include <printf.h>
#include <RF24_config.h>

RF24 radio(7, 8); // CE, CSN

uint64_t address = 0xF0F0F0F0E1LL;

void setup() {
  Serial.begin(9600);
  printf_begin();

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
  radio.printDetails();


}

void loop() {

  if(Serial.available() > 0) {

    char byteLido = Serial.read();

    radio.write(&byteLido, sizeof(byteLido));
    delay(1000);
  }
}
  • Did you stty /dev/ttyACM0 to the correct baud rate? – Edgar Bonet Jul 25 at 15:23
  • hmm.. i dont know how to do it on C code – Shinforinpola Jul 25 at 15:31
  • could you give me some tip on how to do it? – Shinforinpola Jul 25 at 15:34
  • First try from the command line, just to make sure this is the actual issue. – Edgar Bonet Jul 25 at 15:35
  • Sorry, but how can I do it from command line? – Shinforinpola Jul 25 at 15:39
1

Working with serial in C on Linux is not as simple as opening a file and reading from it (though in some circumstances that can work).

You should open the file using open() not fopen(), then use tcgetattr() and tcsetattr() etc to configure the port.

For example:

#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>

struct termios oldsettings, newsettings;
int fd;

// This signal handler will close the port cleanly leaving
// it in the same state it started in
void sighandler(int sig) {
    if (sig == SIGINT) {
        if (fd >= 0) {
            // Restore the settings to original
            tcsetattr(fd, TCSANOW, &oldsettings);

            // Close the port
            close(fd);

            printf("Program teminated. Thank you for playing.\n");
            // Exit the program
            exit(0);
        }
    }
}

int main(int argc, char *argv) {

    // Trap CTRL-C to close the port properly
    signal(SIGINT, &sighandler);

    fd = open("/dev/ttyACM0", O_RDWR);
    if (fd < 0) {
        fprintf(stderr, "Error opening serial port: %s\n", strerror(errno));
        return 10;
    }

    // Save the settings
    tcgetattr(fd, &oldsettings);
    // Grab the current settings again to modify
    tcgetattr(fd, &newsettings);

    // Set the baud rate
    cfsetispeed(&newsettings, B9600);
    cfsetospeed(&newsettings, B9600);

    // Make it a raw port
    cfmakeraw(&newsettings);

    // Apply the settings
    tcsetattr(fd, TCSANOW, &newsettings);

    char c;
    printf("Press CTRL-C to terminate\n");

    while (read(fd, &c, 1) == 1) {
        printf("Got character 0x%02x\n", c);
    }

    fprintf(stderr, "Reception terminated: %s\n", strerror(errno));

    return 0;
}

No one ever said programming C was easy. It makes you appreciate just how much work the Arduino API does behind the scenes for you...

Also you need to be aware that the Arduino resets (if you use an Uno, Mega, or other board with a separate USB interface chip) when you open the serial port. You need to wait about 2 seconds after opening the port before you ever send anything, or whatever you send will be completely ignored. You can do that with:

sleep(2);

after you have opened and configured the port.

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.