首页 > 解决方案 > NI LabVIEW NetworkVariableManager 是否应该保持连接?

问题描述

我从一位同事那里收到了一些 C# 代码,用于与通过以太网连接的 cRIO 设备进行交互。我正在尝试提高代码质量,以使其对未来的用户更易于理解,但是我正在努力从 API 文档中提取一些相关信息。NetworkVariableManager我的主要问题是留在该州是否会引起问题Connected

现在代码使用一个看起来像的类

public class RIOVar<T>
{
    public readonly string location;

    public RIOVar(string location)
    {
        this.location = location;
    }

    public T Get()
    {
        using(NetworkVariableReader<T> reader = new NetworkVariableReader<T>(location) )
        {
            reader.Connect();
            return reader.ReadData().GetValue()
        }
    }

    public void Write(T value)
    {
        using(NetworkVariableWriter<T> writer = new NetworkVariableWriter<T>(location) )
        {
            writer.Connect();
            writer.WriteValue(value);
        }
    }
}

实际的类做的远不止这些,但实际与 cRIO 通信的部分基本上归结为这两个方法和location数据成员。

我想知道的是,在构造函数中将它们作为类成员和reader它们是否会更好(在构造它们时,连接应该是可能的),但我不知道这是否会对计算机和 RIO 相互通信的方式有一些不利影响(可能连接的管理器使用一些资源或程序必须维护某种寄存器......?)因此这里的管理器连接的方法用于读/写操作是更好的设计。writerConnect

标签: c#labview

解决方案


保持变量连接将其支持资源保留在内存中:

  • 线程
  • 插座
  • 数据缓冲区

在线帮助中列出了这些资源,但我不清楚该列表是否完整:

  • NationalInstruments.NetworkVariable uses multiple threads to implement the reading and writing infrastructure. When reading or writing in a tight loop insert a Sleep call, passing 0, to allow a context switch to occur thereby giving the network variable threads time to execute.
  • ... snip ...
  • NationalInstruments.NetworkVariable shares resources such as sockets and data buffers among connections that refer to the same network variable in the same program.

In my opinion, I'd expect better runtime performance by connecting/disconnecting as infrequently as possible. For example, when the network is reachable, connect; when it isn't, disconnect.


推荐阅读