如何使用 Python 和 .NET/C# 来自动化 Princeton Instruments LightField

How to use Python and .NET/C# to automatise Princeton Instruments LightField

我想用Python来控制专有的光谱软件(Princeton Instruments LightField)。我有一个使用 Matlab 的 LightField 自动化示例。该示例使用 .NET 使用提供的 DLL 来控制 LightField。

我使用 pythonnet 将 DLL 加载到 Python,但我无法与 LightField 通信。

这是一个(非)工作示例:

import sys
sys.path.append(r"C:\Program Files\Princeton Instruments\LightField")
sys.path.append(r"C:\Program Files\Princeton Instruments\LightField\AddInViews")

import clr
clr.AddReference('PrincetonInstruments.LightFieldViewV4')
clr.AddReference('PrincetonInstruments.LightField.AutomationV4')
clr.AddReference('PrincetonInstruments.LightFieldAddInSupportServices')

import PrincetonInstruments.LightField.AddIns as AddIns
from PrincetonInstruments.LightField.Automation import Automation

instance = Automation(True,[])

这是错误消息:

File "D:/python/test_lightfield.py", line 21, in <module> instance = Automation(True,[])
TypeError: no constructor matches given arguments

但是,当我查看 help(Automation) 的开头时:

help(Automation)
Help on class Automation in module PrincetonInstruments.LightField.Automation:

class Automation(System.Object)
 |  Void .ctor(Boolean, System.Collections.Generic.List`1[System.String])
 |  
 |  Method resolution order:
 |      Automation
 |      System.Object
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.

或相关的 Matlab 示例文件:

out.automation = PrincetonInstruments.LightField.Automation.Automation(visible,[]);

看起来我用有效参数(一个布尔值和一个空字符串)实例化了自动化 class。 除了我的 Matlab 示例文件外,我没有太多文档。

我做错了什么?

编辑:这是类型问题。使用 .NET 类型列表而不是 Python 列表是可行的。

from PrincetonInstruments.LightField.Automation import Automation
from System.Collections.Generic import List
from System import *
l = List[String]()
instance = Automation(True,l)

检查 __overloads__ 中支持的构造函数签名,例如:

>>> import clr
>>> from System import Decimal
>>> Decimal.__overloads__
System.Decimal(Int32[])
System.Decimal(UInt64)
System.Decimal(UInt32)
System.Decimal(Int64)
System.Decimal(Int32)
System.Decimal(Single)
System.Decimal(Double)
System.Decimal(Int32, Int32, Int32, Boolean, Byte)

编辑:

在传递第二个参数之前将其转换为 .NET 类型 (System.Collections.Generic.List[System.String])。 pythonnet暂不支持容器自动转换

编辑:

操作方法如下:

在 C# 中:

using System;
using System.Collections.Generic;

namespace ListInConstr
{
    public class ListInConstr
    {
        public ListInConstr(bool test1, List<String> test2)
        {

        }
    }
}

将其编译成 DLL:

csc.exe /target:library ListInConstr.cs

在Python中:

>>> import clr
>>> import sys
>>> sys.path.append(r"C:\Debug")
>>> clr.AddReference("ListInConstr")
<System.Reflection.RuntimeAssembly object at 0x02849E50>
>>> from ListInConstr import ListInConstr
>>> ListInConstr.__overloads__
ListInConstr.ListInContr(Boolean, System.Collections.Generic.List`1[System.String])
>>> from System.Collections.Generic import List
>>> from System import String
>>> arg2=List[String]() #do not add elements here
>>> ListInConstr(True,arg2)
<ListInConstr.ListInConstr object at 0x0284E890>
>>>