将字节作为参数传递给C#? [英] Passing bytes as parameter to c#?

查看:62
本文介绍了将字节作为参数传递给C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在尝试从python调用c#方法时陷入困境.我正在使用python 3.2而不是IronPython.我用pip安装了最新版本的python.net

I am currently stuck while trying to call a c# methods from python. I am using python 3.2 and not IronPython. I used pip to install the latest version of python.net

使用ref或out参数时,会出现问题(经常讨论).

Problem occurs (as often discussed) while using ref or out parameters.

到目前为止,这是我的代码:

Here is my code so far:

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.AddReference(path)中键入dll的路径,因为这也将无法工作)

I have had some major issues getting clr. running at all. But in this exact config it seems to work (I need to save the path to a variable, otherwise it wont work, I also cant type the path the dll in clr.AddReference(path) because this wont work as well)

c#版本方法如下:

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

我的状态变量获取一个值,该值与c#类的状态枚举完美配合.

My status variable gets a value which works perfectly with the status enum for the c# class.

问题是:调用后,我的变量版本"为空.为什么?根据:如何使用.NET方法可以在Python中进行修改?这应该是合法的处理方式.我也尝试使用显式版本,但我的名称空间clr不包含clr.Reference().

Problem is: after the call my variable "version" is empty. Why? According to: How to use a .NET method which modifies in place in Python? this should be a legal way to do things. I also tried to use the explicit version but my namespace clr does not contain clr.Reference().

下一个(也是更严重的)问题是pio.gpioReadWrite().有关此问题的信息:

The next (and more severe) problem is pio.gpioReadWrite().Here the info about this one:

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

我在这里收到错误消息:

Here I get the error message:

TypeError: No method matches given arguments

我从上面使用哪个呼叫都没有关系.他们都失败了.

It doesn't matter which of the calls I use from above. All of them fail.

这是调试运行的完整输出:

Here is the full output of a debugging run:

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

希望你们中的一个有解决此问题的想法.

Hope one of you has an idea on how to fix this.

谢谢,凯文

推荐答案

Python.Net无法处理IronPython之类的ref/out参数.
status,readData = gpio.gpioReadWrite(b'\ x01',b'\ x00',b'\ x00')调用不是很正确,因为Python.Net不会将更新的readData作为第二个返回结果.
您可以使用反射处理ref参数.在此处

Python.Net doesn't handle ref/out parameters like IronPython.
status, readData = gpio.gpioReadWrite(b'\x01',b'\x00',b'\x00') call is not quite correct since Python.Net will not return an updated readData as second result.
You can handle ref parameters using reflection. Check out my answer to similar question here

有一个适合您情况的粗略代码模板:

there is a rough code template for your case:

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]

这篇关于将字节作为参数传递给C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆