-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
75 lines (56 loc) · 1.78 KB
/
test.cpp
File metadata and controls
75 lines (56 loc) · 1.78 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
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
#include "serial.hpp"
#include <string>
#include <iostream>
const std::string PORT = "COM3";
const int BAUD_RATE = 9600;
// standart use test
int test1(SerialPort& port) {
std::cout << "Opening port (" << PORT << ")...\n";
if (!port.open()) {
// print error message:
std::cerr << "Couldn't connect to port " << PORT << "\n";
return -1;
}
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << "Writing \"Hello\" to port\n";
port.writeString("Hello World");
std::cout << "Waiting for full answer to return...\n";
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << "Reading in Answer...\n";
std::string answer = port.readString(strlen("Hello World"));
std::cout << "Printing Answer...\n";
std::cout << answer << "\n";
std::cout << "Closing Port...\n";
port.close();
std::cout << "Done with Test1.\n";
return 0;
}
// opening and closing the port multiple times
int test2() {
SerialPort port(PORT, BAUD_RATE);
for (int i = 0; i < 4; i++) {
std::cout << "Opening Port in loop " << i << "...\n";
if (!port.open()) {
std::cerr << "Failed to open Port in loop " << i << "\n";
return -1;
}
std::cout << "Closing Port in loop " << i << "...\n";
port.close();
// little sleep timer to not damage hardwares
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::cout << "Done with Test2.\n";
return 0;
}
static SerialPort* pPort = nullptr;
void closepPort() {
if (pPort != nullptr)
pPort->close();
}
int main(int argc, char** argv) {
SerialPort port(PORT, BAUD_RATE);
pPort = &port;
std::atexit(closepPort);
test1(port);
//test2();
}