6 |
#include "Resource.h" |
#include "Resource.h" |
7 |
#include "H7 serverDlg.h" |
#include "H7 serverDlg.h" |
8 |
|
|
9 |
|
#include <exception> |
10 |
|
|
11 |
TcpClientClass::TcpClientClass(void) |
TcpClientClass::TcpClientClass(void) |
12 |
{ |
{ |
13 |
|
isClosed = false; |
14 |
} |
} |
15 |
|
|
16 |
TcpClientClass::~TcpClientClass(void) |
TcpClientClass::~TcpClientClass(void) |
21 |
{ |
{ |
22 |
// MessageBox(0,"OnReceive","",MB_OK); |
// MessageBox(0,"OnReceive","",MB_OK); |
23 |
|
|
24 |
int size = 1023; |
int size = 4096; |
25 |
char Buffer[1023]; |
char Buffer[4096]; |
26 |
CString Buff; |
CString Buff; |
27 |
|
|
28 |
size = TcpClientClass::Receive(Buffer, size -1); |
size = TcpClientClass::Receive(Buffer, size -1); |
31 |
|
|
32 |
CH7ServerDlg* parent = (CH7ServerDlg*) AfxGetMainWnd(); |
CH7ServerDlg* parent = (CH7ServerDlg*) AfxGetMainWnd(); |
33 |
|
|
34 |
|
CString debug; |
35 |
switch( atoi(Buff) ) |
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(this); |
65 |
|
} |
66 |
|
catch (std::exception e) |
67 |
{ |
{ |
68 |
case 110: |
CString msg; |
69 |
parent->H7Serial.writeTarget(LED3,OFF); |
msg.Format("%d : %s", atoi(Buff), e.what() ); |
70 |
break; |
MessageBox(0, msg, "", MB_OK); |
|
case 111: |
|
|
parent->H7Serial.writeTarget(LED3,ON); |
|
|
break; |
|
|
case 120: |
|
|
parent->H7Serial.writeTarget(LED4,OFF); |
|
|
break; |
|
|
case 121: |
|
|
parent->H7Serial.writeTarget(LED4,ON); |
|
|
break; |
|
|
case 130: |
|
|
parent->H7Serial.writeTarget(LED5,OFF); |
|
|
break; |
|
|
case 131: |
|
|
parent->H7Serial.writeTarget(LED5,ON); |
|
|
break; |
|
71 |
} |
} |
72 |
|
|
73 |
CSocket::OnReceive(nErrorCode); |
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 |
|
mLed3 = parent->H7Serial.readTarget(0); |
87 |
|
if( mLed3 == 0 ) |
88 |
|
output = "110|"; |
89 |
|
else if( mLed3 == 1) |
90 |
|
output = "111|"; |
91 |
|
Send(output,output.GetLength(),0); |
92 |
|
|
93 |
|
Sleep(timer); |
94 |
|
|
95 |
|
mLed4 = parent->H7Serial.readTarget(1); |
96 |
|
if( mLed4 == 0 ) |
97 |
|
output = "120|"; |
98 |
|
else if( mLed4 == 1) |
99 |
|
output = "121|"; |
100 |
|
Send(output,output.GetLength(),0); |
101 |
|
|
102 |
|
Sleep(timer); |
103 |
|
|
104 |
|
mLed5 = parent->H7Serial.readTarget(2); |
105 |
|
if( mLed5 == 0 ) |
106 |
|
output = "130|"; |
107 |
|
else if( mLed5 == 1) |
108 |
|
output = "131|"; |
109 |
|
Send(output,output.GetLength(),0); |
110 |
|
Sleep(timer); |
111 |
|
|
112 |
|
mSw2 = parent->H7Serial.readTarget(3); |
113 |
|
if( mSw2 == 0 ) |
114 |
|
output = "140|"; |
115 |
|
else if( mSw2 == 1 ) |
116 |
|
output = "141|"; |
117 |
|
Send(output,output.GetLength(),0); |
118 |
|
Sleep(timer); |
119 |
|
|
120 |
|
mSw3 = parent->H7Serial.readTarget(4); |
121 |
|
if( mSw3 == 0 ) |
122 |
|
output = "150|"; |
123 |
|
else if( mSw3 == 1 ) |
124 |
|
output = "151|"; |
125 |
|
Send(output,output.GetLength(),0); |
126 |
|
Sleep(timer); |
127 |
|
|
128 |
|
mPot = parent->H7Serial.readTarget(5); |
129 |
|
format.Format("%d|",mPot + 2000); |
130 |
|
Send(format,format.GetLength(),0); |
131 |
|
Sleep(timer); |
132 |
|
|
133 |
|
mTemp = parent->H7Serial.readTarget(6); |
134 |
|
format.Format("%d|",mTemp + 4000); |
135 |
|
Send(format,format.GetLength(),0); |
136 |
|
Sleep(timer); |
137 |
|
} |
138 |
|
|
139 |
|
void TcpClientClass::OnClose(int nErrorCode) |
140 |
|
{ |
141 |
|
isClosed = true; |
142 |
|
|
143 |
|
CSocket::OnClose(nErrorCode); |
144 |
|
} |