Files
usbserial-dw/main.cpp
2021-08-18 06:24:49 +02:00

77 lines
2.0 KiB
C++
Executable File

/*
* File: main.cpp
* Author: fede.tft
*
* Created on September 10, 2009, 10:50 AM
*/
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include "TimeoutSerial.h"
using namespace std;
using namespace boost;
typedef vector< string > split_vector_type;
int main(int argc, char* argv[])
{
string line;
float temperature = NAN;
float humidity= NAN;
float pressure = NAN;
try {
TimeoutSerial serial("/dev/ttyUSB0",115200);
serial.setTimeout(posix_time::seconds(10));
serial.setDTR(false);
serial.setRTS(false);
sleep(5);
//Text test
serial.writeString("?\r\n");
line = serial.readStringUntil("\n");
trim(line);
cout << line << endl;
serial.writeString("measure\r\n");
for (;;) {
line = serial.readStringUntil("\n");
trim(line);
if (line == "") continue;
// cout << line << endl;
split_vector_type SplitVec; // #2: Search for tokens
split( SplitVec, line, is_any_of(" "), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
for (vector<string>::iterator t=SplitVec.begin(); t!=SplitVec.end(); ++t) {
split_vector_type kv;
split(kv, *t, is_any_of("="), token_compress_on);
// cout << "kv[0]=" << kv[0] << endl;
if (kv[0] == "temperature") temperature = stof(kv[1]);
if (kv[0] == "humidity") humidity = stof(kv[1]);
if (kv[0] == "pressure") pressure = stof(kv[1]);
}
if (pressure > 0.0) break;
}
cout << "teplota = " << temperature << endl;
cout << "vlhkost = " << humidity << endl;
cout << "tlak = " << pressure << endl;
serial.close();
} catch(boost::system::system_error& e)
{
cout<<"Error: "<<e.what()<<endl;
return 1;
}
}