changed serial reading from lowlevel to boost with hope for more stability, changed a lot of smart pointers to unique_ptr, added fmt for logging
This commit is contained in:
53
serial_port.cpp
Normal file
53
serial_port.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "serial_port.h"
|
||||
|
||||
serial_port::serial_port(const std::string & device) {
|
||||
try {
|
||||
this->port.open(device);
|
||||
this->port.set_option(boost::asio::serial_port::baud_rate(9600));
|
||||
this->port.set_option(boost::asio::serial_port::parity(
|
||||
boost::asio::serial_port::parity::none));
|
||||
this->port.set_option(boost::asio::serial_port::character_size(
|
||||
boost::asio::serial_port::character_size(8)));
|
||||
this->port.set_option(boost::asio::serial_port::stop_bits(
|
||||
boost::asio::serial_port::stop_bits::one));
|
||||
this->flush();
|
||||
} catch (const boost::system::system_error & e) {
|
||||
throw std::system_error{
|
||||
e.code(), std::string("Could not open serial port ") + device};
|
||||
}
|
||||
std::cout << "Port opened" << std::endl;
|
||||
}
|
||||
|
||||
void serial_port::flush() {
|
||||
auto handle = this->port.native_handle();
|
||||
if (tcflush(handle, TCIOFLUSH) == -1) {
|
||||
throw std::system_error(std::error_code(errno, std::system_category()));
|
||||
}
|
||||
}
|
||||
|
||||
TransCheck serial_port::read_frame() {
|
||||
|
||||
// buffer.reserve(1);
|
||||
TransCheck buffer;
|
||||
boost::asio::read(port, boost::asio::buffer(&buffer, sizeof(buffer)));
|
||||
/*
|
||||
async_read(
|
||||
port, boost::asio::buffer(&buffer, sizeof(buffer)),
|
||||
[&buffer, this](auto error, auto) { std::cout << error << std::endl; });
|
||||
*/
|
||||
// this->ioContext.run();
|
||||
|
||||
// check for valid data
|
||||
if (buffer.begin == '#' && buffer.end == '!') {
|
||||
std::cout << "vaild data" << std::endl;
|
||||
return buffer;
|
||||
}
|
||||
else {
|
||||
std::cout << "Fehler: \n" << std::endl;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
buffer.data[i].update = 0;
|
||||
}
|
||||
this->flush();
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user