Closing a serial port

This is a discussion on Closing a serial port within the Windows Programming forums, part of the Platform Specific Boards category; Hi, Is it possible to obtain the handle to a serial port and close it, in case another app has ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    34

    Question Closing a serial port

    Hi,

    Is it possible to obtain the handle to a serial port and close it, in case another app has opened it and not closed it?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Generally, you should not be able to do that. I think it is technically possible to do, as sysinternals' Process Explorer can close files open by other apps, and I expect serial port to be treated the same as a file at that level.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    34
    Yes, I agree, it's not something you generaly should be allowed to do, but I'm forced to work with lots of badly written software that uses the serial port and crash quite frequently, thus forcing me to reboot my computer to release the serial port. It would be quite time saving (and of course extremely satisfying :-)) if I could write an app that closes the port for me, allowing me to continue working after such incidents.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Eirik View Post
    Yes, I agree, it's not something you generaly should be allowed to do, but I'm forced to work with lots of badly written software that uses the serial port and crash quite frequently, thus forcing me to reboot my computer to release the serial port. It would be quite time saving (and of course extremely satisfying :-)) if I could write an app that closes the port for me, allowing me to continue working after such incidents.
    Perhaps figuring out why the apps are crashing and fix them to close the serial port would be a better spent effort. Also, if the app is still holding the port open, it is likely that the app hasn't exited, which may cause OTHER problems (including that it still has pending operations on the serial port that will interfere with your own app).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    34
    Quote Originally Posted by matsp View Post
    Perhaps figuring out why the apps are crashing and fix them to close the serial port would be a better spent effort. Also, if the app is still holding the port open, it is likely that the app hasn't exited, which may cause OTHER problems (including that it still has pending operations on the serial port that will interfere with your own app).

    --
    Mats
    The apps are proprietary software produced by big companies (automation industry), and there is not much I can do except reporting bugs and sending in crash logs. The software is used for programming and configuring Programmable Logic Controllers (PLCs). I just don't want to reboot my machine each time the program hangs or doesn't exit properly.

    You are right that this is not curing the problem, I am just tired of having to reboot every time, and if I was able to write a simple program that simply gets the handle of the serial port and closes it, so that the same app that just crashed don't complain that the serial port is in use by another app when trying to connect to the PLC, I would be happy.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, google for process explorer, then use it's search facility to find if the serial port is open by any app, and force-close it.

    I just looked at my "teraterm" application, which has \Device\Serial0 open, and I closed it. Nothing bad has happened, but teraterm is no longer "working".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    34
    Thank you. That did what I wanted :-)

    It would still be interesting to see if someone had some code to do this :-)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Eirik View Post
    Thank you. That did what I wanted :-)

    It would still be interesting to see if someone had some code to do this :-)
    Process explorer source-code is available for download, as I understand - I have never lookad at/for it, but I think it is available.

    It is almost certainly a undocumented API (and possibly some trickery like pretending to be another process in some way) involved in this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by matsp View Post
    It is almost certainly a undocumented API (and possibly some trickery like pretending to be another process in some way) involved in this.
    No, it's even a Win32 api FAQ for decades on BBS and Usenet, mainly on Win32 api group
    (news://nntp.aioe.org/comp.os.ms-wind...ogrammer.win32)
    (C code)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21