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. Windows (10) and QSerialPort
QtWS25 Last Chance

Windows (10) and QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportwindows10
12 Posts 4 Posters 2.3k Views
  • 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 2 Oct 2020, 20:13 last edited by
    #1

    Hi all -

    One of the "features" of Windows 10 is that COM ports are created and deleted dynamically -- when you plug something in, you get a new port; when you unplug it, it goes away. (At least this is the behavior when using the FTDI-style USB-serial interface.)

    This is a bit of a problem for me, in that I'd like to be notified when a new device is plugged in, but I don't know of any way to cause this to happen automatically -- in order for a QSerialPort device to emit a signal, it of course first needs to be created, and I can't create it without knowing it's there.

    I could put an interval-based poll into my worker thread, where every so often it scans for new devices. I think this will work, but it will be kind of clunky. Can anyone think of a better mechanism?

    Thanks...

    G 2 Replies Last reply 2 Oct 2020, 21:40
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 2 Oct 2020, 21:03 last edited by
      #2

      Are you stuck with windoze? The udev rules in Linux handle this once you understand enough of the udev rules alchemy.

      M 1 Reply Last reply 2 Oct 2020, 21:08
      0
      • K Kent-Dorfman
        2 Oct 2020, 21:03

        Are you stuck with windoze? The udev rules in Linux handle this once you understand enough of the udev rules alchemy.

        M Offline
        M Offline
        mzimmers
        wrote on 2 Oct 2020, 21:08 last edited by
        #3

        @Kent-Dorfman sadly, yes. Our user community is the prototypical Fortune 500 company, heavily imbued with all kind of Redmond-ware.

        I've given it some more thought, and I don't see any way around this Catch-22. Unless one of our resident Qt demiurges can think of something, I'll probably just go the polling route.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 2 Oct 2020, 21:10 last edited by
          #4

          gotcha...there is probably a udev type event system in windoze but unless you are a high priest in the church of Redmond you wouldn't know about it.

          1 Reply Last reply
          1
          • M mzimmers
            2 Oct 2020, 20:13

            Hi all -

            One of the "features" of Windows 10 is that COM ports are created and deleted dynamically -- when you plug something in, you get a new port; when you unplug it, it goes away. (At least this is the behavior when using the FTDI-style USB-serial interface.)

            This is a bit of a problem for me, in that I'd like to be notified when a new device is plugged in, but I don't know of any way to cause this to happen automatically -- in order for a QSerialPort device to emit a signal, it of course first needs to be created, and I can't create it without knowing it's there.

            I could put an interval-based poll into my worker thread, where every so often it scans for new devices. I think this will work, but it will be kind of clunky. Can anyone think of a better mechanism?

            Thanks...

            G Offline
            G Offline
            Gojir4
            wrote on 2 Oct 2020, 21:40 last edited by
            #5

            @mzimmers hi, I think you can use qabstractnativeeventfilter and look for WM_DEVICECHANGE Windows event, and then refresh your devices list at this moment. Here is an example here https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilter

            M 1 Reply Last reply 2 Oct 2020, 21:43
            3
            • G Gojir4
              2 Oct 2020, 21:40

              @mzimmers hi, I think you can use qabstractnativeeventfilter and look for WM_DEVICECHANGE Windows event, and then refresh your devices list at this moment. Here is an example here https://forum.qt.io/topic/32640/help-with-qabstractnativeeventfilter

              M Offline
              M Offline
              mzimmers
              wrote on 2 Oct 2020, 21:43 last edited by
              #6

              @Gojir4 oh, that looks promising. I'll experiment with it and report back. Thanks!

              1 Reply Last reply
              0
              • M mzimmers
                2 Oct 2020, 20:13

                Hi all -

                One of the "features" of Windows 10 is that COM ports are created and deleted dynamically -- when you plug something in, you get a new port; when you unplug it, it goes away. (At least this is the behavior when using the FTDI-style USB-serial interface.)

                This is a bit of a problem for me, in that I'd like to be notified when a new device is plugged in, but I don't know of any way to cause this to happen automatically -- in order for a QSerialPort device to emit a signal, it of course first needs to be created, and I can't create it without knowing it's there.

                I could put an interval-based poll into my worker thread, where every so often it scans for new devices. I think this will work, but it will be kind of clunky. Can anyone think of a better mechanism?

                Thanks...

                G Offline
                G Offline
                Gojir4
                wrote on 2 Oct 2020, 21:45 last edited by
                #7

                @mzimmers can I ask what is your goal ?

                M 1 Reply Last reply 2 Oct 2020, 21:46
                0
                • G Gojir4
                  2 Oct 2020, 21:45

                  @mzimmers can I ask what is your goal ?

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 2 Oct 2020, 21:46 last edited by
                  #8

                  @Gojir4 I'm just looking for a way to maintain a list of our products that are connected to the PC. Step one is to be notified of a connection, then I send it a discovery message. If it responds, I know it's one of ours, and I add it to the list.

                  G 1 Reply Last reply 2 Oct 2020, 21:49
                  0
                  • M mzimmers
                    2 Oct 2020, 21:46

                    @Gojir4 I'm just looking for a way to maintain a list of our products that are connected to the PC. Step one is to be notified of a connection, then I send it a discovery message. If it responds, I know it's one of ours, and I add it to the list.

                    G Offline
                    G Offline
                    Gojir4
                    wrote on 2 Oct 2020, 21:49 last edited by
                    #9

                    @mzimmers ok, that's very interesting. Hope it will work

                    M 1 Reply Last reply 3 Oct 2020, 13:58
                    0
                    • K Offline
                      K Offline
                      kuzulis
                      Qt Champions 2020
                      wrote on 3 Oct 2020, 06:27 last edited by kuzulis 10 Mar 2020, 06:33
                      #10

                      Look on this.

                      Shortly: you can subscribe on some events from the specific classes of devices (e.g. when the device appeared or disappeared in the system) using Win32 API such as SetupAPI.

                      One of the "features" of Windows 10 is that COM ports are created and deleted dynamically

                      It present on all NT-based Windows, it is Plug & Play.. No? :).

                      1 Reply Last reply
                      2
                      • G Gojir4
                        2 Oct 2020, 21:49

                        @mzimmers ok, that's very interesting. Hope it will work

                        M Offline
                        M Offline
                        mzimmers
                        wrote on 3 Oct 2020, 13:58 last edited by
                        #11

                        @Gojir4: I'm getting close on this. Here's a cleaned-up version of the example:

                        class MyNativeEventFilter: public QAbstractNativeEventFilter
                        {
                        public:
                            virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result)
                            Q_DECL_OVERRIDE;
                        Q_SIGNALS:
                            void notify();
                        };
                        bool MyNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
                        {
                            Q_UNUSED(eventType) // unneeded as long as we're only running on Windows
                            Q_UNUSED(result)
                            MSG *msg = (MSG *)(message);
                        
                            if ((msg->message == WM_DEVICECHANGE)
                               && (msg->wParam == DBT_DEVNODES_CHANGED))
                            {
                                qDebug() << "we hit our event!";
                                emit notify();
                            }
                            return false;
                        }
                        

                        If I comment out the emit statement, it runs and traps the device connect/disconnect (verified in the debugger). But, I can't compile with the emit -- I get an undefined reference. This is probably more of a C++ issue than a Qt issue, but can someone tell me what I'm doing wrong? The example is interesting, but not very useful if it can't notify other QObjects in my application.

                        Thanks...

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mzimmers
                          wrote on 5 Oct 2020, 01:11 last edited by
                          #12

                          Here's my solution for this, in case anyone's interested:

                          The header file:

                          #include <windows.h>
                          #include <dbt.h>
                          
                          #include <QObject>
                          #include <QAbstractNativeEventFilter>
                          
                          class DeviceEventFilter: public QObject, public QAbstractNativeEventFilter
                          {
                              Q_OBJECT
                          public:
                              DeviceEventFilter(QObject *parent);
                              bool nativeEventFilter(const QByteArray &eventType, void *message, long *result)
                              Q_DECL_OVERRIDE;
                          Q_SIGNALS:
                              void notify();
                          };
                          

                          And the source file:

                          #include "eventfilter.h"
                          
                          DeviceEventFilter::DeviceEventFilter(QObject *parent)
                          {
                              Q_UNUSED(parent)
                          }
                          
                          bool DeviceEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
                          {
                              Q_UNUSED(eventType) // unneeded as long as we're only running on Windows
                              Q_UNUSED(result)
                              MSG *msg = (MSG *)(message);
                          
                              if ((msg->message == WM_DEVICECHANGE)
                                      && (msg->wParam == DBT_DEVNODES_CHANGED))
                              {
                                  emit notify();
                              }
                              return false;
                          }
                          
                          1 Reply Last reply
                          6

                          10/12

                          3 Oct 2020, 06:27

                          • Login

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