I am attempting to set up serial communications between C++ in XCode 10.1 on a Mac, and an Arduino Uno R3 running the latest sketchbook IDE. I have done more research than can be summarized, here, and have not found a working solution. Therefore, I am posting these comments and questions as they relate to a 2008 post that looks like it should work. If there is a program & sketch pair that are known to work, I would like to know where to find them.
The C++ program and the Arduino sketch of interest were posted by John_Ryan on October 11, 2008. Because of the length of the two pieces of code, I am including a link here. The post is at: MAC OS X c++ question - Syntax & Programs - Arduino Forum and is read-only. It is possible to find another Arduino forum post that has a similar solution, but both have similar problems. The 2008 post at the link includes a C++ program and a corresponding Arduino sketch.
The program and sketch compiled after I made two changes that are described below.
The connection is made via the USB-B-Type Arduino programming port and a Mac USB Type-A port.
In order to make the programs run, I had to make the following changes:
-
I changed the C++ open statement to reference the USP port as "/dev/cu.usbmodemFA141” because that is how the USB programming port is named on my Mac.
-
In the Arduino sketch, I changed the statement “
Serial.print char(13);
” to “Serial.print(char(13));
“ to correct a typo in the statement as it was published in 2008.
After the changes, the C++ program runs, and prints to the XCode console showing that it is connecting to the Arduino. Sometimes it hangs up and returns a -1 value from the C++ open statement. I tried putting a copy of a close statement after the definition of fd and before the open statement. Sometimes closing a file before an open statement will resolve issues, but that caused an error stating that the value of fd had not been initialized. I can live with that, because there are bigger issues and when the open statement returns a value greater than zero, we move on to see if a value of 0, 1, or 2 actually causes the pin 13 LED to turn off, on, or return its state as described in the C++ under the heading to “Write to Serial Port.”
To run the pair program/sketch pair, I do the following:
- Load the Arduino side to the microcontroller using the Arduino IDE.
- Open the IDE’s Serial Monitor so I might see what the Arduino sketch is trying to tell me.
- Open Xcode and run the C++ program
- With the Arduino IDE Serial Monitor open, I watch to see if anything is printed. However, nothing shows up.
- I know that something is happening on the Arduino because several lights blink when the C++ program is run, but the specified LED at pin 13 never changes state, and nothing is reported back. Nothing is printed to the Arduino IDE Serial Monitor window.
Is there something that I am missing in this and is there a list of steps that are known to work in this situation?