Open Com Port > 9 with C++ ?

Discussion in 'C++' started by Sylvain, Apr 19, 2004.

  1. Sylvain

    Sylvain Guest

    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
    Sylvain, Apr 19, 2004
    #1
    1. Advertising

  2. Sylvain

    George Guest

    > 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
    George, Apr 19, 2004
    #2
    1. Advertising

  3. Sylvain <> spoke thus:

    > I need some help in programming an RS232 serial interface.


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


    (This isn't the place to ask.)

    Your post is off-topic for comp.lang.c++. Please visit

    http://www.slack.net/~shiva/welcome.txt
    http://www.parashift.com/c -faq-lite/

    for posting guidelines and frequently asked questions. Thank you.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 19, 2004
    #3
  4. Sylvain

    Thomas Lutz Guest

    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



    On 19 Apr 2004 03:01:04 -0700, (Sylvain) wrote:

    >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
    Thomas Lutz, Apr 21, 2004
    #4
  5. Sylvain

    Greg Schmidt Guest

    On Wed, 21 Apr 2004 17:30:29 GMT, Thomas Lutz wrote:

    > 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.

    --
    Greg Schmidt
    Trawna Publications http://www.trawna.com/
    Greg Schmidt, Apr 24, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John T. Goodman

    Overhead of 4-port over 2-port SRAM

    John T. Goodman, Jan 25, 2005, in forum: VHDL
    Replies:
    0
    Views:
    596
    John T. Goodman
    Jan 25, 2005
  2. Sean Wolfe
    Replies:
    1
    Views:
    2,247
    Joerg Jooss
    Apr 28, 2005
  3. b3ny
    Replies:
    11
    Views:
    918
    Babu Kalakrishnan
    Nov 20, 2004
  4. Gerald Klix
    Replies:
    0
    Views:
    1,267
    Gerald Klix
    Oct 26, 2005
  5. Pom
    Replies:
    2
    Views:
    1,651
    Bas-i
    Jan 31, 2007
Loading...

Share This Page