In a previous experiment, I made an application (well borrowed) with help of C++ Builder, but I really did not enjoy/like it much. So, I was exploring the web again and found an article. It was much more logical and simple to me, so I decided to give it a shot.
Firstly, we need the Qt SDK. Then we should take sources from here: qt.gitorious.org/qt/qt/trees/4.7/src/corelib/kernel files qwineventnotifier_p.h and qwineventnotifier_p.cpp and place them QtSDKDesktopQt4.7.4mingwincludeQtCoreprivate
Also, download the library dedicated to working with a COM port. Good enough, now we are prepared.
The setup and some details are all could be read by before mentioned links, I will describe here only what I had made.
Task #1. Connect to the proper COM port:
Firstly, some drawing jobs:
It is pretty minimalistic: the dropout menu with the ports list, the dropout menu with a speed choice, and the opening button. Moving directly to the code, which called when the user pressed the Open button:
void RsTest::on_pbOpen_clicked() { if (this->serial->isOpen()) { serial->close(); ui->cbBaud->setEnabled(true); //Разрешаем выбирать скорость ui->cbPort->setEnabled(true); //Разрешаем выбирать порт ui->pbOpen->setText("Open"); //Текст на кнопке } else { serial->setDeviceName(ui->cbPort->currentText()); // порт на открытие if (serial->open(AbstractSerial::ReadWrite)) { qDebug() << "Port " << serial->deviceName() << " opened in " << serial->openMode() << " mode"; //вывод в режиме отладки serial->setBaudRate(ui->cbBaud->currentText()); // Скорость serial->setDataBits(AbstractSerial::DataBits8); serial->setParity(AbstractSerial::ParityNone); serial->setStopBits(AbstractSerial::StopBits1); serial->setFlowControl(AbstractSerial::FlowControlOff); ui->cbBaud->setEnabled(false); ui->cbPort->setEnabled(false); ui->pbOpen->setText("Close"); ui->pbSend->setEnabled(true); } else { qDebug() << "Error opened serial device " << serial->deviceName(); //Ошибка открытия } } }
the result:
Task 2. Teach the GUI to send some bytes to the port.
To make things simpler I took the uC code from the previous experiment. So, when the user press 1, 2, 3, or 4 keys the devboard will be firing up the corresponding LEDs. I wanted to expand the functionality a bit and allowed sending of the any symbol, changing a bit the form:
There are two new elements on the form: lineEdit and pushButton, the first one is for the text entry to be sent, and the second one obviously sending the content of the lineEdit to the port.
void RsTest::on_pbSend_clicked() { QByteArray data; //Переменная для наших данных data.clear(); //Очищаем массив data.append(ui->leSend->text()); //Добавляем данные из строки в массив serial->write(data); //отсылаем данные в порт }
testing:
Task 3. To teach our soft to read the data out of the port
Firstly we need to determine where we will put the data, there are many choices can be done, I used qDebug + change TextLabel, modifying the GUI:
Hooking up the level converter to have the uart device, then shorting RX and TX. This will create a loop: whatever is sent to the TX will immediately appear on the RX line, this is a way to test uart communiactions.
The code:
void RsTest::serialRecieve() //прерывание по попаданию каких либо данных в порт { QByteArray temp_data = serial->readAll(); //readAll() стандартная функция abstractserial.h qDebug() << "Reading data is: " << temp_data; //вывод в отладочном режиме ui->labelRead->setText(temp_data); //вывод на текстовую метку }
testing:
The last thing to remind: if your Qt is not a static version, then you need to add a couple of files to the folder with an app, which you always can find there – %qtdir%\Desktop\Qt\4.7.4\mingw\bin\