0
votes

I'm a beginner in programming, and I must write a plug-in in C++ in a preexisting program (for a project). I'll spare you the details, but I must communicate with an Arduino (USB connected, serial communication), send and read data. I already know how to write to the Arduino device, but I don't know how to read the data from the Arduino ?

To send data to the arduino, I simply use that :

string device ("/dev/ttyACM0 ");
string cmd ("Threshold reached");
system ( (device + cmd).c_str () );

I would like it very much if the reading was as simple as the writing :D

Thank you for your answers

edit : The program runs on lUbuntu and (2 different computers) Ubuntu 14.10 Reedit :

I found a program in c that works juste fine, I compile it and call the binary thanks to "system", it works too. Here is a link to the explanations :https://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/

I know now how to read and write, there's only one issue left : recognizing the data that I'm interested in. So far i've got : string readings = system (./arduino-serial -b 9600 -p /dev/ttyACM0 - r); (is this accurate ? can i put that in a variable ?)

if (readings == "dR:") { int requestedDensity = /that's my issue/;} In my computer's buffer, I'll receive all sort of datas, like, "Asked Temperature 9 : 25 / Humidity : 74 / dR: 80/door 1 opened" and I want to start reading after "dR:" and stop before " / ". Will my condition work ? Will the program start reading just after dR: ?

Then, if that's true, if I convert it like that int requestedDensity = std::stoi (readings); , will it stop reading as soon as it reads something not convertible ? Will it stop at "/" without any errors ? (unfortunately i'm running out of time for my project so I can't really afford to try every answer i find...)

1
I don't believe that your write command works. I think it is missing a <stark
I tried it and it worked though (the Rx led from my Arduino blinked, no error from compiling,..). Any idea of how to read data ?Bernard
@Bernard The LED blinked because it was sending an error message, probably "/dev/ttyACM0: Permission denied" because you tried to execute it and it doesn't have execute permissions (I hope).David Schwartz
«I hope» Why so ? I changed the permissions, added the user name on tty and dialout, I m not sure if that's what your talking about ? I m gonna check the nature of the message received on the arduino asap... I did those things under the advices of someone I think trustable. (Not on my own)Bernard
@Bernard Because executing a terminal is nonsensical, so you shouldn't have permission to execute a terminal. But that's what your code tries to do. (Read the docs for system, they say it "executes the command specified". What command did you specify?)David Schwartz

1 Answers

0
votes

After research and reflection, I decided not to use the serial readings in my program, but only to send data to the Arduino and let it process. So, without further explanations, here's my (simple) code :

//communication des infos avec l'arduino

char buffer [50];               //chaîne stockée dans buffer
int n;
n = sprintf (buffer, "%d", densityLevel);       // n est le nombre de carac
std::cout << "Density level : "<< buffer<<endl;

string lvl = buffer;
string cmd = "c_dL:" + lvl;                      //concaténation
system((string ("./arduino-serial -b 9600 -p /dev/ttyACM0 -s "+ cmd)).c_str());//envoi de l'info par arduino-serial (qui doit être dans le dossier du bin useTracker)

So I tried it, and verified what message the arduino received, and it's working. Since my project's "presentation" deadline has been reached, I had to do with what I had ^^ Too bad for the reading, but I managed to do without it. I hope I'll have more time to do research for my next project...