-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
184 lines (101 loc) · 3.42 KB
/
mainwindow.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer();
statusLabel = new QLabel();
QList<QSerialPortInfo> comPorts = QSerialPortInfo::availablePorts();
QList<qint32> baudListInfo = QSerialPortInfo::standardBaudRates();
foreach (QSerialPortInfo port, comPorts){
ui->portList->addItem(port.portName());
}
foreach (qint32 baudList, baudListInfo) {
ui->baudList->addItem(QString::number(baudList));
}
statusLabel->setText("Waiting..");
ui->statusBar->addWidget(statusLabel);
ui->disconnectButton->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connectButton_clicked(){
modbusMaster = new Modbus();
if(modbusMaster->connectToSlave(ui->portList->currentText(),ui->baudList->currentText().toInt())){
connect(timer,SIGNAL(timeout()),this,SLOT(onTimeoutSlot()));
connect(modbusMaster,SIGNAL(onReadReady(QModbusDataUnit)),this,SLOT(onReadReadySlot(QModbusDataUnit)));
connect(modbusMaster,SIGNAL(onReadError(QString)),this,SLOT(onReadError(QString)));
timer->start(5000);
ui->connectButton->setEnabled(false);
ui->disconnectButton->setEnabled(true);
}else{
statusLabel->setText("Error");
}
}
void MainWindow::onReadReadySlot(QModbusDataUnit reg){
//timer->stop();
int t1 = reg.value(0);
int t2 = reg.value(1);
ui->t1Label->setText(QString::number(t1));
ui->t2Label->setText(QString::number(t2));
ui->t1Gauge->setValue(t1);
ui->t2Gauge->setValue(t2);
// timer->start();
}
void MainWindow::on_pushButton_clicked(){
modbusMaster->executeWriteRequest(10,10,Modbus::HoldingRegisters,ui->rDial->value());
modbusMaster->executeWriteRequest(10,11,Modbus::HoldingRegisters,ui->gDial->value());
modbusMaster->executeWriteRequest(10,12,Modbus::HoldingRegisters,ui->bDial->value());
}
void MainWindow::onTimeoutSlot(){
static int i = 0;
i++;
modbusMaster->executeReadRequest(10,100,Modbus::InputRegisters);
qDebug () << "Debug counter: "<<i;
}
void MainWindow::onReadError(QString msg){
statusLabel->setText(msg);
}
void MainWindow::on_led1_stateChanged(int arg1)
{
if (ui->led1->isChecked()){
modbusMaster->executeWriteRequest(10,50,Modbus::Coils,1);
}else{
modbusMaster->executeWriteRequest(10,50,Modbus::Coils,0);
}
}
void MainWindow::on_led2_stateChanged(int arg1)
{
if (ui->led2->isChecked()){
modbusMaster->executeWriteRequest(10,51,Modbus::Coils,1);
}else{
modbusMaster->executeWriteRequest(10,51,Modbus::Coils,0);
}
}
void MainWindow::on_rDial_actionTriggered(int action)
{
int val = ui->rDial->value();
ui->rLabel->setText(QString::number(val));
}
void MainWindow::on_gDial_actionTriggered(int action)
{
int val = ui->rDial->value();
ui->gLabel->setText(QString::number(val));
}
void MainWindow::on_MainWindow_destroyed()
{
delete modbusMaster;
}
void MainWindow::on_disconnectButton_clicked()
{
timer->stop();
disconnect(modbusMaster,SIGNAL(onReadReady(QModbusDataUnit)),this,0);
disconnect(modbusMaster,SIGNAL(onReadError(QString)),this,0);
modbusMaster->disconnectSlave();
ui->connectButton->setEnabled(true);
ui->disconnectButton->setEnabled(false);
}