2016-06-13 21 views
3

我目前被困在嘗試從python調用c#方法。我使用Python 3.2而不是IronPython。我使用pip安裝最新版本的python.net將字節作爲參數傳遞給c#?

使用ref或out參數時發生問題(經常討論)。

這是到目前爲止我的代碼:

import clr 

path = clr.FindAssembly("USB_Adapter_Driver") 
clr.AddReference(path) 
from USB_Adapter_Driver import USB_Adapter 
gpio = USB_Adapter() 

version2 = '' 
status, version = gpio.version(version2) 
print ('status: ' + str(status)) 
print ('Version: ' + str(version)) 

readMask = bytearray([1]) 
writeData = bytearray([0]) 

print (readMask) 
print (writeData) 

status, readData = gpio.gpioReadWrite(b'\x01',b'\x00',b'\x00') 
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],b'\x00') 
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],) 

我已經得到CLR一些重大問題。一直運行。但在這個確切的配置它似乎工作(我需要保存到一個變量的路徑,否則它不會工作,我也不能鍵入路徑的DLL在clr.AddReference(路徑),因爲這不會工作以及)

C#版本的方法是這樣的:

public USB_Adapter_Driver.USB_Adapter.Status version(ref string ver) 

我的狀態變量獲得與對C#類的狀態枚舉完美工作的價值。

問題是:調用後我的變量「版本」是空的。爲什麼?根據:How to use a .NET method which modifies in place in Python?這應該是一種合法的做事方式。我也嘗試使用顯式版本,但是我的命名空間clr不包含clr.Reference()。

下一個(也是更嚴重的)問題是pio.gpioReadWrite()這裏關於info這一個:

public USB_Adapter_Driver.USB_Adapter.Status gpioReadWrite(byte readMask, byte writeData, ref byte readData) 

在這裏,我得到的錯誤信息:

TypeError: No method matches given arguments 

一點也沒有」無論我從上面使用哪一個電話。他們都失敗了。

下面是一個調試運行的全輸出:你

d:\[project path]\tests.py(6)<module>() 
status: 6 
Version: 
bytearray(b'\x01') 
bytearray(b'\x00') 
Uncaught exception. Entering post mortem debugging 
Running 'cont' or 'step' will restart the program 
d:\[project path]\tests.py(28)<module>() 
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],) 
(Pdb) Traceback (most recent call last): 
    File "D:\WinPython-64bit-3.4.4.2Qt5\python-3.4.4.amd64\lib\pdb.py", line 1661, in main 
    pdb._runscript(mainpyfile) 
    File "D:\WinPython-64bit-3.4.4.2Qt5\python-3.4.4.amd64\lib\pdb.py", line 1542, in _runscript 
    self.run(statement) 
    File "D:\WinPython-64bit-3.4.4.2Qt5\python-3.4.4.amd64\lib\bdb.py", line 431, in run 
    exec(cmd, globals, locals) 
    File "<string>", line 1, in <module> 
    File "d:\[project path]\tests.py", line 28, in <module> 
    status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],) 
TypeError: No method matches given arguments 

希望有一個能對如何解決此問題的想法。

感謝, 凱文

+0

您可以使用分支從這個拉請求來獲取REF /淡出pythonnet工作:https://開頭github.com/pythonnet/pythonnet/pull/227 – denfromufa

+0

相關問題:https://github.com/pythonnet/pythonnet/issues/226 – denfromufa

回答

1

Python.Net不處理像IronPython的REF /輸出參數。
status, readData = gpio.gpioReadWrite(b'\x01',b'\x00',b'\x00')調用並不完全正確,因爲Python.Net不會返回更新後的readData作爲第二個結果。
您可以使用反射來處理參考參數。看看我的回答類似的問題here

有你的情況下,粗糙的代碼模板:

import clr 
clr.AddReference("USB_Adapter_Driver") 
import System 
import USB_Adapter_Driver 

myClassType = System.Type.GetType("USB_Adapter_Driver.USB_Adapter, USB_Adapter_Driver") 
method = myClassType.GetMethod("gpioReadWrite") 
parameters = System.Array[System.Object]([System.Byte(1),System.Byte(0),System.Byte(0)]) 
gpio = USB_Adapter_Driver.USB_Adapter() 
status = method.Invoke(gpio,parameters) 
readData = parameters[2] 
+0

嗨Gosha,感謝您的幫助。如果我按原樣運行代碼,則會出現錯誤,該system.object []不能轉換爲system.byte(在第11行)。我改變了第9行使用System.Array [System.Byte],但我現在得到的錯誤:status = method.Invoke(gpio,參數) TypeError:沒有方法匹配給定的參數。任何想法如何解決? – Kevin

+0

檢查更新的答案。 – igorushi

相關問題