-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_mb_tcp_master_node.cpp.Nw3640
142 lines (121 loc) · 3.76 KB
/
c_mb_tcp_master_node.cpp.Nw3640
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
#include "c_mb_tcp_master_node.h"
C_MB_tcp_master_Node::C_MB_tcp_master_Node(QObject *parent) : QObject(parent)
{
// tcp_master <-> socket
connect(&this->m_tcpMaster,&C_MB_TCP_MASTER::sig_sendData,&this->m_tcpSock,&CTcpSock::sendSockData);
connect(&this->m_tcpSock,&CTcpSock::sig_recvData,&this->m_tcpMaster,&C_MB_TCP_MASTER::slot_recvData);
// 定时器扫描
connect(&this->m_timer,&QTimer::timeout,this,&C_MB_tcp_master_Node::slot_Timer);
// node节点与master规约模块
// connect(&this->m_tcpMaster,&C_MB_TCP_MASTER::sig_proc,this,&C_MB_tcp_master_Node::slot_proc);
// connect(&this->m_tcpMaster,&C_MB_TCP_MASTER::sig_Error,this,&C_MB_tcp_master_Node::slot_Error);
}
// 定时扫描事务请求队列服务
void C_MB_tcp_master_Node::slot_Timer()
{
// 判断modbus驱动是否有空闲的事务位置
if(!this->m_tcpMaster.hasIdle())
{
return;
}
// 取事务请求队首
if(this->m_listTrans.isEmpty())
{
return;
}
this->m_tcpMaster.queryCMD(this->m_listTrans.first());
this->m_listTrans.removeFirst(); // 移出队列头
}
void C_MB_tcp_master_Node::slot_request(MBRequestTransEx trans)
{
int sum = this->m_listTrans.size();
for(int i=0;i<sum;i++)
{
if(this->m_listTrans.at(i).transID == trans.transID)
{
return;
}
}
// 添加到队列
this->m_listTrans.append(trans);
}
void C_MB_tcp_master_Node::slot_proc(int transID, quint8 slaveAdr, enumMB_FuncCode fcode, MB_ReplyBody body)
{
// 按照transID 查找接收应答的 trans对象
int sum = this->m_listTransObj.size();
for(int i=0;i<sum;i++)
{
if(this->m_listTransObj.at(i)->transID()==transID)
{
this->m_listTransObj[i]->replyProc(transID,slaveAdr,fcode,body);
}
}
}
void C_MB_tcp_master_Node::slot_Error(int transID, quint8 slaveAdr, enumMB_FuncCode fcode, TCP_Master_ErrCode errcode)
{
// 按照transID 查找接收应答的 trans对象
int sum = this->m_listTransObj.size();
for(int i=0;i<sum;i++)
{
if(this->m_listTransObj.at(i)->transID()==transID)
{
this->m_listTransObj[i]->replyErr_TCP(transID,slaveAdr,fcode,errcode);
}
}
}
void C_MB_tcp_master_Node::stopServ()
{
// 关闭串口
this->m_tcpSock.disconnectSocket();
// 依次停止事务对象
int sum = this->m_listTransObj.size();
for(int i=0;i<sum;i++)
{
this->m_listTransObj[i]->stop();
}
// 停止事务扫描定时器
this->m_timer.stop();
}
void C_MB_tcp_master_Node::startServ()
{
// 连接tcpServ (... 连接结果)
this->m_tcpSock.connectSocket(this->m_confNode.tcpMasterNode.nodeInfo.tcpServInfo.strIP,this->m_confNode.tcpMasterNode.nodeInfo.tcpServInfo.nPort);
// 启动NODE事务扫描定时器
if(!this->m_timer.isActive())
{
this->m_timer.start(500);
}
// 依次启动事务对象服务
int sum = this->m_listTransObj.size();
for(int i=0;i<sum;i++)
{
this->m_listTransObj[i]->start();
}
}
bool C_MB_tcp_master_Node::setNodeInfo(const confCommuNode &confNode)
{
this->m_confNode = confNode;
int sum = confNode.tcpMasterNode.listTrans.size();
for(int i=0;i<sum;i++)
{
C_MB_master_trans *ptr = new C_MB_master_trans;
if(ptr!=NULL)
{
ptr->setQueryTrans(confNode.tcpMasterNode.listTrans.at(i));
this->m_listTransObj.append(ptr);
connect(ptr,&C_MB_master_trans::sig_reuest,this,&C_MB_tcp_master_Node::slot_request);
}else
{
return false;
}
}
return true;
}
void C_MB_tcp_master_Node::clear()
{
foreach(C_MB_master_trans* ptr,this->m_listTransObj)
{
delete ptr;
}
this->m_listTransObj.clear();
}