PDA

View Full Version : Finding available COM ports with QextSerialPort



Ferric
26th October 2010, 01:22
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.



//************************************************** ************************************************** ****************
//
// Finds all the available serial COM ports on the PC which is running this application.
//
// Returns true if at least one COM port is found. Returns false if no COM ports are found.
//
// This function is run from within MainWindow::findCorrectCOMPort
//
//************************************************** ************************************************** ****************
bool MainWindow::findAvailableCOMPorts()
{
const int MAX_COM_PORT = 100; // The maximum number of COM ports that will be searched for.
QString testPortName;

availablePorts.clear(); // Clears the ports found in the previous search.
QextSerialPort testPort;
testPort.setBaudRate(BAUD9600);
testPort.setFlowControl(FLOW_OFF);
testPort.setParity(PAR_NONE);
testPort.setDataBits(DATA_8);
testPort.setStopBits(STOP_1);

for (int i = 1; i < MAX_COM_PORT; i++)
{
testPortName = QString("COM%1").arg(i);
testPort.setPortName(testPortName);
if (testPort.open(QIODevice::ReadWrite))
{
availablePorts.append(testPortName);
testPort.close();
}
}
if (availablePorts.isEmpty())
{
QMessageBox::information(this,tr("Information Message"),tr("No available COM ports found") );
return false;
}
else
{
qDebug() << "availablePorts:" << availablePorts;
return true;
}

} // End of bool findAvailableCOMPorts()


Thanks for looking

schnitzel
26th October 2010, 07:34
I'm not sure if this will work, but give it a try:
http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/75f33b8d-2f41-4e4b-8b30-c4d0227c494a

squidge
26th October 2010, 09:40
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.

Ferric
26th October 2010, 09:49
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 "\\.\" ?

//************************************************** ************************************************** ****************
//
// Finds all the available serial COM ports on the PC which is running this application.
//
// Returns true if at least one COM port is found. Returns false if no COM ports are found.
//
// This function is run from within MainWindow::findCorrectCOMPort
//
//************************************************** ************************************************** ****************
bool MainWindow::findAvailableCOMPorts()
{
const int MAX_COM_PORT = 100; // The maximum number of COM ports that will be searched for.
QString testPortName;

availablePorts.clear(); // Clears the ports found in the previous search.
QextSerialPort testPort;
testPort.setBaudRate(BAUD9600);
testPort.setFlowControl(FLOW_OFF);
testPort.setParity(PAR_NONE);
testPort.setDataBits(DATA_8);
testPort.setStopBits(STOP_1);

for (int i = 1; i < MAX_COM_PORT; i++)
{
testPortName = QString("\\.\COM%1").arg(i); // I TRY TO PREFIX "\\.\" IN HERE..
testPort.setPortName(testPortName);
if (testPort.open(QIODevice::ReadWrite))
{
availablePorts.append(testPortName);
testPort.close();
}
}
if (availablePorts.isEmpty())
{
QMessageBox::information(this,tr("Information Message"),tr("No available COM ports found") );
return false;
}
else
{
qDebug() << "availablePorts:" << availablePorts;
return true;
}

} // End of bool findAvailableCOMPorts()

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

high_flyer
26th October 2010, 09:49
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...)

squidge
26th October 2010, 18:22
"\\.\" 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 "\\\\.\\".

schnitzel
26th October 2010, 19:39
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).

squidge
26th October 2010, 20:16
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()