我目前被困在嘗試從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
希望有一個能對如何解決此問題的想法。
感謝, 凱文
您可以使用分支從這個拉請求來獲取REF /淡出pythonnet工作:https://開頭github.com/pythonnet/pythonnet/pull/227 – denfromufa
相關問題:https://github.com/pythonnet/pythonnet/issues/226 – denfromufa