-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialPort.h
161 lines (115 loc) · 4.43 KB
/
SerialPort.h
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
#include <Windows.h>
#include <string>
#include <iostream>
#include <tchar.h>
#include <stdio.h>
#define WRITE_SLEEP_TIME 22
class SerialPort {
public:
SerialPort(const LPCSTR COMPortToSet, const bool readingToSet, const bool writingToSet) {
m_COMPortPath = COMPortToSet;
m_canRead = readingToSet;
m_canWrite = writingToSet;
DWORD desiredAccess;
if (!readingToSet || !writingToSet) {
if (readingToSet) {
desiredAccess = GENERIC_READ;
}
else {
desiredAccess = GENERIC_WRITE;
}
}
else {
desiredAccess = GENERIC_READ | GENERIC_WRITE;
}
m_hComm = CreateFile(COMPortToSet, desiredAccess, 0, NULL, OPEN_EXISTING, 0, NULL);
if (m_hComm == INVALID_HANDLE_VALUE) {
std::cout << "Error in opening serial port on " << COMPortToSet << std::endl;
}
else {
std::cout << "Successfully opened serial port on " << COMPortToSet << std::endl;
}
SecureZeroMemory(&m_dcb, sizeof(DCB));
m_dcb.DCBlength = sizeof(DCB);
bool success = GetCommState(m_hComm, &m_dcb);
if (!success) {
std::cout << "GetCommState failed with error " << GetLastError() << std::endl;
}
//PrintCommState();
m_dcb.BaudRate = CBR_57600;
m_dcb.ByteSize = 8;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.Parity = NOPARITY;
m_dcb.EvtChar = 0x04;
success = SetCommState(m_hComm, &m_dcb);
if (!success) {
std::cout << "SetCommState failed with error " << GetLastError() << std::endl;
}
PrintCommState();
m_timeouts.ReadIntervalTimeout = 50;
m_timeouts.ReadTotalTimeoutConstant = 50;
m_timeouts.ReadTotalTimeoutMultiplier = 10;
m_timeouts.WriteTotalTimeoutConstant = 50;
m_timeouts.WriteTotalTimeoutMultiplier = 10;
DWORD dwStoredFlags = EV_RXCHAR;
success = SetCommMask(m_hComm, dwStoredFlags);
if (!success) {
std::cout << "SetCommMask failed with error " << GetLastError() << std::endl;
}
}
bool Close() {
return CloseHandle(m_hComm);
}
bool WriteBytes(const unsigned char *bytesToWrite, const unsigned int size) {
DWORD numOfBytesToWrite = size;
DWORD numOfBytesWritten = 0;
return WriteFile(m_hComm, bytesToWrite, numOfBytesToWrite, &numOfBytesWritten, NULL);
}
bool ReadTheLonelyByte(unsigned int &p_buffer) {
bool success = false;
DWORD dwCommEvent;
WaitCommEvent(m_hComm, &dwCommEvent, NULL);
DWORD noBytesRead;
success = ReadFile(m_hComm,
&p_buffer,
1,
&noBytesRead,
NULL);
if (!success) { return false; }
else { return true; }
}
bool ReadBytes(unsigned int * buffer, const unsigned char bytesToRead) {
bool success = false;
DWORD dwCommEvent;
WaitCommEvent(m_hComm, &dwCommEvent, NULL);
char tempChar;
DWORD noBytesRead;
int i = 0;
do {
success = ReadFile(m_hComm,
&tempChar,
sizeof(tempChar),
&noBytesRead,
NULL);
if (!success) { return false; }
buffer[i] = tempChar;
i++;
} while (i < bytesToRead);
return true;
}
void PrintCommState() {
_tprintf(TEXT("\nBaudRate = %d\nByteSize = %d\nParity = %d\nStopBits = %d\nEventChar = %d\n\n"),
m_dcb.BaudRate,
m_dcb.ByteSize,
m_dcb.Parity,
m_dcb.StopBits,
m_dcb.EvtChar);
}
private:
HANDLE m_hComm;
LPCSTR m_COMPortPath;
DCB m_dcb;
COMMTIMEOUTS m_timeouts;
bool m_canRead;
bool m_canWrite;
};