// 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'; } }