forked from MiniSoda/IEC104
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIEC104Extention.cpp
234 lines (198 loc) · 4.67 KB
/
IEC104Extention.cpp
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
#include "stdafx.h"
#include "IEC104Extention.h"
iec104ex_class::iec104ex_class()
{
mEnding = false;
mAllowConnect = true;
bIntegralTotal = FALSE;
mLog.activateLog();
mLog.dontLogTime();
m_timeout = -1;
//连接事件,用于管理线程连接
//手动重置,初始FALSE
hConnectEvt = CreateEvent(NULL,TRUE,FALSE,NULL);
//程序退出
hAPPExit = CreateEvent(NULL,TRUE,FALSE,NULL);
//每秒执行的计时器
AfxBeginThread( threadStateFunc, this );
//监听端口
AfxBeginThread( threadListening, this );
}
void iec104ex_class::connectTCP()
{
//初始化Socket
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
m_TCPSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( INVALID_SOCKET == m_TCPSocket )
{
char info[255];
sprintf_s( info, "Error at socket(): %d\n", WSAGetLastError() );
mLog.pushMsg( info );
WSACleanup();
return;
}
SOCKADDR_IN addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons( getPortTCP() );
addr.sin_addr.S_un.S_addr = inet_addr( getSecondaryIP() );
int iError = connect(m_TCPSocket, (sockaddr *)&addr, sizeof(addr) );
if( iError <0 )
{
char szInfo[255] = {0};
sprintf_s(szInfo,"Error in connect() at %d %s, ErrorCode: %d",__FUNCTION__, __FILE__, WSAGetLastError());
mLog.pushMsg(szInfo);
return;
}
enable_connect();
onConnectTCP();
return;
}
void iec104ex_class::disconnectTCP()
{
int nRet = shutdown(m_TCPSocket,SD_RECEIVE);
if( SOCKET_ERROR == nRet )
{
char szText[255];
sprintf_s( szText, "ERROR occurs in shutdown(),ERRORCODE: %d ",WSAGetLastError());
mLog.pushMsg( szText );
return;
}
disable_connect();
closesocket(m_TCPSocket);
WSACleanup();
m_TCPSocket = NULL;
onDisconnectTCP();
return;
}
int iec104ex_class::readTCP( char * buf, int szmax )
{
int nRet = recv( m_TCPSocket, buf, szmax, 0);
return nRet;
}
void iec104ex_class::sendTCP( char * data, int size )
{
send( m_TCPSocket, data, size, 0 );
}
void iec104ex_class::interrogationActConfIndication()
{
}
void iec104ex_class::interrogationActTermIndication()
{
if( bIntegralTotal )
{
solicitIntegratedTotal();
bITreceived = false;
}else
setGICountDown( m_timeout );
return;
}
void iec104ex_class::commandActConfIndication( iec_obj *obj )
{
}
void iec104ex_class::commandActTermIndication( iec_obj *obj )
{
}
void iec104ex_class::integraltotalActConfIndication()
{
if (!bITreceived)
{
ReadIntegratedTotal();
}
}
void iec104ex_class::integraltotalActTermIndication()
{
setGICountDown( m_timeout );
return;
}
//send msg to main msg_queue to process data
//be careful about the pointers which may exceed the range of array
//不能使用postmessage, 除非将obj拷贝走
void iec104ex_class::dataIndication( iec_obj *obj, int numpoints )
{
HWND hMainWindow = AfxGetApp()->m_pMainWnd->m_hWnd;
::SendMessage( hMainWindow, WM_INFONOTIFY, (WPARAM )obj, (LPARAM)numpoints);
return;
}
void iec104ex_class::startListening()
{
AfxBeginThread( threadListening, this );
}
UINT iec104ex_class::threadStateFunc( LPVOID lParam )
{
iec104ex_class *pIECex = (iec104ex_class *)lParam;
HANDLE hAPPExit = pIECex->hAPPExit;
while( true)
{
DWORD dwReturns = WaitForSingleObject( hAPPExit, 1000 );
if (dwReturns == WAIT_TIMEOUT)
{
pIECex->onTimerSecond();
}
else if( dwReturns == WAIT_OBJECT_0 )
{
pIECex->disconnectTCP();
break;
}
}
return 0;
}
iec104ex_class::~iec104ex_class()
{
mLog.deactivateLog();
}
UINT iec104ex_class::threadListening( LPVOID lParam )
{
iec104ex_class *pIECex = (iec104ex_class *)lParam;
HANDLE hAPPExit = pIECex->hAPPExit;
HANDLE hWaitObjects[2] = { hAPPExit, pIECex->hConnectEvt };
while( true )
{
DWORD dwReturn = WaitForMultipleObjects( 2, hWaitObjects, false, INFINITE ) ;
if( dwReturn == WAIT_OBJECT_0 )
{
pIECex->disconnectTCP();
break;
}else if( dwReturn == WAIT_OBJECT_0+1 )
{
char buf[1024];
memset( buf, 0 ,1024);
int BytesInQue = recv(pIECex->m_TCPSocket,(char*)buf, 1024, MSG_PEEK);
if( BytesInQue <= 0 )
{
//do something to disconnect
pIECex->disconnectTCP();
}
pIECex->packetReadyTCP();
}
}
return 0;
}
void iec104ex_class::enable_connect()
{
SetEvent(hConnectEvt);
return;
}
void iec104ex_class::disable_connect()
{
ResetEvent(hConnectEvt);
return;
}
void iec104ex_class::OnExit()
{
SetEvent(hAPPExit);
}
void iec104ex_class::setIntegralTotal( BOOL flag )
{
bIntegralTotal = flag;
return;
}
BOOL iec104ex_class::getIntegralTotal( )
{
return bIntegralTotal;
}
void iec104ex_class::setCountDown( const int timeout )
{
m_timeout = timeout;
return;
}