![]() ![]() gui will not redraw etc while code is running and that is If you really find it hard to get work, you can use the blocking api of QSrialportīut do note that this LAGS the GUI. I know it seems a bit complicated but thats how async programming is. Then you can send signal to mainWindow that data is ready.Īnd it that slot in main window, you can then use the data. For those unfamiliar with Qt, QSocketNotifier basically sets a callback for the SIGIO signal and then calls a function in my program from that callback (albeit via Qts events and also signal/slot mechanism). QT SERIAL PORT READ AND WRITE FULLThen when data contains a CR, we know you got full set. What I am doing is to use a QSocketNotifier object (from Qt libraries) to notify my program when there is data to be read on the serial port. Output += QIODevice::readAll() // note the plus In getData you read and append to a buffer Serialport issues one or more readyRead signals its not called by serialport due to incoming data GetData() // you just call it so might be nothing to read yet. Its asynchronous so i think the issue lies here QByteArray MotorPort::getresponse() &H02 &H00 &H09 &H35 &H32 &H08 &H99 I have a demo programm and there i can see what codes will be written to the serial port. I have to write the following to the serialport. ![]() QT SERIAL PORT READ AND WRITE HOW TOThe reference software says encoder's position is 3.032mm. I have a problem, i dont know how to write the correct way to the serial port Hex code's. Ui->statusencoder->append(QString::number(array)) Īfter running this code, My QTextBrowser(statusencoder) shows like this one success Int array //I tried to choose return value type as intįor (int i=0 istatusencoder->append(QString::number(array)) Ui->statusencoder->append(QString(data)) Output = QIODevice::readAll() // Output(QByteArray) is private member of MotorPortĪnd in mainwindow.cpp bool open = rls->openMotorPort("COM5") MotorPort::MotorPort(QSerialPort* parent) : QSerialPort(parent)Ĭonnect(this, SIGNAL(readyRead()), this, SLOT(getData())) īool MotorPort::openMotorPort(const QString &portName) char ch1(63) //63 means "?" in ASCII table.Ĭan you let me know how to send ASCII character? Also, Is QByteArray data = rls-> readAll() enough to read the response?Īfter reading 's answer, I added readyRead. I try to make code like this one, but It doesn't work. The problem is I don't know what to put in the rls->write(). Ui->statusencoder->setText(QString(data)) //statusencoder is QTextBrowser Rls->openMotorPort("COM5") //I Checked my PC and device are connected! Void MotorPort::openMotorPort(const QString &portName)īased on this I wrote code like below. The picture I attach is the some parts of the document. I communicate with my device through serialport, So I use QSerialport. I'm making GUI which displays encoder's position on the QTextBrowser. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |