c# - 如何使用pythonnet使C#函数返回bytearray参数?
问题描述
我正在尝试从导入的 C# DLL 中的函数返回一个字节数组。我不知道怎么去。
我已经使用 pythonnet 将 C# 程序集导入 Python。我必须使用 C#,因为 C# dll 是我正在使用的硬件的第三方 dll。我无法编辑该 dll。
从这个函数中,我成功地返回了不是字节数组的引用类型。
import clr
clr.AddReference(R'C:...\RFID Chip Sorter Project\UHFReader18CSharp.dll')
from ReaderB import StaticClassReaderB
fComAdr = 255
EPC = bytearray(13)
Totallen = int = 0
CardNum = int = 0
z = StaticClassReaderB.Inventory_G2(fComAdr, 0, 0, 0, EPC, Totallen, CardNum, 3)
print("z=", z)
print ("Z[0]=", z[0])
print("EPC=", EPC)
这是我使用 dotpeek 时 dll 中的函数:
[DllImport("Basic.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int Inventory_G2(
ref byte ConAddr,
byte AdrTID,
byte LenTID,
byte TIDFlag,
byte[] EPClenandEPC,
ref int Totallen,
ref int CardNum,
int PortHandle);
这是功能说明:
Function int StaticClassReaderB.Inventory_G2 (unsigned char *ComAdr, unsigned char AdrTID, unsigned char LenTID, unsigned char TIDFlag, unsigned char *EPClenandEPC, int * Totallen, int *CardNum, int FrmHandle);
Parameter:
ComAdr: Input, pointed to the address of the reader.
AdrTID:Input,query TID’s start address.
LenTID: Input,query TID’s data word number.
TIDFlag: Input,query TID’s flag.
TIDFlag=1:query TID.
TIDFlag=0:query EPC.
EPClenandEPC: Output, Pointed to the array storing the inventory result. It is the EPC data of tag Reader read. The unit of the array includes 1 byte EPCLen and N (the value of EPCLen) bytes EPC. It is the former high-word, low word in the EPC of each tag. It is the former high-byte, low byte in the each word.
Totallen: Output. Pointed to the byte count of the EPClenandEPC.
CardNum: Output. Pointed to the number of tag detected.
FrmHandle: Handle of the corresponding communication port the reader is connected. The handle value is got when calling function AutoOpenComPort or OpenComPort.
Returns:
Zero value when successfully, value:
0x01 Return before Inventory finished
0x02 the Inventory-scan-time overflow
0x03 More Data
0x04 Reader module MCU is Full
others when error occurred.
实际结果:
z = (1, 0, 13, 1)
z[0]= 1
EPC=字节数组(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
第一个返回值应该是函数的“返回值”。没关系。
0、13、1 分别是参考值 ComAdr、Totallen 和 CardNum。然而,显然缺少的是“EPClenandEPC”。EPClenandEPC 是一个字节数组。它没有“ref”,并且由于某种原因没有返回。
当我输入 print(EPC) 时,我希望它打印获得的字节数组,但显然我做错了。
我也简要地尝试过使用反射,但我不熟悉它。
谢谢你。
解决方案
推荐阅读
- html - 图片中的复选框位置
- mysql - 内部联接返回重复记录
- python - 来自2类beautifulsoup的数据框
- javascript - react-web-ar - 包含 AFrameRenderer 的渲染组件给出未捕获的错误
- node.js - server.js 文件中的 Cors 代理问题(react、express、sql)
- javascript - 自动填写外部网页的特定登录表单
- javascript - 正则表达式测试所有字符是否具有相同的大小写
- python - 如何将 Python NdArray 转换为 String 并返回 NdArray ?(在这种情况下)
- extjs - 更改事件之前的 extjs 无线电组
- woocommerce - 使用 REST API 从 Woocommerce 获取所有类别