1 Star 0 Fork 0

pangweishen/IEC104SourceCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
IEC104Extention.cpp 3.89 KB
一键复制 编辑 原始数据 按行查看 历史
fatelancer 提交于 2016-03-08 17:10 . Added files via upload
#include <stdafx.h>
#include "IEC104Extention.h"
iec104ex_class::iec104ex_class()
{
mEnding = false;
mAllowConnect = true;
mLog.activateLog();
mLog.dontLogTime();
//连接事件,用于管理线程连接
//手动重置,初始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(1,1), &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 line45 IEC104EX.CPP, ErrorCode: %d",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 sz )
{
send( m_TCPSocket, data, sz, 0 );
}
void iec104ex_class::interrogationActConfIndication()
{
}
void iec104ex_class::interrogationActTermIndication()
{
}
void iec104ex_class::commandActConfIndication( iec_obj *obj )
{
}
void iec104ex_class::commandActTermIndication( iec_obj *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 h1SecondEvt = CreateEvent(NULL,FALSE,FALSE,NULL);
HANDLE hAPPExit = pIECex->hAPPExit;
HANDLE hWaitObjects[2] = { hAPPExit, h1SecondEvt };
while( true)
{
DWORD dwReturns = WaitForMultipleObjects( 2, hWaitObjects, false, 1000 );
if (dwReturns == WAIT_TIMEOUT)
{
pIECex->onTimerSecond();
}
else if( dwReturns == WAIT_OBJECT_0 )
{
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 )
{
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->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);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zfz2/IEC104SourceCode.git
git@gitee.com:zfz2/IEC104SourceCode.git
zfz2
IEC104SourceCode
IEC104SourceCode
master

搜索帮助