-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialComm Windows.cpp
More file actions
61 lines (50 loc) · 1.08 KB
/
serialComm Windows.cpp
File metadata and controls
61 lines (50 loc) · 1.08 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
#include "serial.h"
int main()
{
Serial port;
// Open port.
if (port.Open() == -1)
{
std::cerr << "Failed to open port." << std::endl;
return 1;
}
// std::vector<std::string> ports = port.listAvailablePorts();
// for (int i = 0; i < 10; i++)
// {
// std::cout << ports[i] << std::endl;
// }
char msg[255];
// std::vector<std::string> vec = getAvailablePorts();
SelectComPort();
// for (const auto& str : vec) {
// std::cout << str << std::endl;
//}
std::cout << "Connection established!\n(ON/OFF/q)? ";
std::cin >> msg;
while (strcmp(msg, "q"))
{
if (!port.Write(msg, sizeof(msg)))
{
std::cerr << "Error writing to port." << std::endl;
return 1;
}
Sleep(1);
char readBuf[1024];
bool success;
int readBytes;
Sleep(3000);
strcpy(readBuf, port.Read(success));
if (!success)
{
std::cerr << "Error reading from port." << std::endl;
return (1);
}
std::cout << "Read "
<< " bytes. Received message: " << readBuf
<< std::endl;
std::cout << "(ON/OFF/q)? ";
std::cin >> msg;
}
std::cout << "Bye!" << std::endl;
return 0;
}