1 |
#include "StdAfx.h"
|
2 |
#include ".\tcpclientclass.h"
|
3 |
#include "Define.h"
|
4 |
#include "H7Serial.h"
|
5 |
|
6 |
#include "Resource.h"
|
7 |
#include "H7 serverDlg.h"
|
8 |
|
9 |
#include <exception>
|
10 |
|
11 |
TcpClientClass::TcpClientClass(void)
|
12 |
{
|
13 |
isClosed = false;
|
14 |
}
|
15 |
|
16 |
TcpClientClass::~TcpClientClass(void)
|
17 |
{
|
18 |
}
|
19 |
|
20 |
void TcpClientClass::OnReceive(int nErrorCode)
|
21 |
{
|
22 |
// MessageBox(0,"OnReceive","",MB_OK);
|
23 |
|
24 |
int size = 4096;
|
25 |
char Buffer[4096];
|
26 |
CString Buff;
|
27 |
|
28 |
size = TcpClientClass::Receive(Buffer, size -1);
|
29 |
Buffer[size] = 0;
|
30 |
Buff = Buffer;
|
31 |
|
32 |
CH7ServerDlg* parent = (CH7ServerDlg*) AfxGetMainWnd();
|
33 |
|
34 |
CString debug;
|
35 |
debug.Format("%d\n",atoi(Buff));
|
36 |
OutputDebugString( debug );
|
37 |
|
38 |
try{
|
39 |
switch( atoi(Buff) )
|
40 |
{
|
41 |
case 110:
|
42 |
parent->H7Serial.writeTarget(LED3,OFF);
|
43 |
break;
|
44 |
case 111:
|
45 |
parent->H7Serial.writeTarget(LED3,ON);
|
46 |
break;
|
47 |
case 120:
|
48 |
parent->H7Serial.writeTarget(LED4,OFF);
|
49 |
break;
|
50 |
case 121:
|
51 |
parent->H7Serial.writeTarget(LED4,ON);
|
52 |
break;
|
53 |
case 130:
|
54 |
parent->H7Serial.writeTarget(LED5,OFF);
|
55 |
break;
|
56 |
case 131:
|
57 |
parent->H7Serial.writeTarget(LED5,ON);
|
58 |
break;
|
59 |
case 100:
|
60 |
SendReply(); // <--
|
61 |
break;
|
62 |
}
|
63 |
Sleep(10);
|
64 |
parent->UpdateClient();
|
65 |
}
|
66 |
catch (std::exception e)
|
67 |
{
|
68 |
CString msg;
|
69 |
msg.Format("%d : %s", atoi(Buff), e.what() );
|
70 |
MessageBox(0, msg, "", MB_OK);
|
71 |
}
|
72 |
|
73 |
CSocket::OnReceive(nErrorCode);
|
74 |
}
|
75 |
|
76 |
void TcpClientClass::SendReply(void)
|
77 |
{
|
78 |
CH7ServerDlg* parent = (CH7ServerDlg*) AfxGetMainWnd();
|
79 |
|
80 |
CString output;
|
81 |
short Tmp;
|
82 |
CString format;
|
83 |
const int timer = 10;
|
84 |
|
85 |
Sleep(timer);
|
86 |
Tmp = parent->H7Serial.readTarget(0);
|
87 |
if( Tmp == 0 )
|
88 |
output = "110|";
|
89 |
else if( Tmp == 1)
|
90 |
output = "111|";
|
91 |
Send(output,output.GetLength(),0);
|
92 |
|
93 |
Sleep(timer);
|
94 |
|
95 |
Tmp = parent->H7Serial.readTarget(1);
|
96 |
if( Tmp == 0 )
|
97 |
output = "120|";
|
98 |
else if( Tmp == 1)
|
99 |
output = "121|";
|
100 |
Send(output,output.GetLength(),0);
|
101 |
|
102 |
Sleep(timer);
|
103 |
|
104 |
Tmp = parent->H7Serial.readTarget(2);
|
105 |
if( Tmp == 0 )
|
106 |
output = "130|";
|
107 |
else if( Tmp == 1)
|
108 |
output = "131|";
|
109 |
Send(output,output.GetLength(),0);
|
110 |
Sleep(timer);
|
111 |
|
112 |
Tmp = parent->H7Serial.readTarget(3);
|
113 |
if( Tmp == 0 )
|
114 |
output = "140|";
|
115 |
else if( Tmp == 1 )
|
116 |
output = "141|";
|
117 |
Send(output,output.GetLength(),0);
|
118 |
Sleep(timer);
|
119 |
|
120 |
Tmp = parent->H7Serial.readTarget(4);
|
121 |
if( Tmp == 0 )
|
122 |
output = "150|";
|
123 |
else if( Tmp == 1 )
|
124 |
output = "151|";
|
125 |
Send(output,output.GetLength(),0);
|
126 |
Sleep(timer);
|
127 |
|
128 |
Tmp = parent->H7Serial.readTarget(5);
|
129 |
Tmp += 2000;
|
130 |
format.Format("%d|",Tmp);
|
131 |
Send(format,format.GetLength(),0);
|
132 |
Sleep(timer);
|
133 |
|
134 |
Tmp = parent->H7Serial.readTarget(6);
|
135 |
Tmp += 4000;
|
136 |
format.Format("%d|",Tmp);
|
137 |
Send(format,format.GetLength(),0);
|
138 |
Sleep(timer);
|
139 |
}
|
140 |
|
141 |
void TcpClientClass::OnClose(int nErrorCode)
|
142 |
{
|
143 |
isClosed = true;
|
144 |
|
145 |
CSocket::OnClose(nErrorCode);
|
146 |
}
|