-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMyComPort.cpp
202 lines (180 loc) · 4.77 KB
/
MyComPort.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
// Thread.cpp: implementation of the CThread class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "StringGridctrl.h"
//---------------------------------------------------------------------------
#include "MyComPort.h"
#include <assert.h>
#include <ctype.h>
#include <WINSPOOL.H>
//---------------------------------------------------------------------------
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyComPort::CMyComPort(std::string PortName,DWORD dwBaudRate)
{
this->Port = PortName;
this->BaudRate = dwBaudRate;
this->InputBufferSize = 4096;
this->OutputBufferSize = 4096;
this->Parity = NOPARITY;
this->StopBits = ONESTOPBIT;
this->ByteSize = 8;
TimeOuts.ReadIntervalTimeout = 100;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 20000;
TimeOuts.WriteTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
hComm = NULL;
}
bool CMyComPort::Open()
{
if (IsConnected()) this->Close();
std::string portName = "\\\\.\\" + Port;
hComm = CreateFile(portName.c_str(),GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, 0);
if (hComm == INVALID_HANDLE_VALUE)// The Port does not opened success.
{
hComm = NULL;
return false ;
}
if (!UpdateCommPara())//set the baudrate ,parity ,stopbits go into effect
{
CloseHandle(hComm);
hComm = NULL;
return false;
}
if (!UpdateBufferSize())//set buffer sizes go into effect
{
CloseHandle(hComm);
hComm = NULL;
return false;
}
if (!UpdateTimeoutsPara())//set timeouts parameter go into effect
{
CloseHandle(hComm);
hComm = NULL;
return false;
}
return true;
}
bool CMyComPort::UpdateCommPara()
{
//if the connection hasn't set up ,return faile directly.
if (!IsConnected())return false;
DCB dcb;
assert(GetCommState(hComm ,&dcb));
dcb.BaudRate = this->BaudRate;
dcb.Parity = this->Parity;
dcb.StopBits = this->StopBits;
dcb.ByteSize = this->ByteSize;
if (!SetCommState(hComm ,&dcb))
return false;
return true;
}
bool CMyComPort::UpdateBufferSize()
{
//if the connection hasn't set up ,return faile directly.
if (!IsConnected())return false;
if (!SetupComm(hComm ,InputBufferSize ,OutputBufferSize))
return false;
return true;
}
bool CMyComPort::UpdateTimeoutsPara()
{
//if the connection hasn't set up ,return faile directly.
if (!IsConnected())return false;
if (!SetCommTimeouts(hComm, &TimeOuts))
return false;
return true;
}
bool CMyComPort::IsConnected()
{
return (NULL != hComm);
}
void CMyComPort::Close()
{
if (NULL != hComm)
{
CloseHandle(hComm);
hComm = NULL;
}
}
CMyComPort::~CMyComPort()
{
if (this->IsConnected()) this->Close();
}
int CMyComPort::Read(void * lpBufferOut, DWORD Length)
{
if (!this->IsConnected()) return -1;
DWORD lengthOut = 0;
ReadFile(hComm,lpBufferOut,Length,&lengthOut,NULL);
return lengthOut;
}
int CMyComPort::Write(const void * lpBufferIn, DWORD Length)
{
if (!this->IsConnected()) return -1;
DWORD lengthOut = 0;
WriteFile(hComm,lpBufferIn,Length,&lengthOut,NULL);
return lengthOut;
}
int CMyComPort::ClearBuffer(bool InputBuffer, bool OutBuffer)
{
if (!this->IsConnected()) return -1;
DWORD flags = 0;
if (InputBuffer) flags |= (PURGE_RXCLEAR|PURGE_RXABORT);
if (OutBuffer) flags |= (PURGE_TXCLEAR|PURGE_TXABORT);
PurgeComm(hComm ,flags);
return 0;
}
bool CMyComPort::UpdataSetCommMask()
{
if(!IsConnected()) return false;
if(!SetCommMask(hComm,EV_CTS))
return false;
return true;
}
bool CMyComPort::UpdataWaitCommEvent()
{
if(!IsConnected()) return false;
DWORD dwEvtMask;
if(!WaitCommEvent(hComm,&dwEvtMask,0)) return false;
if(dwEvtMask!=EV_CTS) return false;
return true;
}
bool CMyComPort::UpdataEscapeCommSET()
{
if(!IsConnected()) return false;
if(!EscapeCommFunction(hComm,SETRTS))
return false;
return true;
}
bool CMyComPort::UpdataEscapeCommCLR()
{
if(!IsConnected()) return false;
if(!EscapeCommFunction(hComm,CLRRTS))
return false;
return true;
}
bool CMyComPort::UpdataSetCommRetset()
{
if(!IsConnected()) return false;
if(!EscapeCommFunction(hComm,0))
return false;
return true;
}
bool CMyComPort::UpdataGetCommModemStat()
{
if(!IsConnected()) return false;
DWORD ModemStat;
GetCommModemStatus(hComm,&ModemStat);
if(ModemStat!=MS_CTS_ON)
return false ;
return true;
}