今天写了个RFID的封装类,主要实现c#调用c++中的函数。如果大家有需要,可以当个参考: 

接口文档如下:

 

c++中的Struct 如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef struct 
{
    int deviceID;          //阅读器逻辑ID
    char address[20];      //IP地址或串口;如"COM1","10.86.10.56"
    int status;            //0-未链接;1-已链接,未初始化;2-已链接,已初始化
    int style;             //0-网络客户端;1-网络服务端;2-串口
} ReaderItem;
  
  
  
typedef struct 
{   
    int length; 
    unsigned char tagData[2048]; //标签长度1byte(lenth字)+标签epc码(lenth*2)byte+标签长度1byte+标签epc码
} InventoryResult;

c#的类如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class RFIDBaseManage
   {
 
       [DllImport("xxx.dll")]
       public static extern int Connect(string dev, ref int devid);
 
       [DllImport("xxx.dll")]
       public static extern int Disconnect(int devid);
 
       [DllImport("xxx.dll", EntryPoint = "GetReaders")]
       public static extern int GetReaders(ref ReaderItem item,int maxCount,ref int count);
   }
 
   public class RFIDInfoManage
   {
       [DllImport("xxx.dll", EntryPoint = "EPC1G2_Inventory")]
        public static extern int EPC1G2_Inventory(int DeviceID, short antennalID, ref InventoryResult result);
   }
 
   [StructLayout(LayoutKind.Sequential)]
   public struct ReaderItem
   {
         
       public int deviceID;   
       [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
       public string address;  //IP地址或串口;如"COM1","10.86.10.56"
 
       public int status;  
       public int style;  
   }
 
 
   [StructLayout(LayoutKind.Sequential)]
   public struct InventoryResult
   {
       public int length;
       [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
       public byte[] tagData;      }

 另外,也可以参考这几篇文章:

 C#调用c++Dll结构体数组指针的问题

[转]C#时常需要调用C++DLL

http://hi.baidu.com/yun0216/blog/item/37a463106b575ffec3ce7913.html

 http://blog.csdn.net/xqf222/archive/2010/09/11/5877795.aspx

  对于Byte[]的处理 可以参考

  Byte数组转换成string 的方法积累

posted on 2012-04-01 15:57  代岳强  阅读(252)  评论(0编辑  收藏  举报