如何使用pythonnet使C函数返回bytearray参数?

2024-09-27 02:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个导入的C#DLL函数返回bytearray。我不知道怎么做。在

我已经使用pythonnet将一个C#程序集导入到Python中。我必须使用C,因为C dll是我正在使用的硬件的第三方dll。我不能编辑那个dll。在

通过这个函数,我成功地返回了不是bytearray的引用类型。在

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中的函数:

^{pr2}$

功能说明如下:

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)时,我希望它打印得到的bytearray,但显然我做得不对。在

我也尝试过使用反射,但我不熟悉它。在

谢谢。在


Tags: ofthe函数isqueryintdllx00

热门问题