Jump to content

How to tell if a COM port is in use


Recommended Posts

I have a program (not mine, no source) that uses COM3. It has a "Connect" button that toggles its connection to COM3 on and off.  

I am writing a script that also wants to control the device on COM3.  I want it to click the "Connect" button on the first program, if necessary, to free up COM3.  

I know how to click the button.  It's the "if necessary" part that has me stumped.  

_COM_OpenPort("COM3",...) gets a handle regardless of whether the other program has COM3 connected or not.  

Could someone please tell me how to determine whether the other program is connected to COM3 or whether it is free to use?

 

Link to comment
Share on other sites

I'm not that familiar with COM things, but a quick search shows me an option that may work for you: https://superuser.com/questions/835848/how-to-view-serial-com-ports-but-not-through-device-manager

Quote

In the command prompt use

mode

Used without parameters, mode displays all the controllable attributes of the CON (console) and the available COM devices (and LPT as well).

Here's the output from my CMD window:

C:\Users\Raven>mode

Status for device COM1:
-----------------------
    Baud:            9600
    Parity:          None
    Data Bits:       8
    Stop Bits:       1
    Timeout:         ON
    XON/XOFF:        OFF
    CTS handshaking: OFF
    DSR handshaking: OFF
    DSR sensitivity: OFF
    DTR circuit:     ON
    RTS circuit:     OFF

Seems like you could use Run to launch the mode command, then StdOutRead to get the data and determine if COM3 is in use. It'll be up to you to determine if this is usable for you, but it's one option to look into.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Thank you, Universalist, for the suggestion.  

It is true that if the port is in use the MODE COM3 command gets
  "Device COM3 is not currently available",
and if the port is not in use it gets the status info for the port.  So your suggestion will probably work.  

I think I will wait a while and see if there is a way for AutoIt to get this same info via some UDF that accesses some API or some such.  If not, I'll work on using the MODE command.  

News at eleven....

Thanks again.

 

Link to comment
Share on other sites

There's also this UDF if you could check out: 

It looks like that it uses some DllCalls and some RegEdit reading. Registry things are mentioned in the stackoverflow link I posted (https://superuser.com/questions/835848/how-to-view-serial-com-ports-but-not-through-device-manager) along with a WMIC method. So there's a few ways to potentially do it. I don't think that AutoIt has any 'native' ways of doing serial port things.

 

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

2 hours ago, mistersquirrle said:

There's also this UDF if you could check out: 

Thanks again, mistersquirrle (sorry for picking up the wrong name before).  I have previously poked around in that UDF and I will look some more.  I have been using ComUDF.  

I am also starting to look at windows APIs.  I am not a Windows developer but I think I can recognize a useful API function if I trip on it.  It seems like there should be one underlying the MODE command.  Too bad MODE is not open source.  Maybe if I run it through the linux strings command I can spot an API name.  

Hoo boy!  I forgot MODE is an ancient DOS command and it uses the DOS "int" API.  So, no help there.  

Oh well....

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...