Menu

#578 Avoid double-open of serial ports on Windows

open
8
2008-12-10
2008-12-10
No

Currently, opening a serial port on Windows involves a double-open: (1) open generically (2) check the handle for serialness (3) reopen with OVERLAPPED flag if serial.

This causes trouble in some setups where the open/close (and its timing) is not transparent to the underlying driver, like bluetooth RFCOMM profile. See

http://groups.google.fr/group/comp.lang.tcl/tree/browse_frm/thread/9f2f29db7b5864fd/fd6d04dbc15d8575?rnum=1&_done=%2Fgroup%2Fcomp.lang.tcl%2Fbrowse_frm%2Fthread%2F9f2f29db7b5864fd%2Fdd17ccbb0019ed90%3F#doc_fd6d04dbc15d8575

for a concrete example.

A simple and efficient solution is to use a "name hint" to detect COM ports by their filename instead of resorting to a syscall to detect serialness after the fact.

Rolf Shroedter has suggested that at least the two following patterns should be detected:

COM[0-9]:?
\\.\COM[0-9]+:?

It also turns out that the first one should be applied to the [file tail], since \foo\bar\COM4 is also a valid serial port reference in Windows.

However, we believe that a good strategy should be to make only a reasonable effort in that direction, leaving tricky cases handled by the current behavior; this in order not to spoil the performance of [open] for non-serials.

A patch will be uploaded shortly to support just the above two patterns.

Discussion

  • Rolf Schroedter

    Rolf Schroedter - 2008-12-11

    The following patch performes a "name hint" to detect native Windows serial port name by one of the following patterns (case-insensitive). Pattern matching is checked quite restrictive, because *all open-filenames* need to pass this test.

    COM[1-9]:?
    \\.\COM[0-9]+
    //./COM[0-9]+

    For all filenames not matching the patterns above, the original behavior open-close-reopen is preserved, thus supporting (virtual) comports of any other name:
    1. Open channel with NON-OVERLAPPED flag
    2. Detect serial port by checking GetCommState() result
    3. Reopen the serial port with OVERLAPPED flag
    This behavior can be enforced even for native Windows serial ports e.g. with

    [open /com1 r+]

     
  • Alexandre Ferrieux

    Rolf's patch (he cannot upload)

     
  • Alexandre Ferrieux

    File Added: no_double_serial_open.patch