-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechoSerial.cpp
More file actions
49 lines (38 loc) · 1.24 KB
/
echoSerial.cpp
File metadata and controls
49 lines (38 loc) · 1.24 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unistd.h>
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <message>" << std::endl;
return 1;
}
// Combine all command-line arguments into a single message string
std::string message;
for (int i = 1; i < argc; ++i) {
message += argv[i];
if (i < argc - 1) {
message += " ";
}
}
std::fstream write_stream("/dev/ttyVSEND", std::ios::out);
std::fstream read_stream("/dev/ttyVREAD", std::ios::in);
if (!write_stream.is_open() || !read_stream.is_open()) {
std::cerr << "Error: Could not open virtual serial ports." << std::endl;
return 1;
}
// std::endl adds a newline character ('\n') AND flushes the stream.
write_stream << message << std::endl;
std::cout << "Sent: " << message << std::endl;
sleep(1);
std::string received_message;
if (std::getline(read_stream, received_message)) {
std::cout << "Received: " << received_message << std::endl;
} else {
std::cerr << "Error reading from stream." << std::endl;
}
write_stream.close();
read_stream.close();
return 0;
}