Linking Arduino with Ubuntu without the Arduino's IDE [SOLVED]

I need to control a rover sending to Arduino some controls via USB. I send some char characters (w: forward, a: left etc) from a BeagleBone Black with Ubuntu, from a C++ program. Everything works, but I need to run the Arduino’s IDE, on the BeagleBone BEFORE starting my program. If I don’t run the Arduino’s IDE, the rover don’t’ move. I think the IDE sends some command to Arduino to initialize the USB, and I want to do the same, without the IDE, directly from my program. Any ideas?

main.cc (1.05 KB)

serial.cpp (788 Bytes)

serial.h (191 Bytes)

Codice Arduino.docx (14 KB)

It should be perfectly possible to communicate with the Arduino from Ubuntu. I do it all the time using JRuby and the JVM and there is no need to run the IDE except to upload programs to the Arduino.

I also occasionally use puTTY instead of the Arduino Serial Monitor.

Sorry, but life is too short to try to learn your C code (or anyone else's). Can you describe how your program tries to communicate with the Arduino and what the results are when it doesn't.

...R

What model arduino are you using? The Leonardo board has special requirements to establish proper USB serial communication. Standard Uno type board should have no special needs to work.

Thanks for your answer! Sorry, I'm a newbie... :cold_sweat: So, I'm using a Romeo-all in one (ATmega328) and I must use C/C++ code, cause we are working as a team. I open the USB with this command:

porta = open ("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

and with those settings:

fcntl(porta, F_SETFL, 0);
    struct termios options;
    tcgetattr(porta, &options);
    cfsetispeed(&options, B9600);
                cfsetospeed(&options, B9600);
    options.c_cflag &= ~CSIZE;
    options.c_cflag |=CS8;
    options.c_oflag &= ~OPOST;
    options.c_cflag |= (CLOCAL | CREAD);
    tcsetattr(porta, TCSANOW, &options);

then i write on the USB with this command:

void scrivi(int portafd, char c) // Scrive sulla porta seriale il comando indicato
{
    int n = write(portafd, &c, 1);

Arduino has this code: Serial.begin(9600); //Set Baud Rate to start reading the serial. char val = Serial.read(); to read the serial port switch(val) with options (w: forward, a: left etc...) If i run Arduino's IDE before running my program, everything works. If I don't run the IDE, the program doesn't work. Thank you for your help!

Thank you for summarizing your C code, but I'm afraid it is still almost impenetrable to a JRuby programmer.

However, I wonder if your problem is related to making the serial port available? Perhaps the Arduino IDE opens the port and, more importantly closes it afterwards so it is available to your C program.

You don't say exactly how it doesn't work. Are you getting errors in your C program, or have you built in code to check that the serial port opens properly?

If you are not getting errors on your PC then have you suitable diagnostic code in your Arduino sketch so you can find out if it is receiving anything - for example different led blink patterns.

Can you communicate with the Arduino using something like puTTY (rather than the Arduino IDE). If you can I would suspect there is a problem in you C code.

Can you write a PC test program in another language such as Python, Ruby, JRuby, Java?

...R

However, I wonder if your problem is related to making the serial port available? Perhaps the Arduino IDE opens the port and, more importantly closes it afterwards so it is available to your C program.

I think this is the problem. I don't think the problem is in my c++ code, cause i get the same problem if I try to comunicate with Ubuntu's terminal with this command:

stty -F /dev/ttyUSB0 speed 9600
echo -n w > /dev/ttyUSB0

When i try to send something on the Arduino's USB, a led on the Arduino blink, so I think Arduino get the command, but it doesn't move. Maybe there's something wrong in the Arduino code...

I had the impression that the Arduino program works perfectly with the Arduino Serial Monitor. If so, what can be wrong with it?

Of course if it doesn't work with the Arduino Serial Monitor you should correct the program until it does. And if due to the nature of the Arduino program it can't work with the serial monitor write another short sketch that does, and then get that to work with a PC program.

Rather than try to work with the complexities of redirecting output i suggest (again) that you use puTTY as an alternative to the Arduino serial monitor. The Arduino IDE uses the Java JVM whereas (I presume) puTTY is native Linux C stuff. I haven't found anything that works on the Serial monitor and not on puTTY.

...R

When you open the serial port, the Arduino resets and enters the boot loader to see if you want to upload a new program.

If you send data during this interval, it is eaten by the bootloader, because your program hasn’t started yet. This is likely what is happening for you.

Try waiting one or two seconds after opening the serial port before writing to it. That will give the bootloader time to time out and start your program.

Even better would be to wait for a “ready” signal from the program itself.

Hope that helps,

-br

@billroy, that's a good point. I usually get my sketches to print their name as the first thing they do in setup().

However I assume the Arduino resets when any program opens the serial port so I can't see how opening the Arduino IDE before running a C program could solve the problem as the IDE would have to close the port before the C program could access it and open it.

I have also been assuming that @giurizza is using a regular USB connection between the Beagle Bone and the Arduino.

...R

I apologize for the long time before replying, but I was busy with another project… Thanks to billroy! This was exactly my problem! I solved using sleep(3) before writing on the USB. Yes, the best solution is to have a ready signal, I’ll work on it.

Glad to hear it works. Thanks for letting us know.

...R