将字节作为参数传递给C#?

4

我目前在尝试从Python中调用C#方法时遇到了问题。我正在使用Python 3.2而不是IronPython。我使用pip安装了最新版本的python.net。

问题通常出现在使用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时遇到了一些重大问题。但是在这个确切的配置中,它似乎可以正常工作(我需要将路径保存到变量中,否则它不会工作,我也不能在clr.AddReference(path)中键入dll路径,因为这也不起作用)

C#版本的方法如下:

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

我的状态变量获取了一个值,它与c#类的状态枚举完美匹配。问题是:调用后我的“version”变量为空。为什么?根据如何在Python中使用原地修改的.NET方法?,这应该是合法的。我也尝试过使用明确的版本,但我的命名空间clr不包含clr.Reference()。
下一个(更严重的)问题是pio.gpioReadWrite()。关于它的信息如下:
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

希望你们中有人能想到解决这个问题的方法。
谢谢, Kevin

您可以使用此拉取请求中的分支,使 pythonnet 与 ref/out 协同工作:https://github.com/pythonnet/pythonnet/pull/227 - denfromufa
相关问题:https://github.com/pythonnet/pythonnet/issues/226 - denfromufa
1个回答

2
Python.Net无法像IronPython那样处理ref/out参数。
status, readData = gpio.gpioReadWrite(b'\x01',b'\x00',b'\x00')调用不完全正确,因为Python.Net不会返回更新后的readData作为第二个结果。
您可以使用反射来处理ref参数。请查看我在类似问题这里的答案。

以下是您的情况的代码模板:

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]

嗨Gosha,感谢迄今为止的帮助。如果我按原样运行代码,会出现错误,即无法将system.object[]转换为system.byte(在第11行)。我将第9行更改为使用System.Array[System.Byte],但现在我收到错误:status = method.Invoke(gpio,parameters) TypeError: No method matches given arguments。有什么解决方法吗? - Kevin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接