11 name_ =
new QLineEdit(QString::fromStdString(h.name));
13 ip_address_ =
new QLineEdit(QString::fromStdString(h.ip_address));
16 port_->setRange(0, 1000);
17 port_->setValue(h.port);
23 recovery_->setCurrentIndex(h.modbus_error_recovery_mode);
25 tabs_ =
new QTabWidget;
26 for (std::size_t i(0); i < TAB_INDEX::SIZE; ++i)
27 tables_.emplace_back(
new QTableWidget);
31 labels.push_back(
"Topic name");
32 labels.push_back(
"Register address");
33 labels.push_back(
"Frequency (Hz)");
34 labels.push_back(
"Mask");
36 tables_.at(
BOOL)->setHorizontalHeaderLabels(labels);
40 labels.push_back(
"Topic name");
41 labels.push_back(
"Register address");
42 labels.push_back(
"Frequency (Hz)");
43 labels.push_back(
"Bit shift");
45 tables_.at(
INT8)->setHorizontalHeaderLabels(labels);
51 labels.push_back(
"Topic name");
52 labels.push_back(
"Register address");
53 labels.push_back(
"Frequency (Hz)");
61 labels.push_back(
"Topic name");
62 labels.push_back(
"Register address");
63 labels.push_back(
"Frequency (Hz)");
64 labels.push_back(
"Endianness");
93 table->setColumnWidth(0, 300);
94 table->setColumnWidth(1, 120);
95 table->setColumnWidth(2, 120);
101 QHBoxLayout *name_layout =
new QHBoxLayout;
102 name_layout->addWidget(
new QLabel(
"Name"));
103 name_layout->addWidget(
name_);
105 QHBoxLayout *ip_layout =
new QHBoxLayout;
106 ip_layout->addWidget(
new QLabel(
"IP address"));
109 QHBoxLayout *port_layout =
new QHBoxLayout;
110 port_layout->addWidget(
new QLabel(
"Port"));
111 port_layout->addWidget(
port_);
113 QHBoxLayout *recovery_layout =
new QHBoxLayout;
114 recovery_layout->addWidget(
new QLabel(
"Recovery"));
117 QHBoxLayout *registers_op_layout =
new QHBoxLayout;
118 registers_op_layout->addWidget(
add_data_);
123 layout_->addLayout(name_layout);
125 layout_->addLayout(port_layout);
126 layout_->addLayout(recovery_layout);
128 layout_->addLayout(registers_op_layout);
130 for (
auto d : h.d_bool)
132 for (
auto d : h.d_int8)
134 for (
auto d : h.d_uint8)
136 for (
auto d : h.d_int16)
138 for (
auto d : h.d_uint16)
140 for (
auto d : h.d_int32)
142 for (
auto d : h.d_uint32)
144 for (
auto d : h.d_int64)
146 for (
auto d : h.d_uint64)
148 for (
auto d : h.d_float32)
150 for (
auto d : h.d_float64)
159 const QTableWidget *table,
161 industrial_modbus_tcp::Register &r)
163 r.topic_name =
static_cast<QLineEdit *
>(table->cellWidget(row, 0))->text().toStdString();
164 r.address =
static_cast<QSpinBox *
>(table->cellWidget(row, 1))->value();
165 r.poll_rate_usec = (double) 1e6 / static_cast<QDoubleSpinBox *>(table->cellWidget(row, 2))->value();
170 industrial_modbus_tcp::ModbusHandler handler;
171 handler.ip_address =
ip_address_->text().toStdString();
172 handler.name =
name_->text().toStdString();
173 handler.port =
port_->value();
174 handler.modbus_error_recovery_mode =
recovery_->currentIndex();
176 if (handler.name.empty())
177 throw std::runtime_error(
"Handler name cannot be empty");
181 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
183 industrial_modbus_tcp::Bool
d;
185 d.mask =
static_cast<QSpinBox *
>(table->cellWidget(i, 3))->value();
186 handler.d_bool.emplace_back(d);
192 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
194 industrial_modbus_tcp::Int8
d;
196 d.bit_shift =
static_cast<QSpinBox *
>(table->cellWidget(i, 3))->value();
197 handler.d_int8.emplace_back(d);
203 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
205 industrial_modbus_tcp::UInt8
d;
207 d.bit_shift =
static_cast<QSpinBox *
>(table->cellWidget(i, 3))->value();
208 handler.d_uint8.emplace_back(d);
214 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
216 industrial_modbus_tcp::Int16
d;
218 handler.d_int16.emplace_back(d);
224 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
226 industrial_modbus_tcp::UInt16
d;
228 handler.d_uint16.emplace_back(d);
234 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
236 industrial_modbus_tcp::Int32
d;
238 d.big_endian =
static_cast<QComboBox *
>(table->cellWidget(i, 3))->currentIndex();
239 handler.d_int32.emplace_back(d);
245 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
247 industrial_modbus_tcp::UInt32
d;
249 d.big_endian =
static_cast<QComboBox *
>(table->cellWidget(i, 3))->currentIndex();
250 handler.d_uint32.emplace_back(d);
256 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
258 industrial_modbus_tcp::Int64
d;
260 d.big_endian =
static_cast<QComboBox *
>(table->cellWidget(i, 3))->currentIndex();
261 handler.d_int64.emplace_back(d);
267 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
269 industrial_modbus_tcp::UInt64
d;
271 d.big_endian =
static_cast<QComboBox *
>(table->cellWidget(i, 3))->currentIndex();
272 handler.d_uint64.emplace_back(d);
278 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
280 industrial_modbus_tcp::Float32
d;
282 d.big_endian =
static_cast<QComboBox *
>(table->cellWidget(i, 3))->currentIndex();
283 handler.d_float32.emplace_back(d);
289 for (std::size_t i(0); i < (std::size_t) table->rowCount(); ++i)
291 industrial_modbus_tcp::Float64
d;
293 d.big_endian =
static_cast<QComboBox *
>(table->cellWidget(i, 3))->currentIndex();
294 handler.d_float64.emplace_back(d);
298 for (
auto d : handler.d_bool)
299 if (
d.reg.topic_name.empty())
300 throw std::runtime_error(
"Bool: Topic name cannot be empty!");
302 for (
auto d : handler.d_int8)
303 if (
d.reg.topic_name.empty())
304 throw std::runtime_error(
"int8: Topic name cannot be empty!");
306 for (
auto d : handler.d_uint8)
307 if (
d.reg.topic_name.empty())
308 throw std::runtime_error(
"uint8: Topic name cannot be empty!");
310 for (
auto d : handler.d_int16)
311 if (
d.reg.topic_name.empty())
312 throw std::runtime_error(
"int16: Topic name cannot be empty!");
314 for (
auto d : handler.d_uint16)
315 if (
d.reg.topic_name.empty())
316 throw std::runtime_error(
"uint16: Topic name cannot be empty!");
318 for (
auto d : handler.d_int32)
319 if (
d.reg.topic_name.empty())
320 throw std::runtime_error(
"int32: Topic name cannot be empty!");
322 for (
auto d : handler.d_int64)
323 if (
d.reg.topic_name.empty())
324 throw std::runtime_error(
"int64: Topic name cannot be empty!");
326 for (
auto d : handler.d_float32)
327 if (
d.reg.topic_name.empty())
328 throw std::runtime_error(
"float32: Topic name cannot be empty!");
330 for (
auto d : handler.d_float64)
331 if (
d.reg.topic_name.empty())
332 throw std::runtime_error(
"float64: Topic name cannot be empty!");
339 return name_->text();
345 QLineEdit *topic_name =
new QLineEdit(QString::fromStdString(r.topic_name));
347 topic_name->setValidator(
new QRegExpValidator(QRegExp(
"[A-Za-z0-9_/]+"),
this));
348 table->setCellWidget(table->rowCount() - 1, 0, topic_name);
350 QSpinBox *address =
new QSpinBox;
351 address->setRange(0, 0x20000);
352 address->setValue(r.address);
353 table->setCellWidget(table->rowCount() - 1, 1, address);
355 QDoubleSpinBox *frequency =
new QDoubleSpinBox;
356 frequency->setRange(0.01, 1000);
357 frequency->setSingleStep(1);
358 frequency->setValue((
double) 1e6 / r.poll_rate_usec);
359 table->setCellWidget(table->rowCount() - 1, 2, frequency);
364 QComboBox *truefalse(
new QComboBox);
365 truefalse->addItem(
"False");
366 truefalse->addItem(
"True");
367 truefalse->setCurrentIndex(value);
368 table->setCellWidget(table->rowCount() - 1, 3, truefalse);
374 table->insertRow(table->rowCount());
377 QSpinBox *mask(
new QSpinBox);
378 mask->setRange(0, 65535);
379 mask->setValue(d.mask);
380 table->setCellWidget(table->rowCount() - 1, 3, mask);
386 table->insertRow(table->rowCount());
389 QSpinBox *bit_shift(
new QSpinBox);
390 bit_shift->setRange(0, 16);
391 bit_shift->setValue(d.bit_shift);
392 table->setCellWidget(table->rowCount() - 1, 3, bit_shift);
398 table->insertRow(table->rowCount());
401 QSpinBox *bit_shift(
new QSpinBox);
402 bit_shift->setRange(0, 16);
403 bit_shift->setValue(d.bit_shift);
404 table->setCellWidget(table->rowCount() - 1, 3, bit_shift);
410 table->insertRow(table->rowCount());
417 table->insertRow(table->rowCount());
424 table->insertRow(table->rowCount());
432 table->insertRow(table->rowCount());
440 table->insertRow(table->rowCount());
448 table->insertRow(table->rowCount());
456 table->insertRow(table->rowCount());
464 table->insertRow(table->rowCount());
471 switch (
tabs_->currentIndex())
475 industrial_modbus_tcp::Bool
d;
476 d.reg.poll_rate_usec = 200000;
482 industrial_modbus_tcp::Int8
d;
483 d.reg.poll_rate_usec = 200000;
489 industrial_modbus_tcp::UInt8
d;
490 d.reg.poll_rate_usec = 200000;
496 industrial_modbus_tcp::Int16
d;
497 d.reg.poll_rate_usec = 200000;
503 industrial_modbus_tcp::UInt16
d;
504 d.reg.poll_rate_usec = 200000;
510 industrial_modbus_tcp::Int32
d;
511 d.reg.poll_rate_usec = 200000;
517 industrial_modbus_tcp::UInt32
d;
518 d.reg.poll_rate_usec = 200000;
524 industrial_modbus_tcp::Int64
d;
525 d.reg.poll_rate_usec = 200000;
531 industrial_modbus_tcp::UInt64
d;
532 d.reg.poll_rate_usec = 200000;
538 industrial_modbus_tcp::Float32
d;
539 d.reg.poll_rate_usec = 200000;
545 industrial_modbus_tcp::Float64
d;
557 table->removeRow(table->rowCount() - 1);
ModbusTCPHandler(industrial_modbus_tcp::ModbusHandler h=industrial_modbus_tcp::ModbusHandler())
void addBigEndian(const bool value, QTableWidget *table)
virtual ~ModbusTCPHandler()
industrial_modbus_tcp::ModbusHandler getHandler()
void addInt32(const industrial_modbus_tcp::Int32 &d)
QPushButton * remove_data_
void addInt64(const industrial_modbus_tcp::Int64 &d)
void addUInt64(const industrial_modbus_tcp::UInt64 &d)
void addFloat32(const industrial_modbus_tcp::Float32 &d)
std::vector< QTableWidget * > tables_
void addFloat64(const industrial_modbus_tcp::Float64 &d)
void addInt8(const industrial_modbus_tcp::Int8 &d)
void addInt16(const industrial_modbus_tcp::Int16 &d)
void addRegister(const industrial_modbus_tcp::Register &r, QTableWidget *table)
void addUInt8(const industrial_modbus_tcp::UInt8 &d)
void readRegisterTable(const QTableWidget *table, const unsigned row, industrial_modbus_tcp::Register &r)
void addUInt32(const industrial_modbus_tcp::UInt32 &d)
void addUInt16(const industrial_modbus_tcp::UInt16 &d)
void addBool(const industrial_modbus_tcp::Bool &d)