175 lines
5.6 KiB
C++
Executable File
175 lines
5.6 KiB
C++
Executable File
/*
|
|
* File: main.cpp
|
|
* Author: fede.tft
|
|
*
|
|
* Created on September 10, 2009, 10:50 AM
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <boost/log/sinks/debug_output_backend.hpp>
|
|
#include <boost/log/utility/setup/common_attributes.hpp>
|
|
#include <boost/log/sinks/event_log_backend.hpp>
|
|
#include <boost/thread/future.hpp>
|
|
#include <boost/log/attributes.hpp>
|
|
#include <boost/log/core.hpp>
|
|
#include <boost/log/common.hpp>
|
|
#include <boost/log/sinks.hpp>
|
|
#include <boost/log/utility/setup/console.hpp>
|
|
#include <boost/log/utility/setup.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <boost/log/expressions.hpp>
|
|
#include <boost/parameter/keyword.hpp>
|
|
#include <boost/log/detail/config.hpp>
|
|
#include <boost/log/support/date_time.hpp>
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
#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;
|
|
|
|
bool dataHandler(unsigned long blockNo, char* buffer, int size)
|
|
{
|
|
cout << "Block No. = " << blockNo << endl;
|
|
for (int i=0 ; i<size ; i++) {
|
|
cout << char(buffer[i]) << " ";
|
|
}
|
|
cout << endl;
|
|
|
|
return true;
|
|
}
|
|
|
|
static void init_log(void)
|
|
{
|
|
/* init boost log
|
|
* 1. Add common attributes
|
|
* 2. set log filter to trace
|
|
*/
|
|
boost::log::add_common_attributes();
|
|
boost::log::core::get()->add_global_attribute("Scope",
|
|
boost::log::attributes::named_scope());
|
|
boost::log::core::get()->set_filter(
|
|
boost::log::trivial::severity >= boost::log::trivial::trace
|
|
);
|
|
|
|
/* log formatter:
|
|
* [TimeStamp] [ThreadId] [Severity Level] [Scope] Log message
|
|
*/
|
|
auto fmtTimeStamp = boost::log::expressions::
|
|
format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f");
|
|
auto fmtThreadId = boost::log::expressions::
|
|
attr<boost::log::attributes::current_thread_id::value_type>("ThreadID");
|
|
auto fmtSeverity = boost::log::expressions::
|
|
attr<boost::log::trivial::severity_level>("Severity");
|
|
auto fmtScope = boost::log::expressions::format_named_scope("Scope",
|
|
boost::log::keywords::format = "%n(%f:%l)",
|
|
boost::log::keywords::iteration = boost::log::expressions::reverse,
|
|
boost::log::keywords::depth = 2);
|
|
boost::log::formatter logFmt =
|
|
boost::log::expressions::format("[%1%] (%2%) [%3%] [%4%] %5%")
|
|
% fmtTimeStamp % fmtThreadId % fmtSeverity % fmtScope
|
|
% boost::log::expressions::smessage;
|
|
|
|
/* console sink */
|
|
auto consoleSink = boost::log::add_console_log(std::clog);
|
|
consoleSink->set_formatter(logFmt);
|
|
|
|
/* fs sink */
|
|
auto fsSink = boost::log::add_file_log(
|
|
boost::log::keywords::file_name = "serial-dw_%Y-%m-%d_%H-%M-%S.%N.log",
|
|
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
|
|
boost::log::keywords::min_free_space = 30 * 1024 * 1024,
|
|
boost::log::keywords::open_mode = std::ios_base::app);
|
|
fsSink->set_formatter(logFmt);
|
|
fsSink->locked_backend()->auto_flush(true);
|
|
}
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
string line;
|
|
float temperature = NAN;
|
|
float humidity= NAN;
|
|
float pressure = NAN;
|
|
|
|
init_log();
|
|
|
|
try {
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "TEST";
|
|
TimeoutSerial serial("/dev/ttyUSB0",115200);
|
|
serial.setTimeout(posix_time::seconds(10));
|
|
serial.setDTR(false);
|
|
serial.setRTS(false);
|
|
boost::this_thread::sleep(posix_time::milliseconds(5000));
|
|
|
|
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;
|
|
|
|
//XModem modem(&serial,dataHandler);
|
|
|
|
serial.writeString("capture\r\n");
|
|
boost::this_thread::sleep(posix_time::milliseconds(100));
|
|
line = serial.readStringUntil("\r\n");
|
|
cout << line << endl;
|
|
line = serial.readStringUntil("\r\n");
|
|
cout << line << endl;
|
|
line = serial.readStringUntil("\r\n");
|
|
cout << line << endl;
|
|
line = serial.readStringUntil("\r\n");
|
|
cout << line << endl;
|
|
// line = serial.readStringUntil("\r\n");
|
|
// cout << line << endl;
|
|
|
|
serial.writeString("rb\r\n");
|
|
boost::this_thread::sleep(posix_time::milliseconds(100));
|
|
serial.readChar(1);
|
|
serial.readChar(1);
|
|
//modem.receive();
|
|
boost::this_thread::sleep(posix_time::milliseconds(100));
|
|
serial.writeString("free\r\n");
|
|
|
|
serial.close();
|
|
|
|
} catch(boost::system::system_error& e)
|
|
{
|
|
cout<<"Error: "<<e.what()<<endl;
|
|
return 1;
|
|
}
|
|
}
|
|
|