Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
344 Kevin 1
#include "SerialController.h"
2
 
351 Kevin 3
SerialController::SerialController(QWidget *parent) : QWidget(parent)
344 Kevin 4
{
351 Kevin 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
    // Parameters
53
    connect(this, SIGNAL(Serial_QueryParameters()), serialHelper, SLOT(Serial_QueryParameters()));
54
    connect(serialHelper, SIGNAL(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
55
            this, SLOT(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
56
 
57
    // Connect
58
    connect(this, SIGNAL(Serial_Connect(QString,QString,QString,QString,QString,QString)),
59
            serialHelper, SLOT(Serial_Connect(QString,QString,QString,QString,QString,QString)));
60
    connect(serialHelper, SIGNAL(Serial_Connected()), this, SIGNAL(Serial_Connected()));
61
    connect(serialHelper, SIGNAL(Serial_Connected()), this, SLOT(Serial_LocalConnected()));
62
 
63
    // Disconnect
64
    connect(this, SIGNAL(Serial_Disconnect()), serialHelper, SLOT(Serial_Disconnect()));
65
    connect(serialHelper, SIGNAL(Serial_Disconnected()), this, SIGNAL(Serial_Disconnected()));
66
    connect(serialHelper, SIGNAL(Serial_Disconnected()), this, SLOT(Serial_LocalDisconnected()));
67
 
68
    // Transmit
69
    connect(this, SIGNAL(Serial_TransmitString(QString)), serialHelper, SLOT(Serial_TransmitString(QString)));
70
    connect(this, SIGNAL(Serial_TransmitByteArray(QByteArray)), serialHelper, SLOT(Serial_TransmitByteArray(QByteArray)));
71
 
72
    // Receive
73
    connect(serialHelper, SIGNAL(Serial_ReceivedByte(char)), this, SIGNAL(Serial_ReceivedByte(char)));
74
 
75
    // Threading
76
    connect(serialThread, SIGNAL(finished()), serialHelper, SLOT(deleteLater()));
77
    serialThread->start();
78
 
79
    // Local GUI
80
    connect(btnSerialConnect, SIGNAL(clicked()), this, SLOT(Serial_ConnectToggleBtn()));
81
    connect(btnSerialRefresh, SIGNAL(clicked()), this, SIGNAL(Serial_QueryParameters()));
82
 
83
    emit Serial_QueryParameters();
84
    Serial_LocalDisconnected();
344 Kevin 85
}
86
 
87
SerialController::~SerialController()
88
{
351 Kevin 89
    serialThread->quit();
344 Kevin 90
}
91
 
351 Kevin 92
void SerialController::Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
93
                                               QStringList stopBits, QStringList parity, QStringList flowControl)
344 Kevin 94
{
351 Kevin 95
    QString currPort = cboxSerialPort->currentText();
96
    cboxSerialPort->clear();
97
    cboxSerialPort->addItems(ports);
98
    if (currPort != "" && ports.contains(currPort))
99
        cboxSerialPort->setCurrentText(currPort);
344 Kevin 100
 
351 Kevin 101
    QString currSpeed = cboxSerialSpeed->currentText();
102
    cboxSerialSpeed->clear();
103
    cboxSerialSpeed->addItems(speeds);
104
    if (currSpeed != "") cboxSerialSpeed->setCurrentText(currSpeed);
344 Kevin 105
 
351 Kevin 106
    int currData = cboxSerialDataBits->currentIndex();
107
    cboxSerialDataBits->clear();
108
    cboxSerialDataBits->addItems(dataBits);
109
    if (currData >= 0) cboxSerialDataBits->setCurrentIndex(currData);
346 Kevin 110
 
351 Kevin 111
    int currStop = cboxSerialStopBits->currentIndex();
112
    cboxSerialStopBits->clear();
113
    cboxSerialStopBits->addItems(stopBits);
114
    if (currStop >= 0) cboxSerialStopBits->setCurrentIndex(currStop);
344 Kevin 115
 
351 Kevin 116
    int currParity = cboxSerialParity->currentIndex();
117
    cboxSerialParity->clear();
118
    cboxSerialParity->addItems(parity);
119
    if (currParity >= 0) cboxSerialParity->setCurrentIndex(currParity);
348 Kevin 120
 
351 Kevin 121
    int currFlow = cboxSerialFlowControl->currentIndex();
122
    cboxSerialFlowControl->clear();
123
    cboxSerialFlowControl->addItems(flowControl);
124
    if (currFlow >= 0) cboxSerialFlowControl->setCurrentIndex(currFlow);
344 Kevin 125
}
126
 
351 Kevin 127
void SerialController::Serial_LocalConnected()
344 Kevin 128
{
351 Kevin 129
    btnSerialConnect->setText("&Disconnect");
130
//    cboxSerialPort->setEnabled(false);
131
//    cboxSerialSpeed->setEnabled(false);
132
//    cboxSerialDataBits->setEnabled(false);
133
//    cboxSerialStopBits->setEnabled(false);
134
//    cboxSerialParity->setEnabled(false);
135
//    cboxSerialFlowControl->setEnabled(false);
136
//    btnSerialRefresh->setEnabled(false);
137
    btnSerialRefresh->hide();
138
    labelSerialPort->hide();
139
    labelSerialSpeed->hide();
140
    labelSerialDataBits->hide();
141
    labelSerialStopBits->hide();
142
    labelSerialParity->hide();
143
    labelSerialFlowControl->hide();
144
    cboxSerialPort->hide();
145
    cboxSerialSpeed->hide();
146
    cboxSerialDataBits->hide();
147
    cboxSerialStopBits->hide();
148
    cboxSerialParity->hide();
149
    cboxSerialFlowControl->hide();
150
    emit UpdateStatus("Serial: Connected");
344 Kevin 151
}
152
 
351 Kevin 153
void SerialController::Serial_LocalDisconnected()
344 Kevin 154
{
351 Kevin 155
    btnSerialConnect->setText("&Connect");
156
//    cboxSerialPort->setEnabled(true);
157
//    cboxSerialSpeed->setEnabled(true);
158
//    cboxSerialDataBits->setEnabled(true);
159
//    cboxSerialStopBits->setEnabled(true);
160
//    cboxSerialParity->setEnabled(true);
161
//    cboxSerialFlowControl->setEnabled(true);
162
//    btnSerialRefresh->setEnabled(true);
163
    btnSerialRefresh->show();
164
    labelSerialPort->show();
165
    labelSerialSpeed->show();
166
    labelSerialDataBits->show();
167
    labelSerialStopBits->show();
168
    labelSerialParity->show();
169
    labelSerialFlowControl->show();
170
    cboxSerialPort->show();
171
    cboxSerialSpeed->show();
172
    cboxSerialDataBits->show();
173
    cboxSerialStopBits->show();
174
    cboxSerialParity->show();
175
    cboxSerialFlowControl->show();
176
    emit UpdateStatus("Serial: Disconnected");
344 Kevin 177
}
178
 
351 Kevin 179
void SerialController::Serial_ConnectToggleBtn()
344 Kevin 180
{
351 Kevin 181
    if (serialHelper->connected) {
182
        emit Serial_Disconnect();
183
    } else {
184
        if (cboxSerialPort->currentText() != "" && cboxSerialSpeed->currentText() != "") {
185
            emit Serial_Connect(cboxSerialPort->currentText(), cboxSerialSpeed->currentText(), cboxSerialDataBits->currentText(),
186
                                cboxSerialStopBits->currentText(), cboxSerialParity->currentText(), cboxSerialFlowControl->currentText());
344 Kevin 187
        }
188
    }
189
}
351 Kevin 190
 
191
//void SerialController::Serial_ProcessIncomingData()
192
//{
193
//    char tmpBuffer[SERIAL_BUFFER_SIZE];
194
//    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
195
//    while (len > 0) {
196
//        for (int i = 0; i < len; i++) {
197
//            // Save received data into local buffer
198
//            bufferIn[bufferInIndex] = tmpBuffer[i];
199
 
200
//            // If newline char is received, end current string
201
//            if (tmpBuffer[i] == '\n') {
202
//                if (bufferInOverflow)
203
//                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
204
//                else
205
//                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
206
//                bufferInOverflow = false;
207
//                bufferInIndex = 0;
208
//                emit Serial_ReceivedString(currString);
209
//            } else {
210
//                bufferInIndex++;
211
//            }
212
 
213
//            // If received string is larger than our serial buffer, append to previous data
214
//            if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
215
//                bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
216
//                if (bufferInOverflow)
217
//                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
218
//                else
219
//                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
220
//                bufferInOverflow = true;
221
//                bufferInIndex = 0;
222
//            }
223
//        }
224
//        // Check if there is more data to be read from the serial port
225
//        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
226
//    }
227
//}