Add Ymodem transmit translated to boosts

This commit is contained in:
2021-08-24 16:21:38 +02:00
parent ac26fc6bdd
commit 2b2cbe655d
16 changed files with 1077 additions and 1806 deletions

View File

@@ -6,10 +6,28 @@
*/
#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"
#include "XModem.h"
using namespace std;
using namespace boost;
@@ -27,6 +45,52 @@ bool dataHandler(unsigned long blockNo, char* buffer, int size)
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;
@@ -34,17 +98,16 @@ int main(int argc, char* argv[])
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);
sleep(5);
//Text test
serial.writeString("?\r\n");
boost::this_thread::sleep(posix_time::milliseconds(5000));
line = serial.readStringUntil("\n");
trim(line);
@@ -77,10 +140,10 @@ int main(int argc, char* argv[])
cout << "vlhkost = " << humidity << endl;
cout << "tlak = " << pressure << endl;
XModem modem(&serial,dataHandler);
//XModem modem(&serial,dataHandler);
serial.writeString("capture\r\n");
sleep(1);
boost::this_thread::sleep(posix_time::milliseconds(100));
line = serial.readStringUntil("\r\n");
cout << line << endl;
line = serial.readStringUntil("\r\n");
@@ -93,11 +156,11 @@ int main(int argc, char* argv[])
// cout << line << endl;
serial.writeString("rb\r\n");
sleep(1);
boost::this_thread::sleep(posix_time::milliseconds(100));
serial.readChar(1);
serial.readChar(1);
modem.receive();
sleep(1);
//modem.receive();
boost::this_thread::sleep(posix_time::milliseconds(100));
serial.writeString("free\r\n");
serial.close();