-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
164 lines (138 loc) · 4.41 KB
/
main.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include "SerialPort.hpp"
#include <stdio.h>
// #include <string.h>
#include <cstring>
#include <chrono>
#include <string>//字符串操作需要
#include <conio.h>//_kbhit()需要
using namespace std;
char* portName = "\\\\.\\COM7";
string arduino_port = "\\\\.\\COM7";
#define MAX_DATA_LENGTH 255
char incomingData[MAX_DATA_LENGTH];
//Control signals for turning on and turning off the led
//Check arduino code
char ledON[] = "ON\n";
char ledOFF[] = "OFF\n";
//Arduino SerialPort object
SerialPort *arduino;
//Blinking Delay
const unsigned int BLINKING_DELAY = 1000;
//If you want to send data then define "SEND" else comment it out
#define SEND
// void exampleReceiveData(void)
// {
// int readResult = arduino->readSerialPort(incomingData, MAX_DATA_LENGTH);
// printf("%s", incomingData);
// Sleep(10);
// }
// void exampleWriteData(unsigned int delayTime)
// {
// arduino->writeSerialPort(ledON, MAX_DATA_LENGTH);
// Sleep(delayTime);
// arduino->writeSerialPort(ledOFF, MAX_DATA_LENGTH);
// Sleep(delayTime);
// }
// void autoConnect(void)
// {
// //better than recusion
// //avoid stack overflows
// while(1) {
// // ui - searching
// std::cout << "Searching in progress";
// // wait connection
// while (!arduino->isConnected()) {
// Sleep(100);
// std::cout << ".";
// // arduino = new SerialPort(portName);
// }
// //Checking if arduino is connected or not
// if (arduino->isConnected()) {
// std::cout << std::endl << "Connection established at port " << portName << std::endl;
// }
// #ifdef SEND
// while(arduino->isConnected()) exampleWriteData(BLINKING_DELAY);
// #else // SEND
// while(arduino->isConnected()) exampleReceiveData();
// #endif // SEND
// }
// }
bool key_check()
{
while(1)
{
if (_kbhit())//非阻塞获取用户输入
{
char cTake = _getche();//获取输入字符,并回显
if (cTake == 'a' || cTake == 'A')
{
return true;
cout << "button \" q \" pressed " << endl;
}
}
}
return false;
}
int main()
{
// arduino = new SerialPort(portName);
SerialPort keyboard_test(portName);
if(keyboard_test.isConnected())
{
cout << "串口连接成功" << endl;
}
else
{
cout << "串口连接失败" << endl;
return 0;
}
// keyboard_test.writeSerialPort("1", MAX_DATA_LENGTH);
// keyboard_test.readSerialPort(incomingData,MAX_DATA_LENGTH);
// cout << incomingData << endl;
// while (1)
// {
// keyboard_test.writeSerialPort("1", MAX_DATA_LENGTH);
// Sleep(1000);
// keyboard_test.writeSerialPort("0", MAX_DATA_LENGTH);
// Sleep(1000);
// }
double duration1[10], duration2[10];
chrono::_V2::system_clock::time_point start_time, end_time;
for(int i = 0; i < 10; i++)
{
cout << "------ Test1 " << i+1 << " start. ------" << endl;
start_time = std::chrono::system_clock::now();
keyboard_test.writeSerialPort("1", 16);
key_check();
end_time = chrono::system_clock::now();
auto delay = chrono::duration_cast<chrono::microseconds>(end_time - start_time);
duration1[i] = double(delay.count()) * chrono::microseconds::period::num / chrono::microseconds::period::den;
cout << "------ Test1 " << i+1 << " finished, " << 10-i-1 << " remains ------" << endl;
cout << "Delay = " << duration1[i] << endl;
Sleep(1000);
}
for(int i = 0; i < 10; i++)
{
cout << "------ Test2 " << i+1 << " start. ------" << endl;
keyboard_test.writeSerialPort("1", 16);
start_time = std::chrono::system_clock::now();
key_check();
end_time = chrono::system_clock::now();
auto delay = chrono::duration_cast<chrono::microseconds>(end_time - start_time);
duration2[i] = double(delay.count()) * chrono::microseconds::period::num / chrono::microseconds::period::den;
cout << "------ Test2 " << i+1 << " finished, " << 10-i-1 << " remains ------" << endl;
cout << "Delay = " << duration2[i] << endl;
Sleep(1000);
}
double sum = 0;
for(int i = 0; i < 10; i++)
{
sum += duration1[i];
sum += duration2[i];
}
cout << "Delay of the keyboard is " << sum/20 << endl;
system("pause");
return 0;
// autoConnect();
}