-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWinccView.cpp
More file actions
289 lines (243 loc) · 6.11 KB
/
WinccView.cpp
File metadata and controls
289 lines (243 loc) · 6.11 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// WinccView.cpp : implementation of the CWinccView class
//
#include "stdafx.h"
#include "Wincc.h"
#include "WinccDoc.h"
#include "WinccView.h"
#include "QueryDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWinccView
IMPLEMENT_DYNCREATE(CWinccView, CView)
BEGIN_MESSAGE_MAP(CWinccView, CView)
//{{AFX_MSG_MAP(CWinccView)
ON_COMMAND(IDM_QUERY, OnQuery)
ON_COMMAND(IDM_SET, OnSet)
ON_WM_CREATE()
ON_WM_TIMER()
ON_COMMAND(IDM_COM1, OnCom1)
ON_COMMAND(IDM_COM2, OnCom2)
ON_COMMAND(IDM_COM3, OnCom3)
ON_COMMAND(IDM_COM4, OnCom4)
ON_COMMAND(IDM_COM5, OnCom5)
ON_COMMAND(IDM_COM6, OnCom6)
ON_COMMAND(IDM_COM7, OnCom7)
ON_COMMAND(IDM_COM8, OnCom8)
ON_COMMAND(IDM_SEND, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWinccView construction/destruction
CWinccView::CWinccView()
{
// TODO: add construction code here
flag=0;
handler=NULL;
memset(buffer,0,100);
}
CWinccView::~CWinccView()
{
}
BOOL CWinccView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CWinccView drawing
void CWinccView::OnDraw(CDC* pDC)
{
CWinccDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CWinccView diagnostics
#ifdef _DEBUG
void CWinccView::AssertValid() const
{
CView::AssertValid();
}
void CWinccView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CWinccDoc* CWinccView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWinccDoc)));
return (CWinccDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CWinccView message handlers
void CWinccView::OnQuery()
{
// TODO: Add your command handler code here
if(!flag)
{
query.Create(IDD_QUERY,this);
set.DestroyWindow();
query.ShowWindow(SW_SHOW);
flag=1;
SetTimer(1,1000,NULL);
}
}
void CWinccView::OnSet()
{
// TODO: Add your command handler code here
if(flag)
{
KillTimer(1);
set.Create(IDD_SET,this);
query.DestroyWindow();
set.ShowWindow(SW_SHOW);
flag=0;
}
}
int CWinccView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
query.c=0;
set.s=0;
OnQuery();
return 0;
}
void CWinccView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(handler!=NULL)
{
memset(buffer,0,100);
ReadData(handler,buffer);
if(strlen(buffer)!=0)
query.Show(buffer);
memset(buffer,0,100);
query.UpdateData();
buffer[0]='*';
buffer[1]=query.c+1;//由于单选默认从0开始编号,0的话会被当作结束位,遂+1处理
WriteData(handler, buffer);
}
CView::OnTimer(nIDEvent);
}
void CWinccView::OnSend()
{
// TODO: Add your command handler code here
memset(buffer,0,100);
set.Get(buffer);
WriteData(handler, buffer);
}
void CWinccView::OnCom1()
{
// TODO: Add your command handler code here
handler=InitCom("COM1");
}
void CWinccView::OnCom2()
{
// TODO: Add your command handler code here
handler=InitCom("COM2");
}
void CWinccView::OnCom3()
{
// TODO: Add your command handler code here
handler=InitCom("COM3");
}
void CWinccView::OnCom4()
{
// TODO: Add your command handler code here
handler=InitCom("COM4");
}
void CWinccView::OnCom5()
{
// TODO: Add your command handler code here
handler=InitCom("COM5");
}
void CWinccView::OnCom6()
{
// TODO: Add your command handler code here
handler=InitCom("COM6");
}
void CWinccView::OnCom7()
{
// TODO: Add your command handler code here
handler=InitCom("COM7");
}
void CWinccView::OnCom8()
{
// TODO: Add your command handler code here
handler=InitCom("COM8");
}
HANDLE CWinccView::InitCom(char* comName)
{
HANDLE hCom;
hCom = CreateFile(comName,//COM7口
GENERIC_READ|GENERIC_WRITE, //允许读和写
0, //独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
0, //同步方式
NULL);
if(hCom == (HANDLE)-1)
{
return NULL;
}
SetupComm(hCom,100,100); //输入缓冲区和输出缓冲区的大小都是100
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
//在读一次输入缓冲区的内容后读操作就立即返回,
//而不管是否读入了要求的字符。
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate=115200; //波特率为9600
dcb.ByteSize=8; //每个字节有8位
dcb.Parity=NOPARITY; //无奇偶校验位
dcb.StopBits=1; //两个停止位
SetCommState(hCom, &dcb);
PurgeComm(hCom, PURGE_TXCLEAR|PURGE_RXCLEAR);
return hCom;
}
int CWinccView::ReadData(HANDLE handler, char* buffer)
{
char readBuffer[100];
memset(readBuffer, 0, 100);
DWORD wCount= 100;//读取的字节数
BOOL bReadStat;
bReadStat = ReadFile(handler, readBuffer, wCount, &wCount, NULL);
if(!bReadStat)
{
AfxMessageBox("读串口失败!");
return -1;
}
strcpy(buffer, readBuffer);
PurgeComm(handler, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return 0;
}
int CWinccView::WriteData(HANDLE handler, char* buffer)
{
unsigned long dwBytesWrite;
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(handler, &dwErrorFlags, &ComStat);
dwBytesWrite = strlen(buffer);
bWriteStat=WriteFile(handler, buffer, dwBytesWrite, &dwBytesWrite, NULL);
if(!bWriteStat)
{
AfxMessageBox("写串口失败!");
return -1;
}
return 0;
}