基于C++和Qt开发IEC104规约主站调试软件(第二章)
基于C++和Qt开发IEC104规约主站调试软件需要综合协议规范、多线程编程和GUI框架知识以及IEC104通讯规约相关知识,以下是分步骤实现方案:
开发成果展示:https://live.csdn.net/v/473582
IEC104通讯规约知识学习:https://download.csdn.net/download/liangwwe0/90616493?spm=1011.2124.3001.6210
第一章内容:https://blog.csdn.net/liangwwe0/article/details/147241304?spm=1011.2124.3001.6209
1.界面设计
2.连接从站
void MainWindow::click_conButton(){
if(m_iec104con){
CS104_Connection_destroy(m_iec104con);
m_iec104con = nullptr;
}
//创建连接对象
m_iec104con = CS104_Connection_create(ui->ipEdit->text().toUtf8().constData(), ui->portEdit->text().toInt());
//设置连接事件句柄
CS104_Connection_setConnectionHandler(m_iec104con, connectionHandler, this);
//设置数据接收事件句柄
CS104_Connection_setASDUReceivedHandler(m_iec104con, asduReceivedHandler, this);
//设置报文打印事件句柄
CS104_Connection_setRawMessageHandler(m_iec104con, rawMessageHandler, this);
//开始进行TCP网络连接
if (CS104_Connection_connect(m_iec104con)){
//tcp连接成功,启动iec104连接
CS104_Connection_sendStartDT(m_iec104con);
}
else{
ui->textEdit->append("连接从站失败");
ui->conButton->setText(QString::fromUtf8("连接"));
ui->conButton->setEnabled(true);
}}void MainWindow::connectionHandler(void *parameter, CS104_Connection connection, CS104_ConnectionEvent event){
if(parameter==nullptr)
return;
MainWindow *that = (MainWindow*)parameter;
switch (event) {
case CS104_CONNECTION_OPENED://完成TCP网络连接
qDebug()<<QString("IEC104 Master connection established");
break;
case CS104_CONNECTION_CLOSED://104主站关闭连接
emit that->showMessageSignal("主站已关闭连接",-1);
that->ui->conButton->setText(QString::fromUtf8("连接"));
that->ui->conButton->setEnabled(true);
break;
case CS104_CONNECTION_STARTDT_CON_RECEIVED://连接104从站成功
{
emit that->showMessageSignal("连接从站成功",-1);
that->ui->conButton->setText("断开");
that->ui->conButton->setEnabled(true);
that->ui->analysisButton->setDisabled(true);
}
break;
case CS104_CONNECTION_STOPDT_CON_RECEIVED://104从站
emit that->showMessageSignal("从站关闭连接",-1);
that->ui->conButton->setText(QString::fromUtf8("连接"));
that->ui->conButton->setEnabled(true);
break;
}}void MainWindow::rawMessageHandler(void *parameter, uint8_t *msg, int msgSize, bool sent){
if(parameter==nullptr)
return;
MainWindow *that = (MainWindow*)parameter;
QByteArray data = QByteArray(reinterpret_cast<const char*>(msg),msgSize);
int type =0;
if (sent)
type = 0;
else
type = 1;
emit that->showMessageSignal(data.toHex(' ').toUpper(),type);
}
