如何使用Python和.net / c#来自动化普林斯顿仪器的光场

本文关键字:普林斯顿 自动化 仪器 光场 Python 何使用 net | 更新日期: 2023-09-27 18:16:04

我想用Python来控制一个专有的光谱软件(Princeton Instruments LightField)。我有一个使用Matlab的光场自动化示例。该示例使用。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,[]);

看起来我用有效的参数(一个布尔值和一个空字符串)实例化了自动化类。我没有太多的文档除了我的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)

如何使用Python和.net / c#来自动化普林斯顿仪器的光场

检查__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>
>>>