diff options
| author | Thomas Schmucker <ts@its1.de> | 2026-07-25 15:22:10 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2026-07-25 15:22:10 +0200 |
| commit | 85929231fae3ae797f3a34f6b5d928d2341fc1e1 (patch) | |
| tree | 6da38f31e5e023c3caa4f4b9b5c7b417f7248df4 /src | |
| download | gmc-300e-monitor-85929231fae3ae797f3a34f6b5d928d2341fc1e1.tar.gz gmc-300e-monitor-85929231fae3ae797f3a34f6b5d928d2341fc1e1.tar.bz2 gmc-300e-monitor-85929231fae3ae797f3a34f6b5d928d2341fc1e1.zip | |
erster import
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
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 @@ | |||
| 1 | // Standard C++ | ||
| 2 | #include <array> | ||
| 3 | #include <iomanip> | ||
| 4 | #include <iostream> | ||
| 5 | #include <string> | ||
| 6 | #include <thread> | ||
| 7 | |||
| 8 | // Asio | ||
| 9 | #include <asio.hpp> | ||
| 10 | |||
| 11 | using namespace std; | ||
| 12 | |||
| 13 | int | ||
| 14 | main() | ||
| 15 | { | ||
| 16 | asio::io_context context; | ||
| 17 | |||
| 18 | try { | ||
| 19 | const auto baudrate = 57600; | ||
| 20 | const auto databits = 8; | ||
| 21 | |||
| 22 | asio::serial_port port(context); | ||
| 23 | |||
| 24 | auto send = [&](string_view cmd) { | ||
| 25 | asio::write(port, asio::buffer(cmd)); | ||
| 26 | }; | ||
| 27 | |||
| 28 | port.open("/dev/cuaU0"); | ||
| 29 | if ( !port.is_open() ) { | ||
| 30 | cerr << "Kann Port nicht öffnen...\n"; | ||
| 31 | return EXIT_FAILURE; | ||
| 32 | } | ||
| 33 | |||
| 34 | port.set_option(asio::serial_port::baud_rate(baudrate)); | ||
| 35 | port.set_option(asio::serial_port::parity(asio::serial_port::parity::none)); | ||
| 36 | port.set_option(asio::serial_port::character_size(databits)); | ||
| 37 | port.set_option(asio::serial_port::stop_bits(asio::serial_port::stop_bits::one)); | ||
| 38 | port.set_option(asio::serial_port::flow_control(asio::serial_port::flow_control::none)); | ||
| 39 | |||
| 40 | send("<GETSERIAL>>"); | ||
| 41 | |||
| 42 | jthread thread([&context, &port]() { | ||
| 43 | array<char, 1000> buffer{}; | ||
| 44 | |||
| 45 | function<void()> readFromPort = [&]() { | ||
| 46 | port.async_read_some(asio::buffer(buffer, buffer.size()), [&](std::error_code error, size_t length) { | ||
| 47 | if ( !error ) { | ||
| 48 | cout << "Daten: " << length << ": "; | ||
| 49 | for ( size_t i = 0; i != length; ++i ) { | ||
| 50 | auto chr = static_cast<unsigned char>(buffer.at(i)); | ||
| 51 | |||
| 52 | if ( isprint(chr) ) { | ||
| 53 | cout << static_cast<char>(chr); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | cout << '<' << hex << setw(2) << setfill('0') << static_cast<unsigned>(chr) << dec << '>'; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | cout << '\n'; | ||
| 60 | |||
| 61 | readFromPort(); | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | if ( error != asio::error::operation_not_supported ) { | ||
| 65 | cerr << "Fehler: " << error.message() << '\n'; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | }); | ||
| 69 | }; | ||
| 70 | |||
| 71 | readFromPort(); | ||
| 72 | context.run(); | ||
| 73 | }); | ||
| 74 | |||
| 75 | for ( string line; getline(cin, line); ) { | ||
| 76 | if ( line == "ser?" ) { | ||
| 77 | send("<GETSERIAL>>"); | ||
| 78 | } | ||
| 79 | if ( line == "ver?" ) { | ||
| 80 | send("<GETVER>>"); | ||
| 81 | } | ||
| 82 | if ( line == "date?" ) { | ||
| 83 | send("<GETDATETIME>>"); | ||
| 84 | } | ||
| 85 | if ( line == "hb!" ) { | ||
| 86 | send("<HEARTBEAT1>>"); | ||
| 87 | } | ||
| 88 | |||
| 89 | if ( line == "quit" ) { | ||
| 90 | break; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | port.cancel(); | ||
| 95 | context.stop(); | ||
| 96 | port.close(); | ||
| 97 | } | ||
| 98 | catch ( asio::system_error& e ) { | ||
| 99 | cerr << e.what() << '\n'; | ||
| 100 | } | ||
| 101 | } | ||
