From 85929231fae3ae797f3a34f6b5d928d2341fc1e1 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sat, 25 Jul 2026 15:22:10 +0200 Subject: erster import --- src/app.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/app.cpp (limited to 'src') diff --git a/src/app.cpp b/src/app.cpp new file mode 100644 index 0000000..c6ec00a --- /dev/null +++ b/src/app.cpp @@ -0,0 +1,101 @@ +// Standard C++ +#include +#include +#include +#include +#include + +// Asio +#include + +using namespace std; + +int +main() +{ + asio::io_context context; + + try { + const auto baudrate = 57600; + const auto databits = 8; + + asio::serial_port port(context); + + auto send = [&](string_view cmd) { + asio::write(port, asio::buffer(cmd)); + }; + + port.open("/dev/cuaU0"); + if ( !port.is_open() ) { + cerr << "Kann Port nicht öffnen...\n"; + return EXIT_FAILURE; + } + + port.set_option(asio::serial_port::baud_rate(baudrate)); + port.set_option(asio::serial_port::parity(asio::serial_port::parity::none)); + port.set_option(asio::serial_port::character_size(databits)); + port.set_option(asio::serial_port::stop_bits(asio::serial_port::stop_bits::one)); + port.set_option(asio::serial_port::flow_control(asio::serial_port::flow_control::none)); + + send(">"); + + jthread thread([&context, &port]() { + array buffer{}; + + function readFromPort = [&]() { + port.async_read_some(asio::buffer(buffer, buffer.size()), [&](std::error_code error, size_t length) { + if ( !error ) { + cout << "Daten: " << length << ": "; + for ( size_t i = 0; i != length; ++i ) { + auto chr = static_cast(buffer.at(i)); + + if ( isprint(chr) ) { + cout << static_cast(chr); + } + else { + cout << '<' << hex << setw(2) << setfill('0') << static_cast(chr) << dec << '>'; + } + } + cout << '\n'; + + readFromPort(); + } + else { + if ( error != asio::error::operation_not_supported ) { + cerr << "Fehler: " << error.message() << '\n'; + } + } + }); + }; + + readFromPort(); + context.run(); + }); + + for ( string line; getline(cin, line); ) { + if ( line == "ser?" ) { + send(">"); + } + if ( line == "ver?" ) { + send(">"); + } + if ( line == "date?" ) { + send(">"); + } + if ( line == "hb!" ) { + send(">"); + } + + if ( line == "quit" ) { + break; + } + } + + port.cancel(); + context.stop(); + port.close(); + } + catch ( asio::system_error& e ) { + cerr << e.what() << '\n'; + } +} -- cgit v1.3