Computer Serial Port Communication with Arduino

Hello,

I am currently working on a project where I am extracting data from Microsoft X-Plane 10 to drive my servo motors. I am using a plugin to extract these data. This plugin is written in C++. Inside this plugin, I am processing the extracted data. I want to be able to send these processed data from my plugin to arduino. To do this I need some sort of serial port communication from the computer to the arduino. I was wondering if there are any existing codes that I can use so I can directly send my data from the plugin to the Arduino microcontroller. If not then can someone give me some guidance as to how to do this.

I wrote a demo here to illustrate communication with an Arduino using Python. It may give you some ideas.

Life is too short to program in C/C++ is there is an alternative :slight_smile:

...R

Do you happen to have one for C++ :D. The plugin I am using to extract data from X-Plane 10 is written in C++.

Shiki225:
Do you happen to have one for C++ :D. The plugin I am using to extract data from X-Plane 10 is written in C++.

Did you read the second line of my earlier post ????

It's up to you to do the hard work of rewriting the Python code into C++.

...R

I am using C in Linux for serial port communication. I looked up online and saw that many people use the system calls to finish this task: open(), to open the serial port; write(), to write to the buffer and send to the port; read(), to read from the buffer. I was able to using open() to open a port (“/dev/ttyACM0”) successfully, and the function returns the correct value for the file descriptor (23, in our case). I set up the port as most people did online, and Ill attach that part of the code at the end. However, when we tried to use write() function to simply pass the string testBuffer = “12” to the port, an error message – “broken pipe” pops up. The three parameters for the write function are file descriptor value(fd), content(testbuffer), and the size of the content (sizeof(testBuffer)). I have allocated the space for the testBuffer, so the buffer should not be the problem. I can manually send data to the port to the Arduino by using command “echo” in the terminal, so the other end of the connection should not be the problem neither. Then, a grad student told me that I might need to manually make the second argument of the function to the packet protocol that the port can recognize it. Therefore, I tried to make testBuffer to 8 hex numbers array, then the error message becomes “Bad destination address”. So right now I’m lost about where the error exactly is.

/* allow the process to receive SIGIO /
fcntl(port, F_SETOWN, getpid());
/
Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) /
fcntl(port, F_SETFL, FASYNC);
/

BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/

cfsetispeed(&specs, B9600);
cfsetospeed(&specs, B9600);
specs.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
/
specs.c_iflag = IGNPAR | ICRNL;
/

Raw output.
/
specs.c_oflag = 0;
/

ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
specs.c_lflag = ICANON;

// Set the timeouts
// VMIN is the minimum amount
// of characters to read.
specs.c_cc[VMIN]=1;

// The amount of time to wait
// for the amount of data
// specified by VMIN in tenths
// of a second.
specs.c_cc[VTIME]=0;

tcflush(port, TCIFLUSH);
tcsetattr(port, TCSANOW, &specs);

Attach is a file of the screenshots that I took that shows the error. May need to run Linux to run this. Just cd to the folder's directory and type make. Afterwards, run X-Plane and the error should pop up.

testing.zip (465 KB)

Forget my post above ^.

I finally got the serial communication to work with X-Plane 10 real time. The problem I have now is my X-plane 10 seems to get very slow after a certain amount of time and I think this is the case because it is storing too many data? (I can be wrong). What I did was I change my baud rate to the highest which help me keep X-Plane running but eventually the X-Plane 10 freezes again. I also tried changing my delay at which I am spitting information to the Arduino. This help X-Plane run a little long but eventually it freezes again.

Does this have something to do with my Arduino not being cleared? Does it have something to do with sending data to the Arduino but it is not being cleared?

Another question is, can Arduino slow down a computer in this type of situation?