You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Excel VBA中集成Yoctopuce DLL控制Yocto-4-20mA-Tx模块

刚好在32位Excel环境下折腾过Yoctopuce的模块,给你整理一套完整的实现方案,完全适配你的Windows 10 64位+Excel 2016 32位配置:

前置准备
  • 下载32位版本的Yoctopuce核心DLL(因为Excel 2016是32位进程,只能加载32位DLL),不要下64位版本,否则会报错找不到模块
  • 把DLL放到Excel文件所在的目录,或者Windows的SysWOW64文件夹(32位程序默认的DLL搜索路径),也可以在VBA声明里指定完整路径
VBA函数声明与初始化

首先在VBA模块里声明DLL的核心函数,注意C语言和VBA的类型对应(int对应VBA的Long,char*对应ByVal String):

' 声明Yoctopuce核心DLL函数(32位版本)
Private Declare Function yapiInitAPI Lib "yocto_api.dll" (ByVal connection_type As Long, ByVal errmsg As String) As Long
Private Declare Function yapiUpdateDeviceList Lib "yocto_api.dll" (ByVal forceupdate As Long, ByVal errmsg As String) As Long
Private Declare Function yapiHTTPRequest Lib "yocto_api.dll" (ByVal device As String, ByVal request As String, ByVal buffer As String, ByVal buffsize As Long, fullsize As Long, ByVal errmsg As String) As Long

然后写初始化API和设备连接的代码:

Sub InitYoctoModule()
    ' 定义固定长度的错误缓冲区,避免内存溢出
    Dim errMsg As String * 256
    Dim apiResult As Long
    
    ' 初始化API,连接类型选USB(对应C语言里的YAPI_USB常量,值为1)
    apiResult = yapiInitAPI(1, errMsg)
    If apiResult <> 0 Then
        MsgBox "API初始化失败: " & Trim(errMsg), vbCritical
        Exit Sub
    End If
    
    ' 强制更新设备列表,确保能识别到连接的Yocto-4-20mA-Tx
    apiResult = yapiUpdateDeviceList(1, errMsg)
    If apiResult <> 0 Then
        MsgBox "设备列表更新失败: " & Trim(errMsg), vbCritical
        Exit Sub
    End If
    
    MsgBox "Yocto模块初始化成功,设备已连接", vbInformation
End Sub
核心控制:设置4-20mA输出

通过yapiHTTPRequest发送控制命令,设置输出电流(范围必须在4.0~20.0mA之间)。你需要先通过Yoctopuce官方工具(比如Yocto-Visualizer)获取设备的ID(序列号或自定义逻辑名):

Sub SetOutputCurrent(ByVal deviceId As String, ByVal targetCurrent As Double)
    Dim errMsg As String * 256
    Dim responseBuffer As String * 1024 ' 存储设备响应的缓冲区
    Dim fullResponseSize As Long
    Dim requestResult As Long
    Dim controlRequest As String
    
    ' 校验电流范围,避免无效值
    If targetCurrent < 4# Or targetCurrent > 20# Then
        MsgBox "电流值必须在4.0~20.0mA之间", vbExclamation
        Exit Sub
    End If
    
    ' 构造控制请求字符串,Yoctopuce的API格式为"set_current?current=XXX"
    controlRequest = "set_current?current=" & CStr(targetCurrent)
    
    ' 发送请求到目标设备
    requestResult = yapiHTTPRequest(deviceId, controlRequest, responseBuffer, Len(responseBuffer), fullResponseSize, errMsg)
    If requestResult <> 0 Then
        MsgBox "设置电流失败: " & Trim(errMsg), vbCritical
        Exit Sub
    End If
    
    ' 可选:显示设备响应信息
    MsgBox "电流已成功设置为 " & targetCurrent & "mA" & vbCrLf & "设备响应: " & Trim(responseBuffer), vbInformation
End Sub
调用示例

比如你的设备ID是Y420MA1234,要设置输出12mA,直接调用:

Sub TestSetCurrent()
    Call InitYoctoModule
    Call SetOutputCurrent("Y420MA1234", 12.0)
End Sub
常见问题排查
  • 找不到DLL报错:确认用的是32位DLL,路径正确;可以在Lib后面指定完整路径,比如Lib "C:\MyExcelFiles\yocto_api.dll"
  • 初始化失败:检查USB设备是否插好,是否安装了Yoctopuce的USB驱动(官方工具会自动安装)
  • 电流设置无效:确认设备ID正确,电流值在4~20mA范围内,设备是否处于正常工作状态

内容的提问来源于stack exchange,提问作者RedSmolf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:41:34