简体   繁体   中英

Reading serial data in c++

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...)

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM