Results 1 to 8 of 8

Thread: Finding available COM ports with QextSerialPort

  1. #1
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Finding available COM ports with QextSerialPort

    Hi,

    I have the following code that is searching for available COM ports on the computer. It works fine on all the 5 computers that I have tried excluding one. On this one computer it will not pick up a USB-to-Serial-Adapter plugged into COM13. Windows does recognize the device as connected to COM13.
    All computers have been running windows XP.

    Does anybody have any thoughts on what could be going wrong?
    Any thoughts appreciated.


    Qt Code:
    1. //********************************************************************************************************************
    2. //
    3. // Finds all the available serial COM ports on the PC which is running this application.
    4. //
    5. // Returns true if at least one COM port is found. Returns false if no COM ports are found.
    6. //
    7. // This function is run from within MainWindow::findCorrectCOMPort
    8. //
    9. //********************************************************************************************************************
    10. bool MainWindow::findAvailableCOMPorts()
    11. {
    12. const int MAX_COM_PORT = 100; // The maximum number of COM ports that will be searched for.
    13. QString testPortName;
    14.  
    15. availablePorts.clear(); // Clears the ports found in the previous search.
    16. QextSerialPort testPort;
    17. testPort.setBaudRate(BAUD9600);
    18. testPort.setFlowControl(FLOW_OFF);
    19. testPort.setParity(PAR_NONE);
    20. testPort.setDataBits(DATA_8);
    21. testPort.setStopBits(STOP_1);
    22.  
    23. for (int i = 1; i < MAX_COM_PORT; i++)
    24. {
    25. testPortName = QString("COM%1").arg(i);
    26. testPort.setPortName(testPortName);
    27. if (testPort.open(QIODevice::ReadWrite))
    28. {
    29. availablePorts.append(testPortName);
    30. testPort.close();
    31. }
    32. }
    33. if (availablePorts.isEmpty())
    34. {
    35. QMessageBox::information(this,tr("Information Message"),tr("No available COM ports found") );
    36. return false;
    37. }
    38. else
    39. {
    40. qDebug() << "availablePorts:" << availablePorts;
    41. return true;
    42. }
    43.  
    44. } // End of bool findAvailableCOMPorts()
    To copy to clipboard, switch view to plain text mode 

    Thanks for looking

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Finding available COM ports with QextSerialPort

    I'm not sure if this will work, but give it a try:
    http://social.msdn.microsoft.com/for...0-c4d0227c494a

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Finding available COM ports with QextSerialPort

    On Windows XP and above, all comports should be prefixed with "\\.\" to signify your trying to access a device instead of a file. For legacy reasons, com ports 1 - 9 will work without this, so a usb device on COM13 will not work unless you do this.

    There are also much better ways of detecting the available com ports. For example, the above code will not show comports which are open by another process, which can confuse people.

  4. The following user says thank you to squidge for this useful post:

    Ferric (26th October 2010)

  5. #4
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Finding available COM ports with QextSerialPort

    Thanks, this sounds like it could be the problem.

    I tried putting "\\.\" before the COM port name as in the following code, but now it detects no COM ports. How exactly should I be prefixing with "\\.\" ?
    Qt Code:
    1. //********************************************************************************************************************
    2. //
    3. // Finds all the available serial COM ports on the PC which is running this application.
    4. //
    5. // Returns true if at least one COM port is found. Returns false if no COM ports are found.
    6. //
    7. // This function is run from within MainWindow::findCorrectCOMPort
    8. //
    9. //********************************************************************************************************************
    10. bool MainWindow::findAvailableCOMPorts()
    11. {
    12. const int MAX_COM_PORT = 100; // The maximum number of COM ports that will be searched for.
    13. QString testPortName;
    14.  
    15. availablePorts.clear(); // Clears the ports found in the previous search.
    16. QextSerialPort testPort;
    17. testPort.setBaudRate(BAUD9600);
    18. testPort.setFlowControl(FLOW_OFF);
    19. testPort.setParity(PAR_NONE);
    20. testPort.setDataBits(DATA_8);
    21. testPort.setStopBits(STOP_1);
    22.  
    23. for (int i = 1; i < MAX_COM_PORT; i++)
    24. {
    25. testPortName = QString("\\.\COM%1").arg(i); // I TRY TO PREFIX "\\.\" IN HERE..
    26. testPort.setPortName(testPortName);
    27. if (testPort.open(QIODevice::ReadWrite))
    28. {
    29. availablePorts.append(testPortName);
    30. testPort.close();
    31. }
    32. }
    33. if (availablePorts.isEmpty())
    34. {
    35. QMessageBox::information(this,tr("Information Message"),tr("No available COM ports found") );
    36. return false;
    37. }
    38. else
    39. {
    40. qDebug() << "availablePorts:" << availablePorts;
    41. return true;
    42. }
    43.  
    44. } // End of bool findAvailableCOMPorts()
    To copy to clipboard, switch view to plain text mode 

    Also, I dont believe I want comports that are already open by another process, not for this specific application anyway.

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Finding available COM ports with QextSerialPort

    Just use the QextSerialEnumerator class provided with QextSerialPort.
    Its does all the magic for you, you will only need a simple while loop to go through the QStrings it gives you (as "COM1" "COM2" etc...)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    Ferric (30th October 2010)

  8. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Finding available COM ports with QextSerialPort

    "\\.\" is the string you need to prefix to the com port.
    Since "\" in C is an escape character (eg. so \n works), you need to type it twice for each once you want it.
    So when you type it in a C string, you would use "\\\\.\\".

  9. The following user says thank you to squidge for this useful post:

    Ferric (30th October 2010)

  10. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Finding available COM ports with QextSerialPort

    Quote Originally Posted by squidge View Post
    On Windows XP and above, all comports should be prefixed with "\\.\" to signify your trying to access a device instead of a file. For legacy reasons, com ports 1 - 9 will work without this, so a usb device on COM13 will not work unless you do this.

    There are also much better ways of detecting the available com ports. For example, the above code will not show comports which are open by another process, which can confuse people.
    redundant info - this was all explained in the link from my previous post which shows the double escape character issue.
    you hint at a better way to detect available com ports but then... ?

    As for QextSerialEnumerator which was mentioned in another post - be aware that things like LPT will be included in the list of available ports (on Windows).

  11. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Finding available COM ports with QextSerialPort

    Quote Originally Posted by schnitzel View Post
    redundant info - this was all explained in the link from my previous post which shows the double escape character issue.
    It may well have been, but when I'm replying to a post I'm doing it to help the original poster out in my own time. I regularly don't have time to click on every link in everyones post to see if what I was going to write has already been described in some web page - I just click Reply, type my message and hope that it is useful, which is obviously was, as the original poster tried it out. If you don't like that, tough, complain to a moderator if it makes you feel any better.

    A better way of enumerating serial ports (and IMO, the best way) is included in QextSerialPort itself, and thus I didn't think it needed a direct link to the documentation (and if the original poster couldn't find it, they could have quite easily asked).

    Also, last time I checked, QextSerialEnumerator did NOT return LPT ports, as it stated GUID_CLASS_COMPORT in the call to SetupDiGetClassDevs()

Similar Threads

  1. multiple view ports
    By manmohan in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2009, 07:54
  2. network ports up/down
    By kemp in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:38
  3. ports blocking
    By babu198649 in forum General Programming
    Replies: 3
    Last Post: 4th August 2008, 15:51
  4. Finding widget by name
    By mkarakaplan in forum Newbie
    Replies: 1
    Last Post: 9th December 2007, 17:38
  5. QExtSerialPort with com ports above com9
    By mightymark in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2006, 08:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.