forked from tunwen/modebusLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_mb_tcp_master.cpp
68 lines (60 loc) · 1.68 KB
/
c_mb_tcp_master.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
#include "c_mb_tcp_master.h"
#include <QDebug>
C_MB_TCP_MASTER::C_MB_TCP_MASTER(QObject *parent) : QObject(parent)
{
this->m_affairID = 0;
for(int i=0;i<MB_TCP_MAX_AFF_SUM;i++)
{
this->m_listAffair.append(new C_tcp_master_affair);
connect(this->m_listAffair.last(),&C_tcp_master_affair::sig_proc,this,&C_MB_TCP_MASTER::sig_proc);
connect(this->m_listAffair.last(),&C_tcp_master_affair::sig_sendData,this,&C_MB_TCP_MASTER::sig_sendData);
}
}
// 数据事务分发:接收到的数据广播给所有事务对象
void C_MB_TCP_MASTER::slot_recvData(const QByteArray &array)
{
int listSUM = this->m_listAffair.size();
for(int i=0;i<listSUM;i++)
{
this->m_listAffair[i]->replyData(array);
}
}
// 事务ID 生成 :递增计数 达到MB_TCP_MAX_AFF_ID时计数归 1
quint16 C_MB_TCP_MASTER::makeAffairID()
{
if(this->m_affairID>= MB_TCP_MAX_AFF_ID)
{
this->m_affairID = 1;
}
return this->m_affairID += 1;
}
void C_MB_TCP_MASTER::queryCMD(MBRequestTransEx trans)
{
quint16 affID = this->makeAffairID();
MBRequestTransTCPEx tcpTrans;
tcpTrans.affairID = affID;
tcpTrans.trans = trans;
int listSUM = this->m_listAffair.size();
for(int i=0;i<listSUM;i++)
{
if(this->m_listAffair[i]->isIdel())
{
// 发送查询命令帧
this->m_listAffair[i]->queryCMD(tcpTrans);
return;
}
}
}
// 是否存在空闲affair对象
bool C_MB_TCP_MASTER::hasIdle()
{
int listSUM = this->m_listAffair.size();
for(int i=0;i<listSUM;i++)
{
if(this->m_listAffair[i]->isIdel())
{
return true;
}
}
return false;
}