Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
354 Kevin 1
#include "SerialWidget.h"
2
 
3
SerialWidget::SerialWidget(QWidget *parent) : QWidget(parent)
4
{
5
    // Serial connection UI
6
    btnSerialConnect = new QPushButton("&Connect");
7
    btnSerialRefresh = new QPushButton("&Refresh");
8
    labelSerialPort = new QLabel("Serial Port:");
9
    labelSerialSpeed = new QLabel("Baud Rate:");
10
    labelSerialDataBits = new QLabel("Data:");
11
    labelSerialStopBits = new QLabel("Stop:");
12
    labelSerialParity = new QLabel("Parity Bit:");
13
    labelSerialFlowControl = new QLabel("Flow Control:");
14
    cboxSerialPort = new QComboBox();
15
    cboxSerialPort->setMinimumWidth(80);
16
    cboxSerialSpeed = new QComboBox();
17
    cboxSerialSpeed->setEditable(true);
18
    cboxSerialSpeed->setValidator(new QIntValidator(0, 100000000, this));
19
    cboxSerialDataBits = new QComboBox();
20
    cboxSerialDataBits->setMaximumWidth(60);
21
    cboxSerialStopBits = new QComboBox();
22
    cboxSerialStopBits->setMaximumWidth(60);
23
    cboxSerialParity = new QComboBox();
24
    cboxSerialFlowControl = new QComboBox();
25
    cboxSerialFlowControl->setMinimumWidth(70);
26
 
27
    QGridLayout *serialSettingsLayout = new QGridLayout();
28
    serialSettingsLayout->addWidget(btnSerialConnect, 0, 0);
29
    serialSettingsLayout->addWidget(labelSerialPort, 0, 1);
30
    serialSettingsLayout->addWidget(cboxSerialPort, 0, 2);
31
    serialSettingsLayout->addWidget(labelSerialDataBits, 0, 3);
32
    serialSettingsLayout->addWidget(cboxSerialDataBits, 0, 4);
33
    serialSettingsLayout->addWidget(labelSerialParity, 0, 5);
34
    serialSettingsLayout->addWidget(cboxSerialParity, 0, 6);
35
 
36
    serialSettingsLayout->addWidget(btnSerialRefresh, 1, 0);
37
    serialSettingsLayout->addWidget(labelSerialSpeed, 1, 1);
38
    serialSettingsLayout->addWidget(cboxSerialSpeed, 1, 2);
39
    serialSettingsLayout->addWidget(labelSerialStopBits, 1, 3);
40
    serialSettingsLayout->addWidget(cboxSerialStopBits, 1, 4);
41
    serialSettingsLayout->addWidget(labelSerialFlowControl, 1, 5);
42
    serialSettingsLayout->addWidget(cboxSerialFlowControl, 1, 6);
43
 
44
    setLayout(serialSettingsLayout);
45
 
46
    serialHelper = new SerialHelper();
47
    serialThread = new QThread();
48
    serialHelper->moveToThread(serialThread);
49
 
50
    connect(serialHelper, SIGNAL(UpdateStatus(QString)), this, SIGNAL(UpdateStatus(QString)));
51
 
52
    connect(this, SIGNAL(Serial_QueryParameters()), serialHelper, SLOT(Serial_QueryParameters()));
53
    connect(serialHelper, SIGNAL(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
54
            this, SLOT(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
55
 
56
    connect(this, SIGNAL(Serial_Connect(QString,QString,QString,QString,QString,QString)),
57
            serialHelper, SLOT(Serial_Connect(QString,QString,QString,QString,QString,QString)));
58
    connect(serialHelper, SIGNAL(Serial_Connected()), this, SIGNAL(Serial_Connected()));
59
    connect(serialHelper, SIGNAL(Serial_Connected()), this, SLOT(Serial_LocalConnected()));
60
 
61
    connect(this, SIGNAL(Serial_Disconnect()), serialHelper, SLOT(Serial_Disconnect()));
62
    connect(serialHelper, SIGNAL(Serial_Disconnected()), this, SIGNAL(Serial_Disconnected()));
63
    connect(serialHelper, SIGNAL(Serial_Disconnected()), this, SLOT(Serial_LocalDisconnected()));
64
 
65
    connect(this, SIGNAL(Serial_TransmitByteArray(QByteArray)), serialHelper, SLOT(Serial_TransmitByteArray(QByteArray)));
66
 
67
    connect(serialHelper, SIGNAL(Serial_ReceivedByte(char)), this, SIGNAL(Serial_ReceivedByte(char)));
68
 
69
    connect(serialThread, SIGNAL(finished()), serialHelper, SLOT(deleteLater()));
70
    serialThread->start();
71
 
72
    connect(btnSerialConnect, SIGNAL(clicked()), this, SLOT(Serial_ConnectToggleBtn()));
73
    connect(btnSerialRefresh, SIGNAL(clicked()), this, SIGNAL(Serial_QueryParameters()));
74
 
75
    emit Serial_QueryParameters();
76
    Serial_LocalDisconnected();
77
}
78
 
79
SerialWidget::~SerialWidget()
80
{
81
    serialThread->quit();
82
}
83
 
84
void SerialWidget::Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
85
                                               QStringList stopBits, QStringList parity, QStringList flowControl)
86
{
87
    QString currPort = cboxSerialPort->currentText();
88
    cboxSerialPort->clear();
89
    cboxSerialPort->addItems(ports);
90
    if (currPort != "" && ports.contains(currPort))
91
        cboxSerialPort->setCurrentText(currPort);
92
 
93
    QString currSpeed = cboxSerialSpeed->currentText();
94
    cboxSerialSpeed->clear();
95
    cboxSerialSpeed->addItems(speeds);
96
    if (currSpeed != "") cboxSerialSpeed->setCurrentText(currSpeed);
97
 
98
    int currData = cboxSerialDataBits->currentIndex();
99
    cboxSerialDataBits->clear();
100
    cboxSerialDataBits->addItems(dataBits);
101
    if (currData >= 0) cboxSerialDataBits->setCurrentIndex(currData);
102
 
103
    int currStop = cboxSerialStopBits->currentIndex();
104
    cboxSerialStopBits->clear();
105
    cboxSerialStopBits->addItems(stopBits);
106
    if (currStop >= 0) cboxSerialStopBits->setCurrentIndex(currStop);
107
 
108
    int currParity = cboxSerialParity->currentIndex();
109
    cboxSerialParity->clear();
110
    cboxSerialParity->addItems(parity);
111
    if (currParity >= 0) cboxSerialParity->setCurrentIndex(currParity);
112
 
113
    int currFlow = cboxSerialFlowControl->currentIndex();
114
    cboxSerialFlowControl->clear();
115
    cboxSerialFlowControl->addItems(flowControl);
116
    if (currFlow >= 0) cboxSerialFlowControl->setCurrentIndex(currFlow);
117
}
118
 
119
void SerialWidget::Serial_LocalConnected()
120
{
121
    btnSerialConnect->setText("&Disconnect");
122
//    cboxSerialPort->setEnabled(false);
123
//    cboxSerialSpeed->setEnabled(false);
124
//    cboxSerialDataBits->setEnabled(false);
125
//    cboxSerialStopBits->setEnabled(false);
126
//    cboxSerialParity->setEnabled(false);
127
//    cboxSerialFlowControl->setEnabled(false);
128
//    btnSerialRefresh->setEnabled(false);
129
    btnSerialRefresh->hide();
130
    labelSerialPort->hide();
131
    labelSerialSpeed->hide();
132
    labelSerialDataBits->hide();
133
    labelSerialStopBits->hide();
134
    labelSerialParity->hide();
135
    labelSerialFlowControl->hide();
136
    cboxSerialPort->hide();
137
    cboxSerialSpeed->hide();
138
    cboxSerialDataBits->hide();
139
    cboxSerialStopBits->hide();
140
    cboxSerialParity->hide();
141
    cboxSerialFlowControl->hide();
142
}
143
 
144
void SerialWidget::Serial_LocalDisconnected()
145
{
146
    btnSerialConnect->setText("&Connect");
147
//    cboxSerialPort->setEnabled(true);
148
//    cboxSerialSpeed->setEnabled(true);
149
//    cboxSerialDataBits->setEnabled(true);
150
//    cboxSerialStopBits->setEnabled(true);
151
//    cboxSerialParity->setEnabled(true);
152
//    cboxSerialFlowControl->setEnabled(true);
153
//    btnSerialRefresh->setEnabled(true);
154
    btnSerialRefresh->show();
155
    labelSerialPort->show();
156
    labelSerialSpeed->show();
157
    labelSerialDataBits->show();
158
    labelSerialStopBits->show();
159
    labelSerialParity->show();
160
    labelSerialFlowControl->show();
161
    cboxSerialPort->show();
162
    cboxSerialSpeed->show();
163
    cboxSerialDataBits->show();
164
    cboxSerialStopBits->show();
165
    cboxSerialParity->show();
166
    cboxSerialFlowControl->show();
167
}
168
 
169
void SerialWidget::Serial_ConnectToggleBtn()
170
{
171
    if (serialHelper->connected) {
172
        emit Serial_Disconnect();
173
    } else {
174
        if (cboxSerialPort->currentText() != "" && cboxSerialSpeed->currentText() != "") {
175
            emit Serial_Connect(cboxSerialPort->currentText(), cboxSerialSpeed->currentText(), cboxSerialDataBits->currentText(),
176
                                cboxSerialStopBits->currentText(), cboxSerialParity->currentText(), cboxSerialFlowControl->currentText());
177
        }
178
    }
179
}
180
 
181
//void SerialController::Serial_ProcessIncomingData()
182
//{
183
//    char tmpBuffer[SERIAL_BUFFER_SIZE];
184
//    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
185
//    while (len > 0) {
186
//        for (int i = 0; i < len; i++) {
187
//            // Save received data into local buffer
188
//            bufferIn[bufferInIndex] = tmpBuffer[i];
189
 
190
//            // If newline char is received, end current string
191
//            if (tmpBuffer[i] == '\n') {
192
//                if (bufferInOverflow)
193
//                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
194
//                else
195
//                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
196
//                bufferInOverflow = false;
197
//                bufferInIndex = 0;
198
//                emit Serial_ReceivedString(currString);
199
//            } else {
200
//                bufferInIndex++;
201
//            }
202
 
203
//            // If received string is larger than our serial buffer, append to previous data
204
//            if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
205
//                bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
206
//                if (bufferInOverflow)
207
//                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
208
//                else
209
//                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
210
//                bufferInOverflow = true;
211
//                bufferInIndex = 0;
212
//            }
213
//        }
214
//        // Check if there is more data to be read from the serial port
215
//        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
216
//    }
217
//}