-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus.cpp
167 lines (81 loc) · 3.33 KB
/
modbus.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "modbus.h"
Modbus::Modbus(){
modbusSlave = new QModbusRtuSerialMaster(this);
}
bool Modbus::connectToSlave(QString port, int baud){
if (!modbusSlave) return false;
if (modbusSlave->state() != QModbusDevice::ConnectedState){
modbusSlave->setConnectionParameter(QModbusDevice::SerialPortNameParameter,port);
modbusSlave->setConnectionParameter(QModbusDevice::SerialParityParameter,QSerialPort::NoParity);
modbusSlave->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,baud);
modbusSlave->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,QSerialPort::Data8);
modbusSlave->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,QSerialPort::OneStop);
modbusSlave->setTimeout(1000);
modbusSlave->setNumberOfRetries(3);
}
if (!modbusSlave->connectDevice()){
return false;
}else{
return true;
}
}
QModbusDataUnit Modbus::prepareRequest(int regAdress, int regType){
return QModbusDataUnit( (QModbusDataUnit::RegisterType)regType, regAdress,1);
}
void Modbus::executeReadRequest(int slaveAdress, int regAdress, int regType){
if (!modbusSlave) return;
if (auto *reply = modbusSlave->sendReadRequest(prepareRequest(regAdress,regType),slaveAdress)){
if (!reply->isFinished())
connect(reply,&QModbusReply::finished,this,&Modbus::readReady);
else
delete reply;
}else{
qDebug () << "Error: " << modbusSlave->errorString();
}
}
QModbusDataUnit Modbus::prepareWriteRequest(int regAdress, int regType){
return QModbusDataUnit((QModbusDataUnit::RegisterType)regType,regAdress,1);
}
void Modbus::executeWriteRequest(int slaveAdress, int regAdress, int regType, int value){
QModbusDataUnit writeRequest = prepareWriteRequest(regAdress,regType);
for (uint i=0; i<writeRequest.valueCount(); i++){
writeRequest.setValue(i,value);
}
if (auto * reply = modbusSlave->sendWriteRequest(writeRequest,slaveAdress)){
if (!reply->isFinished()){
//TODO: conncet signal finished to slot
connect(reply,&QModbusReply::finished,this,&Modbus::writeRequestFinished);
}else{
reply->deleteLater();
}
}else{
qDebug () << "Error: " << reply->errorString();
}
}
void Modbus::readReady(){
auto reply = qobject_cast<QModbusReply*>(sender());
if (!reply) return;
if (reply->error()==QModbusDevice::NoError){
const QModbusDataUnit modbusDataUnit = reply->result();
for (uint i=0; i<modbusDataUnit.valueCount();i++){
qDebug () << "Value: " << modbusDataUnit.value(i);
}
}
else if (reply->error()==QModbusDevice::ProtocolError){
qDebug () << "Protocol error: " << reply->errorString();
}else {
qDebug () << "Error: " << reply->errorString();
}
}
void Modbus::writeRequestFinished(){
auto reply = qobject_cast<QModbusReply*>(sender());
if (!reply) return;
if (reply->error()==QModbusDevice::ProtocolError){
qDebug () << "Protocol error: " << reply->errorString();
}else if (!reply->error()==QModbusDevice::NoError){
qDebug () << "Other error: " << reply->errorString();
}
else{
qDebug () << "Send request status: ok ";
}
}