-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_comm.cpp
200 lines (175 loc) · 5.16 KB
/
serial_comm.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <windows.h>
#include <ios>
#include "serial_comm.h"
// Constructor just stores value of COM port to use.
SerialComm::SerialComm(
LPCSTR ComPort /* String with ComPort to use. */
) : ComPort(ComPort)
{
}
// Set port to connect to
void SerialComm::SetComPort(
LPCSTR ComPort /* String with ComPort to use. */
)
{
ComPort = ComPort;
}
/* GETTERS */
// Get serial port connected to
LPCSTR SerialComm::GetComPort()
{
return ComPort;
}
// // Get the connection initialised flag
BOOL SerialComm::GetConnectionInitialised()
{
return bConnectionInitialised;
}
// Get handle to serial connection.
HANDLE SerialComm::GetHandle()
{
if (!GetConnectionInitialised())
{
// Connection handle holds is not initialised
printf("Handle points to uninitialised connection");
}
return hSerial;
}
// Get DCB settings
DCB SerialComm::GetDCBSerialParams()
{
return dcbSerialParams;
}
// Get timeout params
COMMTIMEOUTS SerialComm::GetTimeouts()
{
return timeouts;
}
/* OPEN AND CLOSE CONNECTIONS */
bool SerialComm::OpenSerialConnection()
{
hSerial = CreateFile(
ComPort,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
// Serial port doesn't exist. Inform user.
std::cout << "Serial port " << ComPort << " does not exist.\n";
return 1;
}
// Some other error occurred.
printf("Some error occurred when initialising port.\n");
return FALSE;
}
// Raise connection initialised flag
bConnectionInitialised = TRUE;
return TRUE;
}
bool SerialComm::CloseSerialConnection()
{
if (!CloseHandle(hSerial))
{
// Error in closing the serial connection
std::cerr << "Error closing the serial connection.\n";
return FALSE;
};
// Lower connection initialised flag
bConnectionInitialised = FALSE;
return TRUE;
}
/* CONFIGURE SERIAL CONNECTION */
// Set timeout periods in ms.
bool SerialComm::SetTimeoutPeriods(
int ReadIntervalTimeout, /* Maximum time between read chars. */
int ReadTotalTimeoutConstant, /* Constant in ms. */
int ReadTotalTimeoutMultiplier, /* Multiplier of characters. */
int WriteTotalTimeoutConstant, /* Constant in ms. */
int WriteTotalTimeoutMultiplier /* Multiplier of characters. */
)
{
timeouts.ReadIntervalTimeout = ReadIntervalTimeout;
timeouts.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant;
timeouts.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier;
timeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
timeouts.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier;
if (!bConnectionInitialised)
{
// Connection has not been initialised
printf("Serial connection not initialised.\n");
return FALSE;
}
if (!SetCommTimeouts(hSerial, &timeouts))
{
// Error occurred setting timeouts
printf("Error setting timeouts.\n");
return FALSE;
}
return TRUE;
}
// Initialise DCB struct to store config of serial connection
bool SerialComm::SetSerialConnectionConfig(int BaudRate, int ByteSize, int StopBits, int Parity)
{
if (!bConnectionInitialised)
{
// Connection has not been initialised
printf("Serial connection not initialised.\n");
return FALSE;
}
if (!GetCommState(hSerial, &dcbSerialParams))
{
// Error getting state
printf("Error getting state from serial handle.\n");
return FALSE;
}
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
// Set params to RP Pico default
dcbSerialParams.BaudRate = BaudRate;
dcbSerialParams.ByteSize = ByteSize;
dcbSerialParams.StopBits = StopBits;
dcbSerialParams.Parity = Parity;
// Set config of serial connection
if (!SetCommState(hSerial, &dcbSerialParams))
{
// Error setting serial port state
printf("Error setting serial port state\n");
return FALSE;
}
return TRUE;
}
/* READ AND WRITE FROM PORT */
bool SerialComm::ReadBytes(
LPVOID bpReadBuffer, /* Pointer to buffer to store the bytes read from the port. */
DWORD dwLengthBuffer, /* Length of the buffer. */
LPDWORD dwpBytesRead /* Pointer to int that stores the number of bytes read. */
)
{
if (!ReadFile(hSerial, bpReadBuffer, dwLengthBuffer, dwpBytesRead, NULL))
{
// Error occurred while reading the buffer.
std::cerr << "Error occurred while reading buffer.\n";
return FALSE;
}
return TRUE;
}
bool SerialComm::WriteBytes(
LPCVOID bpData, /* Pointer to the array of bytes to write */
DWORD dwSizeData, /* Size of data to write */
LPDWORD dwpBytesWritten /* Pointer to buffer storing num bytes written */
)
{
if (!WriteFile(hSerial, bpData, dwSizeData, dwpBytesWritten, NULL))
{
// Error ocurred while writing the buffer.
std::cerr << "Error occurred while writing to the buffer.\n";
return FALSE;
}
return TRUE;
}