blob: c6ec00af091d308542515bc90b8dccef43166ad2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
// Standard C++
#include <array>
#include <iomanip>
#include <iostream>
#include <string>
#include <thread>
// Asio
#include <asio.hpp>
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("<GETSERIAL>>");
jthread thread([&context, &port]() {
array<char, 1000> buffer{};
function<void()> 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<unsigned char>(buffer.at(i));
if ( isprint(chr) ) {
cout << static_cast<char>(chr);
}
else {
cout << '<' << hex << setw(2) << setfill('0') << static_cast<unsigned>(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("<GETSERIAL>>");
}
if ( line == "ver?" ) {
send("<GETVER>>");
}
if ( line == "date?" ) {
send("<GETDATETIME>>");
}
if ( line == "hb!" ) {
send("<HEARTBEAT1>>");
}
if ( line == "quit" ) {
break;
}
}
port.cancel();
context.stop();
port.close();
}
catch ( asio::system_error& e ) {
cerr << e.what() << '\n';
}
}
|