/[H7]/trunk/H7 Server/TcpClientClass.cpp
ViewVC logotype

Contents of /trunk/H7 Server/TcpClientClass.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65 - (show annotations) (download)
Tue Feb 6 16:44:32 2007 UTC (17 years, 3 months ago) by hedin
File size: 2966 byte(s)
serial port, baud rate and tcp port is printed to the status window, when you start the server.
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 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 }

  ViewVC Help
Powered by ViewVC 1.1.20