Subversion Repositories Code-Repo

Rev

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
362 Kevin 6
    btnConnect = new QPushButton("&Connect");
7
    btnRefresh = new QPushButton("&Refresh");
8
    labelPort = new QLabel("Serial Port:");
9
    labelSpeed = new QLabel("Baud Rate:");
10
    labelDataBits = new QLabel("Data:");
11
    labelStopBits = new QLabel("Stop:");
12
    labelParity = new QLabel("Parity Bit:");
13
    labelFlowControl = new QLabel("Flow Control:");
14
    cboxPort = new QComboBox();
15
    cboxPort->setMinimumWidth(80);
16
    cboxSpeed = new QComboBox();
17
    cboxSpeed->setEditable(true);
18
    cboxSpeed->setValidator(new QIntValidator(0, 100000000, this));
19
    cboxDataBits = new QComboBox();
20
    cboxDataBits->setMaximumWidth(60);
21
    cboxStopBits = new QComboBox();
22
    cboxStopBits->setMaximumWidth(60);
23
    cboxParity = new QComboBox();
24
    cboxFlowControl = new QComboBox();
25
    cboxFlowControl->setMinimumWidth(70);
354 Kevin 26
 
27
    QGridLayout *serialSettingsLayout = new QGridLayout();
362 Kevin 28
    serialSettingsLayout->addWidget(btnConnect, 0, 0);
29
    serialSettingsLayout->addWidget(labelPort, 0, 1);
30
    serialSettingsLayout->addWidget(cboxPort, 0, 2);
31
    serialSettingsLayout->addWidget(labelDataBits, 0, 3);
32
    serialSettingsLayout->addWidget(cboxDataBits, 0, 4);
33
    serialSettingsLayout->addWidget(labelParity, 0, 5);
34
    serialSettingsLayout->addWidget(cboxParity, 0, 6);
354 Kevin 35
 
362 Kevin 36
    serialSettingsLayout->addWidget(btnRefresh, 1, 0);
37
    serialSettingsLayout->addWidget(labelSpeed, 1, 1);
38
    serialSettingsLayout->addWidget(cboxSpeed, 1, 2);
39
    serialSettingsLayout->addWidget(labelStopBits, 1, 3);
40
    serialSettingsLayout->addWidget(cboxStopBits, 1, 4);
41
    serialSettingsLayout->addWidget(labelFlowControl, 1, 5);
42
    serialSettingsLayout->addWidget(cboxFlowControl, 1, 6);
354 Kevin 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
 
362 Kevin 52
    connect(this, SIGNAL(QueryParameters()), serialHelper, SLOT(QueryParameters()));
53
    connect(serialHelper, SIGNAL(UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
54
            this, SLOT(UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
354 Kevin 55
 
362 Kevin 56
    connect(this, SIGNAL(Connect(QString,QString,QString,QString,QString,QString)),
57
            serialHelper, SLOT(Connect(QString,QString,QString,QString,QString,QString)));
364 Kevin 58
    connect(serialHelper, SIGNAL(Connected(bool)), this, SIGNAL(Connected(bool)));
59
    connect(serialHelper, SIGNAL(Connected(bool)), this, SLOT(LocalConnected(bool)));
354 Kevin 60
 
362 Kevin 61
    connect(this, SIGNAL(Disconnect()), serialHelper, SLOT(Disconnect()));
354 Kevin 62
 
362 Kevin 63
    connect(this, SIGNAL(TransmitByteArray(QByteArray)), serialHelper, SLOT(TransmitByteArray(QByteArray)));
354 Kevin 64
 
362 Kevin 65
    connect(serialHelper, SIGNAL(ReceivedByte(QByteArray)), this, SIGNAL(ReceivedByte(QByteArray)));
354 Kevin 66
 
67
    connect(serialThread, SIGNAL(finished()), serialHelper, SLOT(deleteLater()));
68
    serialThread->start();
69
 
362 Kevin 70
    connect(btnConnect, SIGNAL(clicked()), this, SLOT(ConnectToggleBtn()));
71
    connect(btnRefresh, SIGNAL(clicked()), this, SIGNAL(QueryParameters()));
354 Kevin 72
 
362 Kevin 73
    emit QueryParameters();
364 Kevin 74
    LocalConnected(false);
354 Kevin 75
}
76
 
77
SerialWidget::~SerialWidget()
78
{
79
    serialThread->quit();
80
}
81
 
362 Kevin 82
void SerialWidget::UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
354 Kevin 83
                                               QStringList stopBits, QStringList parity, QStringList flowControl)
84
{
362 Kevin 85
    QString currPort = cboxPort->currentText();
86
    cboxPort->clear();
87
    cboxPort->addItems(ports);
354 Kevin 88
    if (currPort != "" && ports.contains(currPort))
362 Kevin 89
        cboxPort->setCurrentText(currPort);
354 Kevin 90
 
362 Kevin 91
    QString currSpeed = cboxSpeed->currentText();
92
    cboxSpeed->clear();
93
    cboxSpeed->addItems(speeds);
94
    if (currSpeed != "") cboxSpeed->setCurrentText(currSpeed);
354 Kevin 95
 
362 Kevin 96
    int currData = cboxDataBits->currentIndex();
97
    cboxDataBits->clear();
98
    cboxDataBits->addItems(dataBits);
99
    if (currData >= 0) cboxDataBits->setCurrentIndex(currData);
354 Kevin 100
 
362 Kevin 101
    int currStop = cboxStopBits->currentIndex();
102
    cboxStopBits->clear();
103
    cboxStopBits->addItems(stopBits);
104
    if (currStop >= 0) cboxStopBits->setCurrentIndex(currStop);
354 Kevin 105
 
362 Kevin 106
    int currParity = cboxParity->currentIndex();
107
    cboxParity->clear();
108
    cboxParity->addItems(parity);
109
    if (currParity >= 0) cboxParity->setCurrentIndex(currParity);
354 Kevin 110
 
362 Kevin 111
    int currFlow = cboxFlowControl->currentIndex();
112
    cboxFlowControl->clear();
113
    cboxFlowControl->addItems(flowControl);
114
    if (currFlow >= 0) cboxFlowControl->setCurrentIndex(currFlow);
354 Kevin 115
}
116
 
364 Kevin 117
void SerialWidget::LocalConnected(bool connected)
354 Kevin 118
{
364 Kevin 119
    if (connected) {
120
        btnConnect->setText("&Disconnect");
121
        btnRefresh->hide();
122
        labelPort->hide();
123
        labelSpeed->hide();
124
        labelDataBits->hide();
125
        labelStopBits->hide();
126
        labelParity->hide();
127
        labelFlowControl->hide();
128
        cboxPort->hide();
129
        cboxSpeed->hide();
130
        cboxDataBits->hide();
131
        cboxStopBits->hide();
132
        cboxParity->hide();
133
        cboxFlowControl->hide();
134
    } else {
135
        btnConnect->setText("&Connect");
136
        btnRefresh->show();
137
        labelPort->show();
138
        labelSpeed->show();
139
        labelDataBits->show();
140
        labelStopBits->show();
141
        labelParity->show();
142
        labelFlowControl->show();
143
        cboxPort->show();
144
        cboxSpeed->show();
145
        cboxDataBits->show();
146
        cboxStopBits->show();
147
        cboxParity->show();
148
        cboxFlowControl->show();
149
    }
354 Kevin 150
}
151
 
362 Kevin 152
void SerialWidget::ConnectToggleBtn()
354 Kevin 153
{
154
    if (serialHelper->connected) {
362 Kevin 155
        emit Disconnect();
354 Kevin 156
    } else {
362 Kevin 157
        if (cboxPort->currentText() != "" && cboxSpeed->currentText() != "") {
158
            emit Connect(cboxPort->currentText(), cboxSpeed->currentText(), cboxDataBits->currentText(),
159
                                cboxStopBits->currentText(), cboxParity->currentText(), cboxFlowControl->currentText());
354 Kevin 160
        }
161
    }
162
}
163
 
164
//void SerialController::Serial_ProcessIncomingData()
165
//{
166
//    char tmpBuffer[SERIAL_BUFFER_SIZE];
167
//    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
168
//    while (len > 0) {
169
//        for (int i = 0; i < len; i++) {
170
//            // Save received data into local buffer
171
//            bufferIn[bufferInIndex] = tmpBuffer[i];
172
 
173
//            // If newline char is received, end current string
174
//            if (tmpBuffer[i] == '\n') {
175
//                if (bufferInOverflow)
176
//                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
177
//                else
178
//                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
179
//                bufferInOverflow = false;
180
//                bufferInIndex = 0;
181
//                emit Serial_ReceivedString(currString);
182
//            } else {
183
//                bufferInIndex++;
184
//            }
185
 
186
//            // If received string is larger than our serial buffer, append to previous data
187
//            if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
188
//                bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
189
//                if (bufferInOverflow)
190
//                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
191
//                else
192
//                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
193
//                bufferInOverflow = true;
194
//                bufferInIndex = 0;
195
//            }
196
//        }
197
//        // Check if there is more data to be read from the serial port
198
//        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
199
//    }
200
//}