First usable version

This commit is contained in:
2021-08-26 18:48:27 +02:00
parent 1b64b4de81
commit ca0fdd3428
10 changed files with 172 additions and 1786 deletions

4
.gitignore vendored
View File

@@ -6,4 +6,6 @@ cmake_install.cmake
serial-port/
lib/
Ymodem*/
build/
build/*
*.log
*.jpg

View File

@@ -10,7 +10,7 @@ add_executable(timeout ${TEST_SRCS})
set(CMAKE_BUILD_TYPE Debug)
## Link libraries
set(BOOST_LIBS date_time system log)
set(BOOST_LIBS date_time system log program_options)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(timeout ${Boost_LIBRARIES})
find_package(Threads REQUIRED)

View File

@@ -29,6 +29,11 @@
/* Header includes -----------------------------------------------------------*/
#include "Ymodem.h"
#include <string.h>
#include <boost/log/core.hpp>
#include <boost/log/common.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/log/trivial.hpp>
/* Macro definitions ---------------------------------------------------------*/
/* Type definitions ----------------------------------------------------------*/
@@ -130,6 +135,7 @@ void Ymodem::receive()
{
case StageNone:
{
BOOST_LOG_TRIVIAL(debug) << "receiveStageNone";
receiveStageNone();
break;
@@ -137,6 +143,8 @@ void Ymodem::receive()
case StageEstablishing:
{
BOOST_LOG_TRIVIAL(debug) << "receiveStageEstablishing";
receiveStageEstablishing();
break;
@@ -144,6 +152,7 @@ void Ymodem::receive()
case StageEstablished:
{
BOOST_LOG_TRIVIAL(debug) << "receiveStageEstablished";
receiveStageEstablished();
break;
@@ -151,6 +160,7 @@ void Ymodem::receive()
case StageTransmitting:
{
BOOST_LOG_TRIVIAL(debug) << "receiveStageTransmitting";
receiveStageTransmitting();
break;
@@ -158,6 +168,7 @@ void Ymodem::receive()
case StageFinishing:
{
BOOST_LOG_TRIVIAL(debug) << "receiveStageFinishing";
receiveStageFinishing();
break;

View File

@@ -5,6 +5,7 @@
#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/log/trivial.hpp>
#include "TimeoutSerial.h"
using namespace std;
@@ -35,6 +36,11 @@ void YmodemFileReceive::setFilePath(const string &path)
filePath = path + "/";
}
void YmodemFileReceive::setDefaultFileName(const string &file)
{
defaultFileName = file;
}
void YmodemFileReceive::setSerialPort(TimeoutSerial *port)
{
serialPort = port;
@@ -50,8 +56,13 @@ bool YmodemFileReceive::startReceive()
progress = 0;
status = StatusEstablish;
io.run();
BOOST_LOG_TRIVIAL(debug) << "Statring receive";
serialPort->setTimeout(posix_time::seconds(0));
readTimer.expires_from_now(boost::posix_time::millisec(READ_TIME_OUT));
readTimer.async_wait(boost::bind(&YmodemFileReceive::readTimeOut,this,_1));
io.run();
return true;
}
@@ -75,7 +86,8 @@ Ymodem::Status YmodemFileReceive::getReceiveStatus()
void YmodemFileReceive::readTimeOut(const boost::system::error_code& e)
{
readTimer.cancel();
BOOST_LOG_TRIVIAL(debug) << "Read timeout";
receive();
@@ -89,7 +101,7 @@ void YmodemFileReceive::readTimeOut(const boost::system::error_code& e)
void YmodemFileReceive::writeTimeOut(const boost::system::error_code& e)
{
writeTimer.cancel();
serialPort->close();
//serialPort->close();
receiveStatus(status);
}
@@ -117,12 +129,19 @@ Ymodem::Code YmodemFileReceive::callback(Status status, uint8_t *buff, uint32_t
size[j] = buff[i];
}
fileName = name;
if (defaultFileName != "") {
fileName = defaultFileName;
} else {
fileName = name;
}
string file_desc(size);
string sizeStr = file_desc.substr(0,file_desc.find_first_of(' '));
fileSize = stol(sizeStr);
fileCount = 0;
BOOST_LOG_TRIVIAL(debug) << "File open: " << filePath + fileName ;
file.open(filePath + fileName, ios::out | ios::binary);
if(file.is_open() == true)

View File

@@ -20,6 +20,7 @@ public:
~YmodemFileReceive();
void setFilePath(const string &path);
void setDefaultFileName(const string &path);
void setSerialPort(TimeoutSerial *port);
TimeoutSerial *getSerialPort();
@@ -52,6 +53,7 @@ private:
Status status;
string filePath;
string fileName;
string defaultFileName = "";
uint64_t fileSize;
uint64_t fileCount;
};

File diff suppressed because it is too large Load Diff

View File

@@ -1,59 +0,0 @@
{
"configurations" :
[
{
"directories" :
[
{
"build" : ".",
"minimumCMakeVersion" :
{
"string" : "3.1"
},
"projectIndex" : 0,
"source" : ".",
"targetIndexes" :
[
0
]
}
],
"name" : "Debug",
"projects" :
[
{
"directoryIndexes" :
[
0
],
"name" : "TEST",
"targetIndexes" :
[
0
]
}
],
"targets" :
[
{
"directoryIndex" : 0,
"id" : "timeout::@6890427a1f51a3e7e1df",
"jsonFile" : "target-timeout-Debug-d7af418a1b6c2d259633.json",
"name" : "timeout",
"projectIndex" : 0
}
]
}
],
"kind" : "codemodel",
"paths" :
{
"build" : "/home/jaro/serialport-downloader/build",
"source" : "/home/jaro/serialport-downloader"
},
"version" :
{
"major" : 2,
"minor" : 0
}
}

View File

@@ -1,219 +0,0 @@
{
"artifacts" :
[
{
"path" : "timeout"
}
],
"backtrace" : 1,
"backtraceGraph" :
{
"commands" :
[
"add_executable",
"target_link_libraries",
"add_definitions"
],
"files" :
[
"CMakeLists.txt"
],
"nodes" :
[
{
"file" : 0
},
{
"command" : 0,
"file" : 0,
"line" : 9,
"parent" : 0
},
{
"command" : 1,
"file" : 0,
"line" : 15,
"parent" : 0
},
{
"command" : 2,
"file" : 0,
"line" : 8,
"parent" : 0
}
]
},
"compileGroups" :
[
{
"compileCommandFragments" :
[
{
"fragment" : "-g "
},
{
"fragment" : "-std=gnu++11"
}
],
"defines" :
[
{
"backtrace" : 2,
"define" : "BOOST_ALL_NO_LIB"
},
{
"backtrace" : 2,
"define" : "BOOST_ATOMIC_DYN_LINK"
},
{
"backtrace" : 2,
"define" : "BOOST_CHRONO_DYN_LINK"
},
{
"backtrace" : 2,
"define" : "BOOST_DATE_TIME_DYN_LINK"
},
{
"backtrace" : 2,
"define" : "BOOST_FILESYSTEM_DYN_LINK"
},
{
"backtrace" : 3,
"define" : "BOOST_LOG_DYN_LINK"
},
{
"backtrace" : 2,
"define" : "BOOST_REGEX_DYN_LINK"
},
{
"backtrace" : 2,
"define" : "BOOST_SYSTEM_DYN_LINK"
},
{
"backtrace" : 2,
"define" : "BOOST_THREAD_DYN_LINK"
}
],
"language" : "CXX",
"sourceIndexes" :
[
0,
1,
2,
3
]
}
],
"id" : "timeout::@6890427a1f51a3e7e1df",
"link" :
{
"commandFragments" :
[
{
"fragment" : "-g",
"role" : "flags"
},
{
"fragment" : "-rdynamic",
"role" : "flags"
},
{
"backtrace" : 2,
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_date_time.so.1.71.0",
"role" : "libraries"
},
{
"backtrace" : 2,
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_system.so.1.71.0",
"role" : "libraries"
},
{
"backtrace" : 2,
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_log.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "-lpthread",
"role" : "libraries"
},
{
"backtrace" : 2,
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_date_time.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_chrono.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_regex.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_thread.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "/usr/lib/x86_64-linux-gnu/libboost_atomic.so.1.71.0",
"role" : "libraries"
},
{
"fragment" : "-lpthread",
"role" : "libraries"
}
],
"language" : "CXX"
},
"name" : "timeout",
"nameOnDisk" : "timeout",
"paths" :
{
"build" : ".",
"source" : "."
},
"sourceGroups" :
[
{
"name" : "Source Files",
"sourceIndexes" :
[
0,
1,
2,
3
]
}
],
"sources" :
[
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "main.cpp",
"sourceGroupIndex" : 0
},
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "TimeoutSerial.cpp",
"sourceGroupIndex" : 0
},
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "Ymodem.cpp",
"sourceGroupIndex" : 0
},
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "YmodemFileReceive.cpp",
"sourceGroupIndex" : 0
}
],
"type" : "EXECUTABLE"
}

View File

@@ -1,22 +1,22 @@
[
{
"directory": "/home/jaro/serialport-downloader/build",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/main.cpp.o -c /home/jaro/serialport-downloader/main.cpp",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/main.cpp.o -c /home/jaro/serialport-downloader/main.cpp",
"file": "/home/jaro/serialport-downloader/main.cpp"
},
{
"directory": "/home/jaro/serialport-downloader/build",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/TimeoutSerial.cpp.o -c /home/jaro/serialport-downloader/TimeoutSerial.cpp",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/TimeoutSerial.cpp.o -c /home/jaro/serialport-downloader/TimeoutSerial.cpp",
"file": "/home/jaro/serialport-downloader/TimeoutSerial.cpp"
},
{
"directory": "/home/jaro/serialport-downloader/build",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/Ymodem.cpp.o -c /home/jaro/serialport-downloader/Ymodem.cpp",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/Ymodem.cpp.o -c /home/jaro/serialport-downloader/Ymodem.cpp",
"file": "/home/jaro/serialport-downloader/Ymodem.cpp"
},
{
"directory": "/home/jaro/serialport-downloader/build",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/YmodemFileReceive.cpp.o -c /home/jaro/serialport-downloader/YmodemFileReceive.cpp",
"command": "/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_ATOMIC_DYN_LINK -DBOOST_CHRONO_DYN_LINK -DBOOST_DATE_TIME_DYN_LINK -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_LOG_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_REGEX_DYN_LINK -DBOOST_SYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -g -std=gnu++11 -o CMakeFiles/timeout.dir/YmodemFileReceive.cpp.o -c /home/jaro/serialport-downloader/YmodemFileReceive.cpp",
"file": "/home/jaro/serialport-downloader/YmodemFileReceive.cpp"
}
]

169
main.cpp
View File

@@ -21,19 +21,38 @@
#include <boost/parameter/keyword.hpp>
#include <boost/log/detail/config.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/sources/severity_logger.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 <boost/program_options.hpp>
#include "TimeoutSerial.h"
#include "YmodemFileReceive.h"
#include <exception>
using namespace std;
using namespace boost;
namespace po = boost::program_options;
typedef vector< string > split_vector_type;
enum severity_level
{
normal,
notification,
warning,
error,
critical
};
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
bool dataHandler(unsigned long blockNo, char* buffer, int size)
{
cout << "Block No. = " << blockNo << endl;
@@ -79,10 +98,11 @@ static void init_log(void)
/* console sink */
auto consoleSink = boost::log::add_console_log(std::clog);
consoleSink->set_formatter(logFmt);
consoleSink->set_filter(severity >= warning);
/* 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::file_name = "serial-dw_%Y-%m-%d_%H-%M-%S.%N.log",
boost::log::keywords::file_name = "serial-downloader.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);
@@ -94,74 +114,132 @@ static void init_log(void)
int main(int argc, char* argv[])
{
string line;
YmodemFileReceive modem;
float temperature = NAN;
float humidity= NAN;
float pressure = NAN;
po::variables_map vm;
std::string port,path,filename;
int speed;
init_log();
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("measure", "get and print values")
("verbose", po::value<string>()->implicit_value("0"), "verbosity level")
("file", po::value<string>(&filename)->default_value("camera.jpg"),"filename of capture filename")
("path", po::value<string>(&path)->default_value("/tmp"),"directory for file of capture")
("capture", "capture photo of camera")
("port", po::value<string>(&port)->default_value("/dev/ttyUSB0"), "port to read from")
("speed", po::value<int>(&speed)->default_value(115200), "speed read from port")
("temperature", "get and print temprature")
("humidity", "get and print humidity")
("pressure", "get and print pressure")
;
try {
BOOST_LOG_TRIVIAL(debug) << "TEST";
TimeoutSerial serial("/dev/ttyUSB0",115200);
serial.setTimeout(posix_time::seconds(10));
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
} catch(std::exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
}
catch(...) {
cerr << "Exception of unknown type!\n";
return 1;
}
try {
BOOST_LOG_TRIVIAL(debug) << "Starting Program on " << port ;
TimeoutSerial serial(port,speed);
serial.setTimeout(posix_time::seconds(5));
serial.setDTR(false);
serial.setRTS(false);
boost::this_thread::sleep(posix_time::milliseconds(5000));
line = serial.readStringUntil("\n");
trim(line);
cout << line << endl;
BOOST_LOG_TRIVIAL(debug) << line;
serial.writeString("measure\r\n");
if (vm.count("measure") || vm.count("temperature") || vm.count("pressure") || vm.count("humidity")) {
serial.writeString("measure\r\n");
for (;;) {
line = serial.readStringUntil("\n");
trim(line);
if (line == "") continue;
// cout << line << endl;
for (;;) {
line = serial.readStringUntil("\n");
trim(line);
if (line == "") continue;
split_vector_type SplitVec; // #2: Search for tokens
split( SplitVec, line, is_any_of(" "), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
split_vector_type SplitVec;
split( SplitVec, line, is_any_of(" "), token_compress_on );
for (vector<string>::iterator t=SplitVec.begin(); t!=SplitVec.end(); ++t) {
split_vector_type kv;
split(kv, *t, is_any_of("="), token_compress_on);
if (kv[0] == "temperature") temperature = stof(kv[1]);
if (kv[0] == "humidity") humidity = stof(kv[1]);
if (kv[0] == "pressure") pressure = stof(kv[1]);
}
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;
}
if (pressure > 0.0) break;
if (vm.count("measure")) {
cout << "teplota = " << temperature << endl;
cout << "vlhkost = " << humidity << endl;
cout << "tlak = " << pressure << endl;
}
if (vm.count("temperature")) {
cout << temperature << endl;
}
if (vm.count("humidity")) {
cout << humidity << endl;
}
if (vm.count("pressure")) {
cout << pressure << endl;
}
return 0;
}
cout << "teplota = " << temperature << endl;
cout << "vlhkost = " << humidity << endl;
cout << "tlak = " << pressure << endl;
if (vm.count("capture")) {
serial.writeString("capture\r\n");
boost::this_thread::sleep(posix_time::milliseconds(100));
//XModem modem(&serial,dataHandler);
serial.setTimeout(posix_time::milliseconds(500));
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;
try {
for (;;) {
line = serial.readStringUntil("\r\n");
cout << line << endl;
}
} catch (...) {
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.writeString("rb\r\n");
boost::this_thread::sleep(posix_time::milliseconds(100));
serial.readChar(1);
serial.readChar(1);
modem.setSerialPort(&serial);
modem.setFilePath(path);
modem.setDefaultFileName(filename);
modem.startReceive();
boost::this_thread::sleep(posix_time::seconds(1));
serial.writeString("free\r\n");
}
serial.close();
@@ -171,4 +249,3 @@ int main(int argc, char* argv[])
return 1;
}
}