-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialWnd.cpp
131 lines (101 loc) · 3.37 KB
/
SerialWnd.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
// SerialWnd.cpp - Implementation of the CSerialWnd class
//
// Copyright (C) 1999-2003 Ramon de Klein (Ramon.de.Klein@ict.nl)
//
// 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
//////////////////////////////////////////////////////////////////////
// Include the standard header files
#include "pch.h"
#define STRICT
#include <crtdbg.h>
#include <tchar.h>
#include <windows.h>
//////////////////////////////////////////////////////////////////////
// Include module headerfile
#include "SerialWnd.h"
//////////////////////////////////////////////////////////////////////
// Disable warning C4127: conditional expression is constant, which
// is generated when using the _RPTF and _ASSERTE macros.
#pragma warning(disable: 4127)
//////////////////////////////////////////////////////////////////////
// Enable debug memory manager
#ifdef _DEBUG
#ifdef THIS_FILE
#undef THIS_FILE
#endif
static const char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Code
// Register the standard CSerialWnd COM message
const UINT CSerialWnd::mg_nDefaultComMsg = ::RegisterWindowMessage(_T("CSerialWnd_DefaultComMsg"));
CSerialWnd::CSerialWnd()
: m_hwndDest(0)
, m_nComMsg(WM_NULL)
, m_lParam(0)
{
}
CSerialWnd::~CSerialWnd()
{
// Check if the thread handle is still there. If so, then we
// didn't close the serial port. We cannot depend on the
// CSerial destructor, because if it calls Close then it
// won't call our overridden Close.
if (m_hThread)
{
// Display a warning
_RPTF0(_CRT_WARN, "CSerialWnd::~CSerialWnd - Serial port not closed\n");
// Close implicitly
Close();
}
}
LONG CSerialWnd::Open (LPCTSTR lpszDevice, HWND hwndDest, UINT nComMsg, LPARAM lParam, DWORD dwInQueue, DWORD dwOutQueue)
{
// Call the base class first
long lLastError = CSerialEx::Open(lpszDevice,dwInQueue,dwOutQueue);
if (lLastError != ERROR_SUCCESS)
return lLastError;
// Save the window handle, notification message and user message
m_hwndDest = hwndDest;
m_nComMsg = nComMsg?nComMsg:mg_nDefaultComMsg;
m_lParam = lParam;
// Start the listener thread
lLastError = StartListener();
if (lLastError != ERROR_SUCCESS)
{
// Close the serial port
Close();
// Return the error-code
m_lLastError = lLastError;
return m_lLastError;
}
// Return the error
m_lLastError = ERROR_SUCCESS;
return m_lLastError;
}
LONG CSerialWnd::Close (void)
{
// Reset all members
m_hwndDest = 0;
m_nComMsg = WM_NULL;
// Call the base class
return CSerialEx::Close();
}
void CSerialWnd::OnEvent (EEvent eEvent, EError eError)
{
// Post message to the client window
::PostMessage(m_hwndDest,m_nComMsg,MAKEWPARAM(eEvent,eError),LPARAM(m_lParam));
}