-
Notifications
You must be signed in to change notification settings - Fork 22
/
SerialPort.cpp
187 lines (163 loc) · 3.6 KB
/
SerialPort.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
// SerialPort.cpp: implementation of the CSerialPort class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SerialPort.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSerialPort::CSerialPort()
{
m_hCom = INVALID_HANDLE_VALUE;
m_sError.Empty();
m_dwError = 0;
}
CSerialPort::~CSerialPort()
{
}
HANDLE CSerialPort::OpenPort(LPCTSTR sPort,DWORD dwBaudRate,BYTE byDataBits,
BYTE byParity,BYTE byStopBits,BOOL bSynchronization)
{
bool bFlagTry = FALSE; //指示是否正常退出了try块
TCHAR szPort[10] = {0};
_tcscpy(szPort,_T("\\\\.\\"));
_tcscat(szPort,sPort);
DWORD dwFlag = 0;
if(bSynchronization)
{
dwFlag = 0;
}
else
{
dwFlag = FILE_FLAG_OVERLAPPED;
}
if(m_hCom!=INVALID_HANDLE_VALUE)
{
::CloseHandle(m_hCom);
m_hCom=INVALID_HANDLE_VALUE;
}
__try
{
m_hCom = CreateFile(szPort,
GENERIC_READ|GENERIC_WRITE,
0, //共享模式
NULL,
OPEN_EXISTING,
dwFlag,
NULL);
if(m_hCom==INVALID_HANDLE_VALUE)
{
__leave;
}
if(SetupComm(m_hCom,2048,1024)==0)
{
__leave;
}
if(PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR
| PURGE_RXCLEAR )==0)
{
__leave;
}
DCB dcb; //定义dcb对象
if(!GetCommState(m_hCom,&dcb))
{
__leave;
}
COMMTIMEOUTS ct;
GetCommTimeouts(m_hCom, &ct);
ct.ReadIntervalTimeout = 500;
ct.ReadTotalTimeoutConstant = 1000;
ct.ReadTotalTimeoutMultiplier = 1000;
ct.WriteTotalTimeoutConstant = 0;
ct.WriteTotalTimeoutMultiplier = 0;
if(!SetCommTimeouts(m_hCom, &ct))
{
__leave;
}
dcb.Parity = byParity; //获得校验方式
if(byParity!=NOPARITY)
{
dcb.fBinary = TRUE; //允许校验
}
dcb.BaudRate = dwBaudRate; //获得通信速率
dcb.ByteSize = byDataBits; //数据位数
dcb.StopBits = byStopBits; //停止位
dcb.fOutxCtsFlow = FALSE; //流量控制
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
// 将设定的参数值用于该串口
if(!SetCommState(m_hCom,&dcb))
{
__leave;
}
bFlagTry = TRUE;
}//end try
__finally
{
// if(AbnormalTermination())
if(!bFlagTry)
{
m_dwError = GetLastError();
if(m_hCom!=INVALID_HANDLE_VALUE)
{
::CloseHandle(m_hCom);
m_hCom=INVALID_HANDLE_VALUE;
}
}
}
return m_hCom;
}
BOOL CSerialPort::ClosePort()
{
if(CloseHandle(m_hCom))
{
m_hCom = INVALID_HANDLE_VALUE;
return TRUE;
}
else
{
return FALSE;
}
}
CString CSerialPort::GetError()
{
return GetErrorDesc(m_dwError);
}
DWORD CSerialPort::GetErrorCode() const
{
return m_dwError;
}
CString CSerialPort::GetErrorDesc(DWORD dwError)
{
CString strResult;
HLOCAL hlocal = NULL; //malloc a result buffer
BOOL fOk = FormatMessage
(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwError, MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),
(PTSTR) &hlocal, 0, NULL);
if (!fOk)
{
//check if it is netError
//remember do not load the dll or module the dll loaded
HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
if (hDll != NULL)
{
fOk =FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
hDll, dwError,
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),
(PTSTR) &hlocal, 0, NULL);
FreeLibrary(hDll);
}
}
// if(!fOk)
// return GetSocketErrorDesc(dwError);
strResult.Format(_T("错误代码 : %ld\n错误消息: %s"),dwError,hlocal);
LocalFree(hlocal);
return strResult;
}