/* * File: main.cpp * Author: fede.tft * * Created on September 10, 2009, 10:50 AM */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 ; iadd_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("TimeStamp", "%Y-%m-%d %H:%M:%S.%f"); auto fmtThreadId = boost::log::expressions:: attr("ThreadID"); auto fmtSeverity = boost::log::expressions:: attr("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::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: "<