Question

I have a custom made hardware pannel that I need to connect to.

Using QAbstractSocket class, I managed to connect to it and communicate with it.

But the issue is that if I unplug the ethernet cable after the connected state is reached, no update is made to the state.

So i need a way to "ping" it every X seconds to make sure the connection is not lost.

What would be the best way to do so?

  • For example, a 2nd thread created once it's connected that will frenquently "ping" the device?

This question is not related to the ping part but to the every X seconds part.

Was it helpful?

Solution

I don't know if that's the best way but here is what I used.

QTimer is your fiend in this situation. You don't need to get a different thread for this as it is asynchronous (Qt documentation strongly advises not to use threads in this case here for example)

I used QTimer to be able to trigger a specific method every interval of time. (you can set the QTimer as singleshot)

I used another QTimer, to wait for the reply, once the first timed out.

By using the signal/slots etc... It was really easy to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top