PC-Arduino comms using C++ (in Linux)

Hi

I already read (and used) the PC-Arduino comms using Python. It works :slight_smile:
I already implemented a small C++ program/Arduino Sketch that works, but... needs the Arduino IDE serial comm to start.

In the Python code it is written "import serial"

what is/where is the "serial" library?

is there a C++ equivalent?

Cheers

Serial support in the style of;

Serial.print("Hello World");

Is built into the IDE, check out some of the basics examples, such as DigitalReadSerial or ReadASCIIString.

I meant the C++ library.

I guess the "termios.h" C library is the equivalent

#include <termios.h> // POSIX terminal control definitionss

Did I guess right, or there is a "serial.h" library specific to Arduino connections?

You don't need to specifically #include anything to support serial comms in the Arduino IDE

As @srnet said, it is built into the Arduino environment provided by the IDE. The IDE Serial Monitor provides a basic serial terminal, but if you want to use screen controls such as cursor positioning, then you can use an alternative terminal program on the PC

I think the OP is asking about serial handling in a C++ program running under Linux, in which case, look for a tutorial on serial char file handling

Reading back, that is quite possible, but I am confused by

Sorry, it was a bit short I know

  1. I load the sketch into the Arduino using the IDE
  2. I start the C++ program
    => Nope ! the C++ program can not open the communication channel

1') I load the sketch into the Arduino, using the IDE and then I open the serial console (of the IDE)
2') I start the C++ program
=> Yes !!! it works as it should be and I can now close the Arduino IDE (serial console and the IDE itself).

I already read about this, it seems that I need to wait for the Arduino to reset... I was trying to adapt the code from "PC-Arduino comms using Python"... but I guess that instead of the "include Serial" (on the Python code) I need the "termios.h".

I didn't try that yet, a kind of merging of two contributions I read in the forum.

Which Arduino board are you using ?

Using AVR based Arduino boards, when the Arduino Serial port is opened, either by opening the Serial monitor or by an external connection, then the board is reset

This can be prevented by making a hardware modification to the board and there are also Arduino and other boards that do not reset the board because the serial connection is handled by another chip on the board and not the main processor

I'm testing in an Arduino Mega 2560, but I want to use the final code in a Arduino Uno.

Both of those will reset when the serial port is opened

... and that's my problem. From what I read I need to wait for the Arduino to reset and then I can begin the communication.

My questions are:

  1. "when the Arduino Serial port is opened"
    I imagine that the "openning" happens when I write in the C++ code something like " fd_ard = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);", the O_RDWR is (!?) read+write

How can I wait for the Arduino to reset?

  1. I imagine (I didn't test it yet) that I can open it RDWR, then (as suggested in the Python code) wait for a response of the Arduino (that will be in the sketch), and then pursue with the communication (without closing the connection).

Am I thinking correctly?

The "is there anybody there ?" technique is probably the most certain way of ensuring that the connection is available and could be extended to have the Arduino send back the CRC of the received message to be checked at the PC side to ensure that the message was received correctly

Once open, do you actually need to close the serial connection ?

How complicated do you want to make this ?

No I don't... I will open it and then when the program ends ... the connection is closed.

The simplest possible :slight_smile:

The goal is to have a simple program then receive commands and pass them to the Arduino ... this Arduino is in term the master of a network of Arduinos receiving commands and making actions... everything is working (almost) the PC/C++ program is the last part, for the prototype, version 0.

Later today I will try this and I will report to you. Thank you for all your help.

The simplest way is to open the serial connection, wait a while then send blindly with no way of knowing that the data arrived

At the very least you would be wise to top and tail your messages with begin and end markers to ensure that the Arduino keeps in step with the data being sent

See Serial input basics - updated

Too long ago for me to remember :wink: Maybe this helps as a generic guide: linux - How to open, read, and write from serial port in C? - Stack Overflow.

In Arduino's setup()

void setup()
{
  Serial.begin(yourBaudrate);
  Serial.print("<Arduino ready>");
}

And in your Linux code, wait for the message before starting to talk to the Arduino.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.