Open Com Port > 9 with C++ ?

S

Sylvain

Hi everybody !

I need some help in programming an RS232 serial interface.

Opening a com port upper than "COM9:" (for example "COM10:", "COM11:"
etc)returns ERROR_INVALID_NAME error.


Details:
I use C++ with MFC with Wisual Studio 6.0 on XP.
The example code:
-----------------------------------------------------------------
m_hPort = CreateFile (lpszPortName, // Pointer to name of the
port
GENERIC_READ | GENERIC_WRITE,
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to security
attribute
OPEN_EXISTING, // How to open
0, // Port attributes
NULL); // Handle to port with
attribute

With lpszPortName from "COM1:" to "COM9:" it works well.
With lpszPortName equal to "COM10:" and more error 123
ERROR_INVALID_NAME occurs !

I tried in HyperTerminal, and I hav no problem to open com port 10,
11, 12 ...

Any ideas what causes the Port to respond with "INVALID_NAME"`?

Help is greatly appreciated.
Thanks in advance,
bye,
Sylvain
 
G

George

With lpszPortName from "COM1:" to "COM9:" it works well.
With lpszPortName equal to "COM10:" and more error 123
ERROR_INVALID_NAME occurs !

Maybe there should be a HEX encoded number
 
T

Thomas Lutz

The cause of the problem is that CreateFile accepts strings "com1" -
"com9" as names of devices and reject to do so for two and more digit
numbers. In order to open "com10" and above we actually have to feed
"\\\\.\\com10" string to CreateFile.

For more serial I/O tips, tricks and free utilities, visit
www.taltech.com
 
G

Greg Schmidt

The cause of the problem is that CreateFile accepts strings "com1" -
"com9" as names of devices and reject to do so for two and more digit
numbers. In order to open "com10" and above we actually have to feed
"\\\\.\\com10" string to CreateFile.

I wasn't going to reply to this thread because it's off topic, but I feel
that a partial off-topic answer is worse than a complete off-topic answer,
and hence I'll mention that the form Thomas gives above is also valid for
all of the single-digit com ports. IOW, there is no requirement to do any
special case checking: if you need to support high-numbered com ports, just
use the \\\\.\\comx form for everything from com1 up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top