forked from cilel/VS_positioning_Z
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncSerialComm.cpp
255 lines (210 loc) · 7.51 KB
/
SyncSerialComm.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**===================================================================
* SyncSerialComm.cpp: implementation of the CSyncSerialComm class.
* ==================================================================*/
#include <string.h>
#include <assert.h>
#include <sstream>
#include <iostream>
#include "SyncSerialComm.h"
using namespace std;
// ================= Constructor ======================
CSyncSerialComm::CSyncSerialComm(const char *pszPortName): m_hSerialComm(INVALID_HANDLE_VALUE)
{
assert(pszPortName);
m_pszPortName = new char[strlen(pszPortName)];
strcpy(m_pszPortName, pszPortName);
}
// ================= Destructor ======================
CSyncSerialComm::~CSyncSerialComm()
{
if(m_pszPortName)
delete[] m_pszPortName;
Close();
}
/**===================================================================
* Name: Open
* Return: HRESULT
* Comment: This function is used to open a connection with serial port
* Uses non-overlapped i/o, and allows for reading & writing to the port
* ==================================================================*/
HRESULT CSyncSerialComm::Open()
{
HRESULT hResult;
m_hSerialComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);
if(m_hSerialComm == INVALID_HANDLE_VALUE)
{
unsigned long error = ::GetLastError();
hResult = E_FAIL;
cout<<" Found an error: "<<error<<endl;
}
else
{
winLog<<"Serial port opened..."<<endl;
hResult = S_OK;
}
return hResult;
}
/**===================================================================
* Name: Close
* Return: HRESULT
* Comment: This function is used to close the serial port connection
* This function is called with the destructor
* ==================================================================*/
HRESULT CSyncSerialComm::Close()
{
if(m_hSerialComm != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hSerialComm);
m_hSerialComm = INVALID_HANDLE_VALUE;
}
winLog<<"Serial port closed..."<<endl;
return S_OK;
}
/**===================================================================
* Name: ConfigPort
* Parameter: dwBaudRate - This must be set to the baud rate of the
* serial port connection otherwise invalid reads occur.
* dwTimeOutInSec - Specifies the timeout for read and write of the serial
port connection in seconds
* Return: HRESULT
* Comment: This function is used to configure the serial port connection.
* ==================================================================*/
HRESULT CSyncSerialComm::ConfigPort(DWORD dwBaudRate, DWORD dwTimeOutInSec)
{
if(!SetupComm(m_hSerialComm, 1024, 1024))
return E_FAIL;
DCB dcbConfig;
if(GetCommState(m_hSerialComm, &dcbConfig)) /* Configuring Serial Port Settings */
{
dcbConfig.BaudRate = dwBaudRate;
dcbConfig.ByteSize = 8;
dcbConfig.Parity = NOPARITY;
dcbConfig.StopBits = ONESTOPBIT;
dcbConfig.fBinary = TRUE;
dcbConfig.fParity = TRUE;
winLog<<"Serial port Configure successfully..."<<endl;
}
else
return E_FAIL;
if(!SetCommState(m_hSerialComm, &dcbConfig))
return E_FAIL;
COMMTIMEOUTS commTimeout;
if(GetCommTimeouts(m_hSerialComm, &commTimeout)) /* Configuring Read & Write Time Outs */
{
commTimeout.ReadIntervalTimeout = 1000*dwTimeOutInSec;
commTimeout.ReadTotalTimeoutConstant = 1000*dwTimeOutInSec;
commTimeout.ReadTotalTimeoutMultiplier = 0;
commTimeout.WriteTotalTimeoutConstant = 1000*dwTimeOutInSec;
commTimeout.WriteTotalTimeoutMultiplier = 0;
}
else
return E_FAIL;
if(SetCommTimeouts(m_hSerialComm, &commTimeout))
return S_OK;
else
return E_FAIL;
}
/**===================================================================
* Name: Read
* Parameter: ppszBuf - The buffer that will have the value that was
* read in from the serial port.
* dwSize - The size of the buffer
* Return: HRESULT
* Comment: This function sets an event that will be signalled if the
any byte is buffered internally. Once this occurs, the function keeps
reading multiple a single byte at a time until there is no more furthur
byte to read from the input stream
* ==================================================================*/
HRESULT CSyncSerialComm::Read(char **ppszBuf, DWORD &dwSize)
{
HRESULT hResult = S_OK;
std::stringbuf sb;
DWORD dwEventMask;
if(!SetCommMask(m_hSerialComm, EV_RXCHAR)) /* Setting Event Type */
return E_FAIL;
if(WaitCommEvent(m_hSerialComm, &dwEventMask, NULL)) /* Waiting For Event to Occur */
{
char szBuf;
DWORD dwIncommingReadSize;
do
{
if(ReadFile(m_hSerialComm, &szBuf, 1, &dwIncommingReadSize, NULL) != 0)
{
if(dwIncommingReadSize > 0)
{
dwSize += dwIncommingReadSize;
sb.sputn(&szBuf, dwIncommingReadSize);
}
}
else
{
unsigned long error = ::GetLastError();
hResult = E_FAIL;
cout<<" Found an error: "<<error<<endl;
break;
}
}
while(dwIncommingReadSize > 0);
*ppszBuf = new char[dwSize];
strcpy(*ppszBuf, (sb.str()).c_str());
return hResult;
}
else
return E_FAIL;
}
/**===================================================================
* Name: Write
* Parameter: szBuf - The buffer holding the bytes to write to the serial
* port connection
* dwSize - The size of the buffer
* Return: HRESULT
* Comment: This function writes one byte at a time until all the bytes
in the buffer is sent out
* ==================================================================*/
HRESULT CSyncSerialComm::Write(const char *pszBuf, DWORD dwSize)
{
HRESULT hResult = S_OK;
assert(pszBuf);
unsigned long dwNumberOfBytesSent = 0;
// cout<<"THE DATA INSIDE IS: "<<pszBuf<<" "<<dwSize<<endl;
while(dwNumberOfBytesSent < dwSize)
{
unsigned long dwNumberOfBytesWritten;
if(WriteFile(m_hSerialComm, &pszBuf[dwNumberOfBytesSent], 1, &dwNumberOfBytesWritten, NULL) != 0)
{
if(dwNumberOfBytesWritten > 0)
++dwNumberOfBytesSent;
else
{
unsigned long error = ::GetLastError();
hResult = E_FAIL;
cout<<" Found an error: "<<error<<endl;
break;
}
}
else
{
unsigned long error = ::GetLastError();
hResult = E_FAIL;
cout<<" Found an error: "<<error<<endl;
break;
}
//cout<<"The number of bytes written: "<<dwNumberOfBytesWritten<<endl;
}
return hResult;
}
/**===================================================================
* Name: Flush
* Parameter: dwFlag - The flag specifying if the input/output buffer
to be flushed
* Return: HRESULT
* Comment: This function is flushes the specfied buffer
* Note: By default, both the input and output buffers are flushed
* ==================================================================*/
HRESULT CSyncSerialComm::Flush(DWORD dwFlag)
{
if(PurgeComm(m_hSerialComm, dwFlag))
return S_OK;
else
return E_FAIL;
}