LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using a VISA session by more than one application

Hello,
I need to be able to use one Harware (RS232 or GPIB  via VISA) by two applications sequentially. For performance reasopns, I do not want to close and re-init the hardware every time I hand it over to the other application.
 
The way it should work:
1) App1 initiates the hardware
2) App1 communicates to hardware.
3) App1 sends a hand-over message to App2
4) App2 communicates to hardware.
5) App2 sends a hand-over message to App1
6) Goto 2)
 
So I isolated the VISA init and communication functions to a DLL. The DLL has a shared memory block where I write the VISA session handle from the initialization.
The first process (App1) which uses the DLL initializes the VISA session and writes the handle into the shared memory. Now, the plan was that the second process (App2) uses this session handle, and continoues communication to the hardware.
However, it turns out that the second process cannot use that handle
The error message in App2 is: "The given session or object reference is invalid."
 
How can I solve this puzzle?

Thanks,
Greg
 
0 Kudos
Message 1 of 12
(5,185 Views)
0 Kudos
Message 2 of 12
(5,174 Views)

Hello Garth,

thanks for the reply. Yes, I do use the memory mapped file approach described in the article. And this works just fine. The problem I face is , that the very same VISA session handle that I got in Process 1, seems not usable in Process 2.

In other words: The VISA handle seems only valid in the process it was created.

Thanks,

Greg

 

0 Kudos
Message 3 of 12
(5,147 Views)

Hi Greg,

The method you are using to pass the session between the DLLs should be working.  The error you are receiving basically implies that the session is somehow being closed out.  In LabVIEW there is a setting that automatically closes a VISA session when the top level vi goes idle, resulting in an Invalid VISA Session Error.  There might be a similar setting in the program you are using.  My guess is that your program is somehow closing it out within the DLL or automatically closing the session when you access the other DLL.

What program are you using to call the DLLs?  What is the exact error that you are receiving?  What information are you passing between the DLLs?  Some more specific information about what you are working with may help me figure out the issue.

Regards,

Lauren L.

Applications Engineering
National Instruments
Message 4 of 12
(5,115 Views)

Hello Lauren,

thanks for taking care of that issue. I have compiled a detailled description of the problem in the attached pdf.

Best Regards,

Greg

 

 

0 Kudos
Message 5 of 12
(5,094 Views)

Hi Greg,

Thanks for the detailed description of your setup and problem.  The DLL appears to make sense, so I have been trying to build it for further testing.  In order to make it easier, could you include all the CVI project files including the .prj, .c, .h files as well as any other files needed to compile the DLL?

Regards,

Lauren L.

Applications Engineering
National Instruments
0 Kudos
Message 6 of 12
(5,049 Views)

Hello Lauren,

sure, no problem. All the necessary files are zipped and attached.

In the DLL, I initialize and access a GPIB instrument. You will have to adapt the address and command to the hardware you use. The effect also shows with ASRLx if you prefer RS232.

Thanks!
Best Regards,

Greg

 

0 Kudos
Message 7 of 12
(5,041 Views)


Lauren L wrote:

The method you are using to pass the session between the DLLs should be working. 


I'm not sure about that. The handle is only a pointer to a structure containing the internal system data: although the handle may legally exist in shared memory between two applications, the structure to which it points still only 'belongs' to the first application that created the handle. This structure cannot be directly accessed by the second application, since it is not in shared memory.
 
JR
0 Kudos
Message 8 of 12
(4,982 Views)

I believe that JR is correct.  After looking into this further I am not sure that it is possible to do using two different applications.  It can be done in two different VI's (in LabVIEW) because they are located in the same application.  In separate applications, however, Windows will make sure they have their own memory space, meaning the second application will not be able to access the VISA session.

You might need to consider other alternatives, such as calling everything within one application or opening and closing the session each time.  I am sorry that there are not any better options.  Good luck with everything!

Lauren L.

Applications Engineering
National Instruments
0 Kudos
Message 9 of 12
(4,967 Views)

Hello Lauren,
Hello JR,

thanks for your help. However I don't want to give up so fast. After all, - we're only looking to access a bunch of bytes  Smiley Wink

I found a "DuplicateHandle" function in the SDK, that does exactly the thing I need to do, only with windows-, not with VISA handles.

However, isn't there either a equivalent function for VISA handles, or would anyone know how to implement such a function for VISA?

Thanks again,

Greg

 

 

DuplicateHandle

The DuplicateHandle function duplicates an object handle. The duplicate handle refers to the same object as the original handle. Therefore, any changes to the object are reflected through both handles. For example, the current file mark for a file handle is always the same for both handles.

BOOL DuplicateHandle(
  HANDLE hSourceProcessHandle,  // handle to source process
  HANDLE hSourceHandle,         // handle to duplicate
  HANDLE hTargetProcessHandle,  // handle to target process
  LPHANDLE lpTargetHandle,      // duplicate handle
  DWORD dwDesiredAccess,        // requested access
  BOOL bInheritHandle,          // handle inheritance option
  DWORD dwOptions               // optional actions
);
0 Kudos
Message 10 of 12
(4,954 Views)