LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I check if a serial port is busy?

Solved!
Go to solution

When using a VISA Resource controller to list and select a serial port, is it possible to check if the selected port is busy (already active in another application) or not?

0 Kudos
Message 1 of 10
(10,988 Views)

Do you mean if it is used by another application such as HyperTerminal or TerraTerm?

If so, then yes it is possible.  I am trying to remember how I did it...  I think it was using activeX (or dll) to find out if an application was running.

 

I will do a quick search and post it (if I find it).  Otherwise, I can look at the code tonight to see how I did that.

 

OR

 

Do you mean if the port is used by another running instance of LabVIEW or even applications which use the same VISA drivers, such as CVI?

0 Kudos
Message 2 of 10
(10,984 Views)

Thanks for your quick(!) answer!

 

Yes, that's exactly what I mean.  If a different program like HyperTerminal, Putty or TeraTerm is connected to the port.  I'm selecting the port in the very beginning of the program, and opening the port later in a different part of the init phase, so I can't try to open the port to check for error messages either.

0 Kudos
Message 3 of 10
(10,977 Views)

Checking for error message didn't work.  That part I remember.

I did a bit of a search and did not find anything yet.

I do remember looking on the MIcrosoft site and it mentionned how to do it.  I then coded the explanation into LabVIEW.

I thought I posted something here.

0 Kudos
Message 4 of 10
(10,973 Views)

I think I used the FindProcDLL plugin as described below.

 

using the name of the process

You can detect if your application is running by using the FindProcDLL plug-in to check if a process having the name of your application executable is running.

Make sure your application executable has an easily identifiable name, or you risk detecting another unrelated application having the same name as yours.

Example:

FindProcDLL::FindProc "MyApp.exe"
IntCmp $R0 1 0 notRunning
    MessageBox MB_OK|MB_ICONEXCLAMATION "MyApp is running. Please close it first" /SD IDOK
    Abort
notRunning:

Additionally you may decide (or offer the user the possibility) to kill the running process forcibly, by using the KillProc plug-in. Note: This can be dangerous as the state of the application and edited files might not be saved correctly.

[edit] using a window class

If your application creates a (main) window with a specific classname or title, you can use it to detect whether the application is running.

Unless you're sure of the application behaviour, be aware that many applications have dynamically generated window classname (different each time), and localized window title depending on the user language.

You can use Visual Studio tool Spy++ or freeware tool SysTree++ to find the window class of your application.

Examples:

FindWindow $0 "MyAppWindowClass"
StrCmp $0 0 notRunning
    MessageBox MB_OK|MB_ICONEXCLAMATION "MyApp is running. Please close it first" /SD IDOK
    Abort

or

FindWindow $0 "" "MyApp Window Title"
StrCmp $0 0 notRunning
    MessageBox MB_OK|MB_ICONEXCLAMATION "MyApp is running. Please close it first" /SD IDOK
    Abort
Message 5 of 10
(10,972 Views)

This rings a bell.

 

All I wanted was to know if the application was running.  I did not care which port it used, I just killed the application (HyperTerm or TerraTerm).

If you followed the link in my last post, you also find KillProcDll.exe

 

 

 

Introduction

This plugin provides the ability to check if any process running just with the name of its .exe file.

  FindProc "process_name.exe"

The return code is stored in the $R0 variable.

The return codes are as follows:

  • 0 = Process was not found
  • 1 = Process was found
  • 605 = Unable to search for process
  • 606 = Unable to identify system type
  • 607 = Unsupported OS
  • 632 = Process name is invalid
Message 6 of 10
(10,967 Views)

Ouch, this was advanced! 🙂  And way beyond my LabVIEW skills, I'm afraid. 🙂

 

It's not possible to use a reference to the selected port and check a status flag in any way?

 

If not, how then can I implement the code you proposed?

 

I'm not so interested in knowing which application is using the selected port, and definitely not in forcibly ending the application.  I just want to know if the port it busy/connected or not.

 

0 Kudos
Message 7 of 10
(10,952 Views)
Solution
Accepted by topic author ojohnsen

For checking if port already opened or not you can also use CreateFile WinAPI function.

Also you can read following article: Serial Communications in Win32.

From my point of view using VISA functions much more easier. Just call VISA Configure Serial Port.vi, and if port buzy, then you should receive error code 0xBFFF0072 (the resource is valid, but VISA cannot currently access it). Not fully understand, why you can't use it.

 

Andrey.

 

Message 8 of 10
(10,950 Views)

In my case, I wanted to close whatever application was using the serial port.

So I guess, it was a more complicated approach..  😉

 

Andrey's suggestion is very easy to implement.

0 Kudos
Message 9 of 10
(10,925 Views)

 Andrey Dmitriev

 

@Andrey Dimitriev:

VISA Configure Serial Port.vi worked like a charm.  Exactly what I was after. 🙂

 

@Ray.R:

Yes, I would expect that killing a process would involve a bit more extensive coding. 🙂  I'll keep your tips in mind if I have to look into that on another project.

0 Kudos
Message 10 of 10
(10,898 Views)