visa打开与仪器的通信通道称为会话session

Communication Channels: Sessions
The examples from Introductory Programming Examples used an operation called viOpen() to open communication channels with the instruments. In VISA terminology, this channel is known as a session. A session connects you to the resource you addressed in the viOpen() operation and keeps your communication and attribute settings unique from other sessions to the same resource. In VISA, a resource can have multiple sessions to it from the same program and for interfaces other than Serial, even from other programs simultaneously. Therefore, you must consider some things about the resource to be local, that is, unique to the session, and other things to be global, that is, common for all sessions to the resource.

If you look at the descriptions of the various attributes supported by the VISA resources, you will see that some are marked global (such as VI_ATTR_INTF_TYPE) and others are marked local (such as VI_ATTR_TMO_VALUE). For example, the interface bus that the resource is using to communicate with the device (VI_ATTR_INTF_TYPE) is the same for everyone using that resource and is therefore a global attribute. However, different programs may have different timeout requirements, so the communication timeout value (VI_ATTR_TMO_VALUE) is a local attribute.

Again, look at the message-based communication example. To open communication with the instrument, that is, to create a session to the INSTR Resource, you use the viOpen() operation as shown below:

status = viOpen(defaultRM, "GPIB0::1::INSTR", VI_NULL, VI_NULL, &instr);

In this case, the interface to which the instrument is connected is important, but only as a means to uniquely identify the instrument. The code above references a GPIB device on bus number 0 with primary address 1. The access mode and timeout values for viOpen() are both VI_NULL. Other values are defined, but VI_NULL is recommended for new users and all instrument drivers.

However, notice the statement has two sessions in the parameter list for viOpen(), defaultRM and instr. Why do you need two sessions? As you will see in a moment, viOpen() is an operation on the Resource Manager, so you must have a communication channel to this resource. However, what you want is a session to the instrument; this is what is returned in instr.

For the entire duration that you communicate with this GPIB instrument, you use the session returned in instr as the communication channel. When you are finished with the communication, you need to close the channel. This is accomplished through the viClose() operation as shown below:

status = viClose(instr);

At this point, the communication channel is closed, but you are still free to open it again or open a session to another device. Notice that you do not need to close a session to open another session. You can have as many sessions to different devices as you want.

沟通渠道: 会话
编程入门实例中的例子使用了一个叫做viOpen()的操作来打开与仪器的通信通道。在VISA术语中,这个通道被称为会话。一个会话将你和你在viOpen()操作中寻址的资源连接起来,并使你的通信和属性设置与同一资源的其他会话保持一致。在VISA中,一个资源可以有多个会话从同一个程序中进入,对于Serial以外的接口,甚至可以同时从其他程序进入。因此,你必须考虑关于资源的一些事情是本地的,即对会话来说是唯一的,而其他事情是全局的,即对资源的所有会话来说是共同的。

如果你看一下VISA资源所支持的各种属性的描述,你会发现有些被标记为全局的(如VI_ATTR_INTF_TYPE),有些被标记为局部的(如VI_ATTR_TMO_VALUE)。例如,资源用来与设备通信的接口总线(VI_ATTR_INTF_TYPE)对使用该资源的每个人来说都是一样的,因此是一个全局属性。然而,不同的程序可能有不同的超时要求,所以通信超时值(VI_ATTR_TMO_VALUE)是一个本地属性。

再一次,看看基于消息的通信例子。为了打开与仪器的通信,也就是创建一个通往INSTR资源的会话,你可以使用viOpen()操作,如下所示:

status = viOpen(defaultRM, "GPIB0::1::INSTR", VI_NULL, VI_NULL, &instr) ;

在这种情况下,仪器所连接的接口很重要,但只是作为唯一识别仪器的手段。上面的代码引用了一个总线号为0、主地址为1的GPIB设备。viOpen()的访问模式和超时值都是VI_NULL。可以定义其他的值,但对于新用户和所有的仪器驱动程序来说,建议使用VI_NULL。

然而,注意到该语句在viOpen()的参数列表中有两个会话,defaultRM和instr。为什么需要两个会话?稍后你会看到,viOpen()是对资源管理器的操作,所以你必须有一个与这个资源的通信通道。然而,你想要的是一个到仪器的会话;这就是在instr中返回的东西。

在你与这个GPIB仪器通信的整个过程中,你使用instr中返回的会话作为通信通道。当你完成通信后,你需要关闭该通道。这可以通过viClose()操作来完成,如下所示:

status = viClose(instr);

在这一点上,通信通道被关闭了,但是你仍然可以自由地再次打开它或者打开一个会话到另一个设备。注意,你不需要关闭一个会话来打开另一个会话。你可以在不同的设备上有任意多的会话,只要你想。

原文地址:Communication Channels: Sessions - NI

posted @ 2023-06-02 17:50  小头痛  阅读(104)  评论(0编辑  收藏  举报