1 Star 0 Fork 12

冰雅轩/iec104text

forked from 真高兴/iec104text 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
qiec104.cpp 4.46 KB
一键复制 编辑 原始数据 按行查看 历史
真高兴 提交于 2014-08-20 09:03 . v1.0 创建104text
/*
* This software implements an IEC 60870-5-104 protocol tester.
* Copyright ?2010,2011,2012 Ricardo L. Olsen
*
* Disclaimer
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <QApplication>
#include "qiec104.h"
QIec104::QIec104( QObject *parent ) :
QObject( parent )
{
mEnding = false;
mAllowConnect = true;
SendCommands = 0;
mLog.activateLog();
mLog.doLogTime();
tcps = new QTcpSocket();
tmKeepAlive = new QTimer();
connect( tcps, SIGNAL(readyRead()), this, SLOT(slot_tcpreadytoread()) );
connect( tcps, SIGNAL(connected()), this, SLOT(slot_tcpconnect()) );
connect( tcps, SIGNAL(disconnected()), this, SLOT(slot_tcpdisconnect()) );
connect( tcps, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_tcperror(QAbstractSocket::SocketError)),Qt::DirectConnection );
connect( tmKeepAlive, SIGNAL(timeout()), this, SLOT(slot_keep_alive()) );
tcps->moveToThread( &tcpThread );
tcpThread.start( QThread::TimeCriticalPriority );
}
QIec104::~QIec104()
{
delete tmKeepAlive;
delete tcps;
}
void QIec104::dataIndication( iec_obj *obj, int numpoints )
{
emit signal_dataIndication( obj, numpoints );
}
void QIec104::connectTCP()
{
tcps->close();
if ( !mEnding && mAllowConnect )
tcps->connectToHost( getSecondaryIP(), getPortTCP(), QIODevice::ReadWrite );
}
void QIec104::disconnectTCP()
{
tcps->close();
}
void QIec104::slot_tcperror( QAbstractSocket::SocketError socketError )
{
if ( socketError != QAbstractSocket::SocketTimeoutError )
{
char buf[100];
sprintf( buf, "SocketError: %d", socketError );
mLog.pushMsg( (const char *)buf );
}
}
int QIec104::readTCP( char * buf, int szmax )
{
if (!mEnding)
return tcps->read( buf, szmax );
else
return 0;
}
// send tcp data, user provided
void QIec104::sendTCP( char * data, int sz )
{
if ( tcps->state() == QAbstractSocket::ConnectedState )
if ( !mEnding )
tcps->write( data, sz );
}
void QIec104::slot_tcpconnect()
{
tcps->setSocketOption( QAbstractSocket::LowDelayOption, 1 );
onConnectTCP();
emit signal_tcp_connect();
}
void QIec104::slot_tcpdisconnect()
{
onDisconnectTCP();
emit signal_tcp_disconnect();
}
void QIec104::slot_keep_alive()
{
if ( !mEnding )
onTimerSecond();
}
void QIec104::interrogationActConfIndication()
{
emit signal_interrogationActConfIndication();
}
void QIec104::interrogationActTermIndication()
{
emit signal_interrogationActTermIndication();
}
void QIec104::commandActConfIndication( iec_obj *obj )
{
emit signal_commandActConfIndication( obj );
}
void QIec104::commandActTermIndication( iec_obj *obj )
{
emit signal_commandActTermIndication( obj );
}
void QIec104::terminate()
{
mEnding = true;
tcps->close();
tcpThread.quit();
tcpThread.wait( 1000 );
if ( tcpThread.isRunning() )
tcpThread.terminate();
if ( tcpThread.isRunning() )
tcpThread.wait( 2000 );
}
void QIec104::slot_tcpreadytoread()
{
if ( tcps->bytesAvailable() < 6 )
return;
packetReadyTCP();
int cnt = 0;
// espera para ver se chega mais alguma coisa pela rede
while ( (tcps->bytesAvailable() > 5) && (cnt++ < 10) )
{
tcps->waitForReadyRead( 10 );
packetReadyTCP();
}
}
void QIec104::disable_connect()
{
mAllowConnect = false;
if ( tcps->state() == QAbstractSocket::ConnectedState )
disconnectTCP();
}
void QIec104::enable_connect()
{
mAllowConnect = true;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ice_elegant/iec104text.git
git@gitee.com:ice_elegant/iec104text.git
ice_elegant
iec104text
iec104text
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385