-
Notifications
You must be signed in to change notification settings - Fork 0
/
DA_COM.cpp
158 lines (130 loc) · 3.7 KB
/
DA_COM.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
/*
Copyright (c) 2019 Friedl Jakob
This project may just be used within the >DIPLOMARBEIT VERONIKA< at HTBLA-Wels and with agreement of the author
*/
#include "DA_COM.h"
DA::SerialPort::SerialPort(const char *portName) {
this->openPort(portName);
}
DA::SerialPort::SerialPort() {}
DA::SerialPort::~SerialPort()
{
if (this->connected) {
this->connected = false;
CloseHandle(this->handler);
}
}
void DA::SerialPort::openPort(const char *portName) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 12);
this->connected = false;
this->handler = CreateFileA(static_cast<LPCSTR>(portName),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (this->handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
printf("<COM>\tERROR:\tHandle was not attached. Reason: %s not available\n", portName);
}
else
{
printf("<COM>\tERROR:\tunkown error!\n");
}
}
else {
DCB dcbSerialParameters = { 0 };
if (!GetCommState(this->handler, &dcbSerialParameters)) {
printf("<COM>\tERROR:\tfailed to get current serial parameters\n");
}
else {
dcbSerialParameters.BaudRate = CBR_128000;
dcbSerialParameters.ByteSize = 8;
dcbSerialParameters.StopBits = ONESTOPBIT;
dcbSerialParameters.Parity = NOPARITY;
dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &dcbSerialParameters))
{
printf("<COM>\tALERT:\tcould not set Serial port parameters\n");
}
else {
this->connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
}
}
}
SetConsoleTextAttribute(hConsole, 15);
}
void DA::SerialPort::closePort() {
if (this->connected) {
this->connected = false;
CloseHandle(this->handler);
}
}
unsigned int DA::SerialPort::readSerialPort(unsigned char *buffer, unsigned int buf_size)
{
DWORD bytesRead;
unsigned int toRead = 0;
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue > 0) {
if (this->status.cbInQue > buf_size) {
toRead = buf_size;
}
else toRead = this->status.cbInQue;
}
else return 0;
if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL)) return bytesRead;
return 0;
}
bool DA::SerialPort::writeSerialPort(const char *buffer, unsigned int buf_size)
{
DWORD bytesSend;
if (!WriteFile(this->handler, (void*)buffer, buf_size, &bytesSend, 0)) {
ClearCommError(this->handler, &this->errors, &this->status);
return false;
}
else return true;
}
bool DA::SerialPort::isConnected()
{
return this->connected;
}
bool DA::SerialPort::available(unsigned int n)
{
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue >= n)
{
return true;
}
else return false;
}
unsigned int DA::SerialPort::clearRest() {
ClearCommError(this->handler, &this->errors, &this->status);
unsigned char *rest = new unsigned char[this->status.cbInQue];
SerialPort::readSerialPort(rest, this->status.cbInQue);
delete[] rest;
return this->status.cbInQue;
}
unsigned int DA::SerialPort::available() {
ClearCommError(this->handler, &this->errors, &this->status);
return this->status.cbInQue;
}
void DA::SerialPort::printOutput() {
if (this->available()) {
printf_s("Serial OUT:\n");
while (this->available()) {
for (unsigned int i = 0; i < 10; i++) {
if (this->available()) {
unsigned char *outByte = new unsigned char[1];
this->readSerialPort(outByte, 1);
printf_s("%hx\t", (unsigned int)outByte);
delete[] outByte;
}
else break;
}
printf_s("\n");
}
}
}