Serial port expanding enumerator example
-
Hi!
I am trying to expand the enumerator example and create a Serial Port object. I try then to invoke some QSerialPort member-functions using that object but I get some errors. Eventually I would like to send some data via COM4. See image: http://s29.postimg.org/yq4mgwe2f/Build_Errors_1_1_16.jpg -
Hi
You need to include
#include <QtSerialPort>
also
You have only QSerialPortInfo so It dont know serial port
in your code and that it was it means with incomplete type. :) -
I have added some lines to the code and got some errors. See image
http://s22.postimg.org/g6isurxbl/Build_Errors_1_6_16.jpgIs the following sequence correct? 1)set port name 2)open the port 3) write data 4) close the port
serialPort.setPortName(COM4);
serialPort.open(QIODevice::WriteOnly);
serialPort.writeData(0xAA, 6);
serialPort.close();thanks
-
Hi,
- COM4 is not a string, put it between quotes
- use write and with a QByteArray
Also, add a check that open is successful. That way you are sure the port is open and the rest of the code should work.
-
@SGaist Thanks
I added the following lines of code and now runs successfully. see lines 34-55
http://s8.postimg.org/j8fvvd3hx/Build_Errors_1_7_16.jpgThe check I have to verify that port COM4 is open gives false so the program stacks at while loop.
Do I need to use the setPort() as well?
What does the setPortName() does? -
That's not a check, it's just a blocking while loop. If it can't be opened the first time, the chances are unlikely that it will become later.
Just use an if and print the error string so you will know what is happening.
-
@SGaist
http://s12.postimg.org/gvao8d6st/Build_Errors_1_8_16.jpgI use a usb to serial TTL FTDI cable (TTL-232RG-VIP-WE) connected to COM4.
It seems that the port doesn't open.
How to print the error string? -
qDebug() << serialPort.errorString();
-
-
You're opening it twice
-
@SGaist
http://s22.postimg.org/n3cplzer5/Build_Errors_1_8_16_d.jpg
I commented line 49 and now it opens the port and transmits the data.(data need to be captured in scope)
I will mark this as an answer.
I will be creating another question in order to design a minimum gui that selects baud rate, stop bits and data bits and then transmits data that have been placed to a text box. Probably I will be needing your help. I have been reading for the last year C++ and recently I have ordered book by Jasmin Blanchette.
thanks again. -
hi
The Terminal example that comes with Qt has dialog for selecting
comport settings.
Its minimal sample and sounds a lot like what u need.
http://doc.qt.io/qt-5/qtserialport-terminal-example.html