read data RS232 without polling - c++

i read data from RS232 in a for loop.this has no good performance.i want don't have a for loop instead of that there was a thing like event that when data is receiving it fire. is this possible? for example when data is receiving from COM16 event firing.

It looks like this project is being added into Qt 5.1 already in Qt 5!
http://qt-project.org/wiki/QtSerialPort
http://doc-snapshot.qt-project.org/qt5-stable/qtserialport/qtserialport-index.html
The examples show two different ways to get the information out of the Serial port:
This one uses a signal slot connection:
http://doc-snapshot.qt-project.org/qt5-stable/qtserialport/terminal.html
This one uses polling in a loop:
http://doc-snapshot.qt-project.org/qt5-stable/qtserialport/blockingslave.html
This is a different one, but can do all the same things. I've used this one before with success. It has a signal slot available in the examples.
https://code.google.com/p/qextserialport/
Hope that helps.

Related

How to modify QWebsocket timers in Qt

I have a simple Client application (using QWebSocket) that wants to connect to my server application (i.e. QWebSocketServer).
When I open a connection to a webSocketServer that is down/unavailable, my webSocket fires a "disconnectd" signal after 30 sec.
This is good as it helps me to understand that the server is down/unavailable so I can retry or warn the user about the problem.
If the link between the client and server fails the same thing happens. i.e. after writing (sendBinaryMessage) to the webSocket causes the disconnected signal to be fired after 30 secs.
I would like to know what are the default timers in QWebSocket and how I can modify them?
Where can I find such information/documentation? The Qt documentation on webSockets does not mention this behaviour at all! Should I read the code or ...?!
Thanks in advance
I doubt that any of these timers are part of Qt; these timers exist as part of the underlying operating system's implementation of TCP/IP. A socket waiting for a connection to time out will eventually go bad if the remote end does not respond. Same if a sent data is not acknowledge after a reasonable amount of time.
Qt however does everything asynchronously and makes use of signals and slots to notify you when something has happened. This means that if you want to shorten a timeout the simplest way to do this is using a QTimer that runs in parallel to you QAbstractSocket, if the timer times out before the socket signals its response, you can then take appropriate action.
Failing that, there may be some socket options that allow you to set the various timeouts on your TCP Connection to your liking.
From QWebSocket:
This class was modeled after QAbstractSocket.
QAbstractSocket in turn inherits from QIODevice.
The documentation of these classes have some information about timeouts.
Specifically you can see the default of 30 seconds pop up here and there.
Another place to look at is QObject's documentation (QWebSocket inherits it). Perhaps by overriding QObject's timer-related virtual functions you can somehow get in between these mechanisms and perhaps change the timeout.
Sorry to not be of more help.

Qt c++ signal slots - necessary to clear signals?

sorry, I can`t be mor speciic, because I am stuck in a jumble of classes, and several signal/slots.
In my project I use several signal-slots. Now I have the impression that depending on how often I start a routine that emits a signal my slot is run several times.
For the first rum my Slot is run once; In the second run it is run twice.... When I close my program I start again with running it once.
Is there a need to somehow finish/end/delete a signal after it is sent ?
Thank you
Take a look to the Qt::UniqueConnection flag.
You're connecting signals to slots in reaction to events. This causes duplicate connections as the events are repeated. In most cases, this is a bug. Usually you want to set up connections in class constructors, or otherwise when new objects are created and added to your system.
The unique connection will mask the problem, but not solve it - the solution is to move the connect statements to locations where they won't be re-executed.
A a signal stays connected, until either the disconnect() is used, or the sender or receiver is deleted.
So each signal/slot pair has to beconnected only once, and then every time the signal is emitted, the slot gets called.

How to know when QSerialPort is ready for more data in Qt5?

Is there some way to be notified when my serial device is ready to receive more data?
I am implementing a program that feeds commands to a servo controller over serial using the asynchronous API of QSerialPort. The program works, but it seems that I am experiencing congestion.
In my application I keep a registry of most updated servo position that I wish to synchronize with the controller with as low latency as possible. My attempt at pushing new data to the serial port as quickly as time lets me leads to what I think is a buildup of data that makes the communication break down altogether.
My question is, can I be notified when the serial interface is ready to receive new data somehow while using QSerialPort in asynchronous mode?
The only other option I see is to use synchronous mode and call waitForBytesWritten() But I really don't want to do that.
NOTE: I have studied the serial port examples in the Qt5 documentation carefully, but the example for asynchronous write does not provide the information I need, as it perform a one-shot send before quitting.
You could listen to the signal bytesWritten and check bytesToWrite to determine if the write buffer is empty. Both are inherited from QIODevice.

Qt TCP Socket reading issue

I try to read the TCP port with Qt libraries and a GUI Program.
The problem is that when I use this code to read the socket:
QTcpSocket *a = new QTcpSocket();
a->connectToHost("127.0.0.1", 5000);
a->read(data,LEN);
it's not working and I need to put an extra function:
a->waitForReadyRead(1)
but this functions slows my Program, I use a pthread to run this code continuously. I have a question, how can I speed up my program and also to read the data successfully?
You need to use Qt event loop and connect 'data available' event signal in order to read socket data asynchronously. Connection syntax in your case may look like this:
QObject::connect(s, &QTcpSocket::readyRead, yourReaderFunction);
Your program loop should contain a call to event processing function QApplication::processEvents(), or you can use built-in loop QApplication::exec().
The signals and slots mechanism is a core feature of Qt, and you need to understand and use it properly in order to interact with most of Qt classes:
Signal and slot basics
Qt5 new signal and slot syntax

Best way to get a query result

I'm developing an application that gets large images from an Internet server which is the best way to download this images, without freeze the entire application? I mean background download. I have thought about download it in another thread.
Yes, you need to spawn another thread to do the network communication, and then when it is finished doing it's reading, you can use a volatile boolean flag to indicate that the work is complete and the main/application thread can take the data and incorporate it. The data may be "part" of an image if you want to show the image coming in piece by piece (as a browser does).
A background thread will work, but it's tricky to get right and not usually necessary... Qt4 makes it very easy to do non-blocking I/O in the main thread using the QTcpSocket class -- basically you connect the QTcpSocket object's readReady() signal to a slot it your program, and have your slot read out the newly available data from the QTcpSocket when it is called. For an example, have a look at the fortuneclient example in the Qt examples directory ($QTDIR/examples/network/fortuneclient).