-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialMFC.h
127 lines (108 loc) · 3.93 KB
/
SerialMFC.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
// SerialMFC.h - Definition of the CSerialMFC class
//
// Copyright (C) 1999-2003 Ramon de Klein ([email protected])
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef __SERIAL_MFC_H
#define __SERIAL_MFC_H
//////////////////////////////////////////////////////////////////////
// Include CSerialWnd base class
#include "SerialWnd.h"
//////////////////////////////////////////////////////////////////////
// Make sure that MFC is already loaded
//
// We don't include it here, because MFC based applications typically
// use a precompiled header file.
#ifndef __AFXWIN_H__
#error "CWnd class isn't included (include AFXWIN.H)"
#endif
//////////////////////////////////////////////////////////////////////
//
// CSerialMFC - MFC wrapper for serial communication handling
//
// CSerialMFC is a very thing wrapper around the CSerialWnd class,
// which makes it more suitable for use in MFC-based applications.
// Unfortenately, the MFC framework isn't very flexible when it comes
// to adding custom message handlers. We cannot add our own types to
// the message handlers, but we're stuck to use 32-bit integers.
//
// Add the following entry to the window's message map to make sure
// that MFC calls your handler, when a serial message comes in.
//
// DECLARE_MESSAGE_MAP(CMyClass,CWnd)
// ...
// ON_WM_SERIAL(OnSerialMsg)
// ...
// END_MESSAGE_MAP()
//
// A typical handler could look like this:
//
// afx_msg LRESULT CMyClass::OnSerialMsg (WPARAM wParam, LPARAM lParam)
// {
// CSerial::EEvent eEvent = CSerial::EEvent(LOWORD(wParam));
// CSerial::EError eError = CSerial::EError(HIWORD(wParam));
//
// // lParam: User-defined 32-bit integer (type LPARAM)
// switch (eEvent)
// {
// case CSerialMFC::EEventRecv:
// // TODO: Read data from the port
// break;
// ...
// }
//
// // Return successful
// return 0;
// }
//
//
// Pros:
// -----
// - Easy to use
// - Fully ANSI and Unicode aware
// - Integrates seamless in MFC applications
//
// Cons:
// -----
// - No more then CSerialWnd, except that you need MFC
//
// Copyright (C) 1999-2003 Ramon de Klein
// ([email protected])
class CSerialMFC : public CSerialWnd
{
// Operations
public:
// Open the serial communications for a particular COM port. You
// need to use the full devicename (i.e. "COM1") to open the port.
virtual LONG Open (LPCTSTR lpszDevice, CWnd* pwndDest, UINT nComMsg = WM_NULL, LPARAM lParam = 0, DWORD dwInQueue = 0, DWORD dwOutQueue = 0)
{
CString strTemp = _T("\\\\.\\");
strTemp += lpszDevice;
return CSerialWnd::Open(strTemp,pwndDest->GetSafeHwnd(),nComMsg,lParam,dwInQueue,dwOutQueue);
}
};
//////////////////////////////////////////////////////////////////////
// Message map macro for the default COM message
#define ON_WM_SERIAL(memberFxn) \
ON_REGISTERED_MESSAGE(CSerialMFC::mg_nDefaultComMsg,memberFxn)
//////////////////////////////////////////////////////////////////////
// Don't link to the default runtime libraries, because this will
// give a warning during link-time.
#ifdef _DEBUG
#pragma comment(linker,"/nodefaultlib:LIBCD")
#else
#pragma comment(linker,"/nodefaultlib:LIBC")
#endif
#endif // __SERIAL_MFC_H