如何使用调用库函数节点将字符串传递回labview

发布于 2024-07-08 03:43:00 字数 103 浏览 3 评论 0原文

我想使用LabVIEW的调用库函数节点来访问DLL函数,并让该函数返回一个字符串以显示在我的VI上。 我该怎么做呢? 我很高兴从 DLL 返回数字,但我真的很努力寻找如何返回字符串的示例。

I want to use LabVIEW's Call Library Function Node to access a DLL function, and have this function return a string to displayed on my VI. How would I go about doing this? I am quite happy returning numbers from my DLL, but am really struggling to find any examples of how to return a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世晨晓 2024-07-15 03:43:00

至少有几种方法可以从调用库函数节点返回字符串:

  1. 从 DLL 函数返回 C 字符串指针,并将调用库函数节点配置为返回类型为“C 字符串指针”。 请注意,返回的字符串在函数返回后必须有效,因此它不能是指向堆栈上分配的字符串的指针。 它必须是以下之一:在堆上分配、静态分配、常量字符串文字等。

    看起来 LabVIEW 目录中的 examples/dll/regexpr/Regular Expression Solution/VIs/Get Error String.vi 采用了这种方法。

  2. 在VI中分配一个字符串,按照Azim的建议使用“C字符串指针”参数将其传递到调用库函数节点,并覆盖DLL中的内容。 分配字符串的一种方法是使用 Initialize Array 创建所需大小的 u8 数组,然后使用 Byte Array To String 将其转换为字符串。

    确保您传入的字符串足够大以容纳字符串的内容,并确保将字符串长度传递给 DLL,以便它知道缓冲区有多大。 我相信默认参数是一个空字符串。 如果您的 VI 的第一次猜测不够大,那么计算出正确的字符串长度可能需要调用 DLL 两次。

  3. 使用“字符串句柄”参数将字符串传递到调用库函数节点,并使用 DLL 中的 LabVIEW 函数根据需要调整字符串大小。 这需要您的 DLL 专门设计为与 LabVIEW 接口,并且需要链接到 LabVIEW 提供的静态库。

    该方法的示例随 LabVIEW 一起提供,格式为 examples/dll/hostname/hostname.vi

There are at least a few ways to return a string from a Call Library Function Node:

  1. Return a C string pointer from your DLL function, and configure the Call Library Function Node to have a return type of "C String Pointer". Note that the returned string must be valid after the function returns, so it can't be a pointer to a string allocated on the stack. It must be one of the following: allocated on the heap, statically allocated, a constant string literal, etc.

    It looks like examples/dll/regexpr/Regular Expression Solution/VIs/Get Error String.vi in the LabVIEW directory takes this approach.

  2. Allocate a string in your VI, pass it to the Call Library Function Node using a "C String Pointer" parameter as Azim suggested, and overwrite its contents in the DLL. One way to allocate the string is to use Initialize Array to create a u8 array of the desired size, and then use Byte Array To String to convert it to a string.

    Be sure that the string you pass in is large enough to hold the contents of your string, and make sure to pass the string length to the DLL so that it knows how large the buffer is. I believe that the default parameter is an empty string. Figuring out the right string length may require calling into the DLL twice, if your VI's first guess isn't large enough.

  3. Pass the string to the Call Library Function Node using a "String Handle" parameter, and use LabVIEW functions in your DLL to resize the string as necessary. This requires your DLL to be specifically designed to interface with LabVIEW and requires linking against a static library that is provided with LabVIEW.

    An example of this method ships with LabVIEW as examples/dll/hostname/hostname.vi.

浅暮の光 2024-07-15 03:43:00

我从你的问题假设你已经有一个可以将数字返回到Labview的DLL。 为了从 DLL 返回字符串,我

void returnString(char myString[])
{
  const char *aString = "test string";
  memcpy(myString, aString, 12);
}

在 Labview 中使用以下 C++ 函数创建了一个 DLL,然后使用调用库函数节点并按如下方式配置它:

    Library Name or Path: c:\path\to\my\custom.dll
           Function Name: returnString
           Calling Convention: C

    Parameters: 
           Parameter: return type
                type: void

           Parameter: arg1
                type: String
                string format: C String Pointer

    Function prototype:
        void returnString(CStr arg1);

将程序框图中的 arg1 输出连接到字符串指示器并运行后。 字符串“test string”应出现在前面板上。

我尝试让 returnString 函数的类型为 CStr,

CStr returnString()
{ ...
 }

但在编译 DLL 项目时出现构建错误。

Update

感谢@bk1e评论,不要忘记在Labview中为字符串预先分配空间。

I assume from your question that you already have a DLL that can return numbers to Labview. To return a string from the DLL, I have created a DLL with the following C++ function

void returnString(char myString[])
{
  const char *aString = "test string";
  memcpy(myString, aString, 12);
}

In Labview I then use the Call Library Function Node and configure it as follows

    Library Name or Path: c:\path\to\my\custom.dll
           Function Name: returnString
           Calling Convention: C

    Parameters: 
           Parameter: return type
                type: void

           Parameter: arg1
                type: String
                string format: C String Pointer

    Function prototype:
        void returnString(CStr arg1);

After connect the arg1 output in the block diagram to a string indicator and run. The string "test string" should appear in the front panel.

I tried to have the returnString function be of type CStr as in

CStr returnString()
{ ...
 }

but I got build errors when compiling the DLL project.

Update

Thanks to @bk1e comment don't forget to pre-allocate space in Labview for the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文