嚴格意義上講,此文不算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;
}