How do I export these values?

Hello, I'm working on a project that requires me to perform communications between a Beaglebone Black and a pc running linux/unix/ubuntu.
We figured out how to communicate over the port manually via terminal commands (after exporting the port through c++) and now I'm trying to get some code written in c++ so that we can get away from bash commands.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <fstream>
#include <cstdlib>
#define deviceport "/dev/ttyACM0"

using namespace std;

int main(int argc, char * argv[])
{
	ofstream file;
	file.open(deviceport);
	for(int i = 1; i <= argc; i++)
	{
		file << argv[i] << " ";
	}
	file << "\n";
//	file.flush();
	file.close();
//	fflush(ofstream);
}

Here's the code I have right now... My problem is that is sort of works. By that, I mean after I compile and run this code, the beaglebone I have listening for data on that tty port doesn't get the output- until I echo (somevalue) > /dev/ttyACM0
At which point it promptly sends the data over, and I see the output on the beaglebone as (whatIputin)(whatiechoin) of course there are no spaces or line breaks or anything... but that's not my concern. What I want to know is how I can forgo the echo nonsense, as we need fully automated transmission of information from PC to bone. I'm rather new to c++, so I don't know exactly what to call this problem, otherwise I'd likely have found it on google three hours ago... I would just like to know what I have to do to actually SEND the values, I was going to read the source of echo, however I'm running on a live key, and haven't set up my apt-repositories. I would just run bash from c++ but our project leader believes bash is too insecure.
sorry if my wording/spelling is a bit erratic, I'm frustrated, its 4am, and the laptop I'm using has an annoyingly sensitive touchpad that sends me halfway through a paragraph as i type when my palm brushes over it so that I unknowingly type half of a word in the middle of nowhere. Anyway, if you know how to solve this problem I would greatly appreciate the help. Also, if you do help, please provide an explanation, code I don't understand is as useful to me as the code I already have which is non-functional. If it helps, I have downloaded a library which provides functions for this sort of thing, as well as an example which I have tested and it DOESN'T have the problem my code does, the issue is that I cant tell the difference between what my code is doing and his.
serialib@googlegroups.com
google that, first result should go to the library.

Thanks,
DocFail
You're using buffered file access, so you need to use endl to write the end of line.

However, for comms, it's better to use open/write/close; i.e. the native unbuffered I/O calls.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char* argv[])
{
    int fd = open(deviceport, O_WRONLY);
    if (fd != -1)
    {
        for (int i = 0; i < argc; ++i)
        {
            char buf[32];
            snprintf(buf, sizeof(buf), "%s ", argv[i]);
            write(fs, buf, strlen(buf));
        }
        write(fs, "\n", 1);
        close(fd);
    }
 
    return 0;
}
Last edited on
> You're using buffered file access, so you need to use endl to write the end of line.
But close() or the destructor ought to flush the stream


By the way, there is out of bounds access in the OP code
Topic archived. No new replies allowed.