使用NI-VISA.dll进行串口通讯的问题求助

hcyang 3月前 435

我想使用arrdio调用.net NationalInstruments.Visa.dll来实现用仪器设备用通讯,先用串口进行测试验证,结果可以正常写入,但是回读的时候报错“ivi.visa.timeoutexception异常”,经查资料有可能是因为终止符的原因,但是我写入的数据是带有"\n"终止符的(USB串口工具TX-RX短接验证)。请各位大牛帮看一下有哪里不对。

aardio验证代码如下:

import console;
import dotNet;

dotNet.reference(
	["Ivi.Visa"] = $"~\lib\visa\.res\Ivi.Visa.dll";
	["NationalInstruments.Visa"] = $"~\lib\visa\.res\NationalInstruments.Visa.dll";
)
dotNet.import("Ivi.Visa")
dotNet.import("NationalInstruments.Visa")

var power=NationalInstruments.Visa.SerialSession("ASRL6::INSTR");
power.SendEndEnabled=true
power.TerminationCharacterEnabled=true
power.TerminationCharacter=10		//终止符10=LF=\n
power.TimeoutMilliseconds=2000
power.RawIO.Write("hello world!\n");		//写串口命令,用Bus Hound抓包确认是写成功的
sleep(1000)
var pdata=power.RawIO.ReadString()	//读串口命令,此处报Ivi.Visa.IOTimeoutException异常,用Bus Hound抓包确认是有数据输入的	
console.log(pdata)					//

以上验证所使用的dll在C#中验证OK,且有测试写的字符串去掉"\n"也会报Ivi.Visa.IOTimeoutException异常。

与C#代码相比,串口的打开方式不同,C#代码如下:

using System;
using NationalInstruments.Visa;
using Ivi.Visa;

namespace SerialPortExample_NationalInstruments
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. 创建 VISA 资源管理器实例  
            NationalInstruments.Visa.ResourceManager powerrs = new ResourceManager();
            NationalInstruments.Visa.SerialSession powerseesion = (NationalInstruments.Visa.SerialSession)powerrs.Open("ASRL6::INSTR");

            //2. 设置串口配置(如果需要)
            powerseesion.SendEndEnabled = true;
            powerseesion.TerminationCharacterEnabled = true;
            powerseesion.TerminationCharacter = 10;
            powerseesion.TimeoutMilliseconds = 2000;
            
            // 3. 发送数据  
            powerseesion.RawIO.Write("hello world!\n");

            // 4. 读取响应                  
            string response = powerseesion.RawIO.ReadString();

            // 5. 输出响应  
            Console.WriteLine("Response: " + response);
            

            Console.ReadKey();
        }
    }
}


上传的附件:
最新回复 (5)
  • mndsoft 3月前
    0 2
    这里有个参考 https://www.chengxu.xyz/t/474
  • hcyang 3月前
    0 3

    非常感谢,我下载原版工程打开试了一下。结果报库引用dll错误,也没看出来哪里有问题

  • popde 3月前
    0 4
    你电脑上可能没有安装vc运行库, 最简单的方法, 百度搜[vc运行库大全],安装下
  • popde 3月前
    0 5

    我重新编译了个包含运行时的dll , 你可以用这个替换工程目录res里面的那个同名dll 

    下载 https://www.chengxu.xyz/upload/file/20240603/1717379428283302.zip

    刚看到这里有附件功能,  上传到了附件

    上传的附件:
  • hcyang 3月前
    0 6
    感谢各位的帮助。问题已经解决了,引用NationalInstruments.Visa.dll的代码没问题,只是不能用同一个串口自发自收。使用VISADLLEx.dll的把loadDll中的$去掉也可以解决报错。如果把NationalInstruments.Visa.dll的代码封装成库的思路该是怎样的?这个库中串口用SerialSession,GPIB用GpibSession,USB用UsbSession。该如何二次封装?
返回
发新帖