-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialCommunication.cpp
93 lines (73 loc) · 2.32 KB
/
SerialCommunication.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
#include "SerialCommunication.h"
#include <algorithm>
#include <iostream>
const std::vector<uint32_t> STANDARD_BAUD_RATES{ CBR_110, CBR_300, CBR_600, CBR_1200, CBR_2400, CBR_4800, CBR_9600, CBR_14400, CBR_19200, CBR_38400, CBR_57600, CBR_115200 };
SerialPort::SerialPort(LPCSTR port_name, DWORD baud_rate) :
port_name(port_name)
{
if (checkBaudRate(baud_rate)) {
this->baud_rate = baud_rate;
}
else {
printf("ERROR: please use one of the following standard baud rates: ");
for (auto &elem : STANDARD_BAUD_RATES) {
std::cout << elem << std::endl;
}
}
handler = CreateFileA(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handler == INVALID_HANDLE_VALUE) {
last_error = GetLastError();
if (last_error == ERROR_FILE_NOT_FOUND) {
std::cout << "ERROR: Unable to open port " << port_name << std::endl;
}
else {
std::cout << "ERROR creating port handle" << std::endl;
}
system("pause");
exit(EXIT_FAILURE);
}
DCB serialConnection;
if (!(GetCommState(handler, &serialConnection))) {
std::cout << "ERROR: couldnt get settings for specified port" << std::endl;
system("pause");
exit(EXIT_FAILURE);
}
serialConnection.BaudRate = baud_rate;
serialConnection.ByteSize = 8;
serialConnection.StopBits = ONESTOPBIT;
serialConnection.Parity = NOPARITY;
serialConnection.fDtrControl = DTR_CONTROL_ENABLE;
if (!(SetCommState(handler, &serialConnection))) {
std::cout << "ERROR: couldnt apply settings to specified port" << std::endl;
system("pause");
exit(EXIT_FAILURE);
}
else {
this->connected = true;
PurgeComm(handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(INIT_TIME_MS);
}
}
SerialPort::~SerialPort() {
CloseHandle(handler);
connected = false;
}
int SerialPort::read(uint8_t* buffer, uint8_t buffer_size) {
DWORD bytesRead = 0;
unsigned int toRead = 0;
ClearCommError(handler, &last_error, &status);
if (status.cbInQue > 0) {
if (status.cbInQue > buffer_size) {
toRead = buffer_size;
}
else toRead = status.cbInQue;
if (ReadFile(handler, buffer, toRead, &bytesRead, NULL)) return bytesRead;
}
return 0;
}
bool SerialPort::isConnected() {
return this->connected;
}
bool SerialPort::checkBaudRate(DWORD baud_rate) {
return (std::find(STANDARD_BAUD_RATES.begin(), STANDARD_BAUD_RATES.end(), baud_rate) != STANDARD_BAUD_RATES.end());
}