Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I detect usb removed when serial port is open?
Forum Updated to NodeBB v4.3 + New Features

How can I detect usb removed when serial port is open?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 379 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheCeylann
    wrote on last edited by
    #1

    There is a datalogger that measures temperature and humidity. I am reading the recorded temperature and humidity data using QSerialPort. I want to terminate the serial port connection if the usb is removed from my computer while my Serial Port is open. How can I detect that the USB has been removed? I would be glad if you help me in this regard..

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @TheCeylann,

      Seems like you want to watch for QSerialPort::ResourceError errors (among others):

      QSerialPort::ResourceError An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.

      So perhaps something like:

      connect(&serialPort, &QSerialPort::errorOccurred, this, [](const QSerialPort::SerialPortError error) {
        if (error == QSerialPort::ResourceError) {
          // handle possible unplugged event.
        }
      });
      

      Cheers.

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #3

        The ResourceError may trigger or may not trigger, it depends on the OS, on the driver type and version.

        A best way is to write an own class watcher based on the SetupAPI on Windows, or the Udev on Linux, (but the MacOSX has something similar, I think), which will subscribe to the detach events for the desired device class (USB or SEIRIAL). You can use a GOOGLE to search how to use that APIs.

        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Another possibility is to use KDE's Solid framework. It is cross-platform and should allow you to implement what @kuzulis suggests without going low level.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved