关于C#如何使用Prodave(实战DllImport)2011-07-16 博客园 臭臭的大佬严格意义上讲,此文不算OPC的范畴。起因是,另一个项目的PLC强人说,OPC慢,用prodave吧,好,用就用吧,装好Prodave看是看其英文 资料,虽然英文不好,但好在这里英文很简单。好了,上网查了点资料,这里还要感谢几个朋友的帮忙,让我对于C#与C++的数据结构有了更进 一步的认识,也学会了使用DllImport在本文的开头,我要说明下,Prodave是西门子的通信方式,即使我提供了Prodave6.dll,您不注册也是 没用用的,所以请使用西门子的安装程序,哪里下载?自己百度一下。不要来问我哪里下载Prodave6.dll,也不要问我为什么程序会报错说没 有注册dll下面开始进入正文。(1)上来第一个函数,就是连接PLC的LoadConnection_ex6,在说明书里描述如下:LoadConnection_ex6 The basic LoadConnection_ex6 function initializes the adapter, checks if thE driver is loaded, initializes the addresses that have been assigned parameters and activates the selected interface. LoadConnection_ex6 is used to set up a transport connection via MPI/PB- or IP addresses (TCP/IP protocol) int LoadConnection_ex6 (int ConNr, char* pAccessPoint, int ConTableLen, CON_TABLE_TYPE * pConTable); Parameters ConNr [in] Number of the connection (max. 64 connections). pAccessPoint [in] access point (zero-terminated) of the driver used, e.g. "S7ONLINE" for the MPI driver or 0 (default). ConTableLen [in] length of the table of connections provided by the user in bytes pConTablE [in] pointer to address list of connected users; ‘Adr ==0’ is taken as the end mark of the list. #pragma pack(1) typedef union { unsigned char Mpi; // MPI/PB station address (2) unsigned char Ip[4]; // IP address (192.168.0.1) unsigned char Mac[6]; // MAC address (08-00-06-01-AA-BB) } CON_ADR_TYPE; typedef struct { CON_ADR_TYPE Adr; // connection address unsigned char AdrType; // Type of address: MPI/PB (1), IP (2), MAC (3) unsigned char SlotNr; // Slot number unsigned char RackNr; // Rack number } CON_TABLE_TYPE; #pragma pack(1)好吧,起先其他的转换网上都有 ,不难,但是出现了union共用体,恩C#没有这个概念。怎么办?起先,参考网上的资料,采用[StructLayout (LayoutKind.Explicit)] struct S1 { [FieldOffset(0)] int a; [FieldOffset(0)] int b; }