2016-11-03 2 views
3

Ich möchte mit Python eine proprietäre spektroskopische Software (Princeton Instruments LightField) steuern. Ich habe ein LightField-Automatisierungsbeispiel mit Matlab. Das Beispiel verwendet .NET, um LightField mit bereitgestellten DLLs zu steuern.Verwendung von Python und .NET/C# zur Automatisierung von Princeton Instruments LightField

Ich habe pythonnet verwendet, um die DLLs in Python zu laden, aber ich konnte nicht mit LightField kommunizieren. Hier

ist ein (Nicht-) Arbeitsbeispiel:

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,[]) 

Und hier ist die Fehlermeldung:

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

Allerdings, wenn ich am Anfang Hilfe (Automation) aussehen:

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. 

oder die entsprechende Matlab Beispieldatei:

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

Es sieht so aus, als ob ich die Automation-Klasse mit gültigen Argumenten instanziiert (eine boolesche und eine leere Zeichenfolge). Ich habe nicht viel Dokumentation abgesehen von meiner Matlab-Beispieldatei.

Was mache ich falsch?

EDIT: Dies war ein Problem des Typs. Unter Verwendung einer .NET-Typliste funktioniert stattdessen eine Python-Liste.

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

Antwort

2

Sucht nach unterstütztem Konstruktor Signaturen in __overloads__, z.B .:

>>> 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) 

EDIT:

Konvertieren Sie Ihr zweites Argument für .NET-Typen (System.Collections.Generic.List[System.String]), bevor es vorbei. Die automatische Konvertierung von Containern wird in Python noch nicht unterstützt.

EDIT:

Hier ist, wie dies zu tun:

In C#:

using System; 
using System.Collections.Generic; 

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

     } 
    } 
} 

Kompilieren diese in DLL:

csc.exe /target:library ListInConstr.cs

In Python:

Hier
>>> 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> 
>>> 
+0

ist die Ausgabe von Automation .__ overloads__ Out [4]: ​​PrincetonInstruments.LightField.Automation.Automation (Boolean, System.Collections.Generic.List'1 [System.String]) – Darckense

+0

Konvertieren Sie Ihr zweites Argument. NET-Typ (System.Collections.Generic.List [System.String]) vor der Übergabe. Die automatische Konvertierung von Containern wird in Python noch nicht unterstützt. – denfromufa

+0

Sorry, schwer zu sein, aber wie mache ich das? Wenn ich zuerst System und dann instance = Automation (True, System.Collections.Generic.List [System.String]) importiere, gibt es mir eine andere Fehlermeldung. (ArgumentException: Impossible de convertire objet de type 'System.RuntimeType' de type 'System.Collections.Generic.List'1 [System.String]'.) (Entschuldigung für das Französisch, es sagt, es kann den System.RunTime-Typ nicht in den zweiten Collections.Generic.List-Typ konvertieren ...) – Darckense

Verwandte Themen