我试图从导入的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 = bytearray(b'\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00')
第一个返回的值应该是函数的“返回值”。没关系。
0、13、1分别是参考值ComAdr,Totallen和CardNum。但是,明显缺少的是“ EPClenandEPC”。 EPClenandEPC是一个字节数组。它没有“ ref”,并且由于某种原因也没有返回。
当我键入print(EPC)时,我希望它能打印获得的字节数组,但显然我做错了。
我也曾短暂尝试过使用反射,但是我对此并不熟悉。
谢谢。