Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Detecting QSerialPort disconnected in middle of program execution



  • Hello everyone,

    I need to detect and create exception if somebody detach the serialport when the program is running.
    I went through number of solution but there any inbuild feature to do the same.


  • Moderators

    @kira

    please define this a bit more

    detach the serialport



  • @j-hilk : Sure.
    If suppose i have connected a serial device like ardunio to my COM Port and i fetching data when user clicks the button. But if my program is running and somebody removes the cable can i generate run time error acknowledgement that the cable is removed.
    Please let me know in case of any other clarification


  • Moderators

    @kira

    well, in that case, not really.

    You can potentially detect, when the OS looses the Port from the internal list, via for example QSerialPortInfo::availablePorts(), But you would have to regulary poll that.

    Besides that, there is
    https://doc.qt.io/qt-5/qserialport.html#errorOccurred

    that should fire, when you try to access the port and nothing answers



  • UPD: It is possible, the QSP::errorOccurred() signal can not be even triggered in some cases (e.g. it depends on a serial port driver, on OS and on a solar flashes). In some cases, the file descriptor of a device will not report about the errors at the I/O operations when a device is pulled out. I say about the USB/serial devices... There are only one way to do this (to watch for the USB devices attach/detach) - it is to create a separate 'watcher' class himself. If you use Windows, then you can watch for the appropriate system WM messages (Google will help you). On Linux you can use the udev monitor API, of course, if the udev is installed (Google will help you). On OSX you can use similar system-specific mechanism (Google will help you). Earlier there are a separate library, called QextSerialPort, where was a class QExtSerialPortWatcher (as I remember). You can try to take some code from there. So, no any good cross-platform solution to do that (that's why QSP has not any separate 'watcher' class).



  • @kuzulis :thanks


Log in to reply