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 |
// Depending on what we have received from client, we replay with the relevant data.
|
40 |
switch( atoi(Buff) )
|
41 |
{
|
42 |
case 110:
|
43 |
parent->H7Serial.writeTarget(LED3,OFF);
|
44 |
break;
|
45 |
case 111:
|
46 |
parent->H7Serial.writeTarget(LED3,ON);
|
47 |
break;
|
48 |
case 120:
|
49 |
parent->H7Serial.writeTarget(LED4,OFF);
|
50 |
break;
|
51 |
case 121:
|
52 |
parent->H7Serial.writeTarget(LED4,ON);
|
53 |
break;
|
54 |
case 130:
|
55 |
parent->H7Serial.writeTarget(LED5,OFF);
|
56 |
break;
|
57 |
case 131:
|
58 |
parent->H7Serial.writeTarget(LED5,ON);
|
59 |
break;
|
60 |
case 100:
|
61 |
SendReply(); // <--
|
62 |
break;
|
63 |
}
|
64 |
Sleep(10);
|
65 |
parent->UpdateClient(this);
|
66 |
}
|
67 |
catch (std::exception e)
|
68 |
{
|
69 |
CString msg;
|
70 |
msg.Format("%d : %s", atoi(Buff), e.what() );
|
71 |
MessageBox(0, msg, "", MB_OK);
|
72 |
}
|
73 |
|
74 |
CSocket::OnReceive(nErrorCode);
|
75 |
}
|
76 |
|
77 |
void TcpClientClass::SendReply(void)
|
78 |
{
|
79 |
CH7ServerDlg* parent = (CH7ServerDlg*) AfxGetMainWnd();
|
80 |
|
81 |
CString output;
|
82 |
short Tmp;
|
83 |
CString format;
|
84 |
const int timer = 10;
|
85 |
|
86 |
Sleep(timer);
|
87 |
mLed3 = parent->H7Serial.readTarget(0);
|
88 |
if( mLed3 == 0 )
|
89 |
output = "110|";
|
90 |
else if( mLed3 == 1)
|
91 |
output = "111|";
|
92 |
Send(output,output.GetLength(),0);
|
93 |
|
94 |
Sleep(timer);
|
95 |
|
96 |
mLed4 = parent->H7Serial.readTarget(1);
|
97 |
if( mLed4 == 0 )
|
98 |
output = "120|";
|
99 |
else if( mLed4 == 1)
|
100 |
output = "121|";
|
101 |
Send(output,output.GetLength(),0);
|
102 |
|
103 |
Sleep(timer);
|
104 |
|
105 |
mLed5 = parent->H7Serial.readTarget(2);
|
106 |
if( mLed5 == 0 )
|
107 |
output = "130|";
|
108 |
else if( mLed5 == 1)
|
109 |
output = "131|";
|
110 |
Send(output,output.GetLength(),0);
|
111 |
Sleep(timer);
|
112 |
|
113 |
mSw2 = parent->H7Serial.readTarget(3);
|
114 |
if( mSw2 == 0 )
|
115 |
output = "140|";
|
116 |
else if( mSw2 == 1 )
|
117 |
output = "141|";
|
118 |
Send(output,output.GetLength(),0);
|
119 |
Sleep(timer);
|
120 |
|
121 |
mSw3 = parent->H7Serial.readTarget(4);
|
122 |
if( mSw3 == 0 )
|
123 |
output = "150|";
|
124 |
else if( mSw3 == 1 )
|
125 |
output = "151|";
|
126 |
Send(output,output.GetLength(),0);
|
127 |
Sleep(timer);
|
128 |
|
129 |
mPot = parent->H7Serial.readTarget(5);
|
130 |
format.Format("%d|",mPot + 2000);
|
131 |
Send(format,format.GetLength(),0);
|
132 |
Sleep(timer);
|
133 |
|
134 |
mTemp = parent->H7Serial.readTarget(6);
|
135 |
format.Format("%d|",mTemp + 4000);
|
136 |
Send(format,format.GetLength(),0);
|
137 |
Sleep(timer);
|
138 |
}
|
139 |
|
140 |
void TcpClientClass::OnClose(int nErrorCode)
|
141 |
{
|
142 |
isClosed = true;
|
143 |
|
144 |
CSocket::OnClose(nErrorCode);
|
145 |
}
|