|
| 1 | +using System.Runtime.InteropServices; |
| 2 | + |
| 3 | +namespace Tuxeip |
| 4 | +{ |
| 5 | + public unsafe class Tuxeip_Class |
| 6 | + { |
| 7 | + public const int MAX_MSG_LEN = 1024; // a verifier |
| 8 | + |
| 9 | + public const int EIP_PORT = 0xAF12;//44818 |
| 10 | + public const int ENCAP_PROTOCOL = 0x0001; |
| 11 | + |
| 12 | + /* Ethernet IP commands */ |
| 13 | + public const int EIP_NOP = 0x0000; |
| 14 | + public const int EIP_LISTTARGETS = 0x0001; // Reserved for legacy RA |
| 15 | + public const int EIP_LISTSERVICES = 0x0004; |
| 16 | + public const int EIP_LISTIDENTITY = 0x0063; |
| 17 | + public const int EIP_LISTINTERFACES = 0x0064; |
| 18 | + public const int EIP_REGISTERSESSION = 0x0065; |
| 19 | + public const int EIP_UNREGISTERSESSION = 0x0066; |
| 20 | + public const int EIP_SENDRRDATA = 0x006F; |
| 21 | + public const int EIP_SENDUNITDATA = 0x0070; |
| 22 | + public const int EIP_INDICATESTATUS = 0x0072; |
| 23 | + public const int EIP_CANCEL = 0x0073; |
| 24 | + |
| 25 | + // Ethernet IP status code |
| 26 | + public const int EIP_SUCCESS = 0x0000; |
| 27 | + public const int EIP_INVALID_COMMAND = 0x0001; |
| 28 | + public const int EIP_MEMORY = 0x0002; |
| 29 | + public const int EIP_INCORRECT_DATA = 0x0003; |
| 30 | + public const int EIP_INVALID_SESSION_HANDLE = 0x0064; |
| 31 | + public const int EIP_INVALID_LENGTH = 0x0065; |
| 32 | + public const int EIP_UNSUPPORTED_PROTOCOL = 0x0069; |
| 33 | + |
| 34 | + // Ethernet IP Services Class |
| 35 | + public const int EIP_SC_COMMUNICATION = 0x0100; |
| 36 | + public const int EIP_VERSION = 0x01; |
| 37 | + public const int CIP_DEFAULT_TIMEOUT = 1000; |
| 38 | + |
| 39 | + private const ushort _OriginatorVendorID = 0xFFFE; |
| 40 | + private const uint _OriginatorSerialNumber = 0x12345678; |
| 41 | + private const byte _Priority = 0x07; |
| 42 | + private const sbyte _TimeOut_Ticks = 0x3f; |
| 43 | + private const short _Parameters = 0x43f8, _TO_Parameters = 0x43f8; |
| 44 | + private const byte _Transport = 0xa3; |
| 45 | + private const sbyte _TimeOutMultiplier = 0x01; |
| 46 | + |
| 47 | + const string PATH = @"tuxeipAB.dll"; |
| 48 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 49 | + private static extern Eip_Session* _OpenSession(string serveur, int port, int buffer_len, int timeout); |
| 50 | + public static Eip_Session* OpenSession(string serveur) |
| 51 | + { |
| 52 | + return _OpenSession(serveur, EIP_PORT, MAX_MSG_LEN, CIP_DEFAULT_TIMEOUT); |
| 53 | + } |
| 54 | + |
| 55 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 56 | + public static extern void CloseSession(Eip_Session* session); |
| 57 | + |
| 58 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 59 | + public static extern int _RegisterSession(Eip_Session* session); |
| 60 | + |
| 61 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 62 | + public static extern int _UnRegisterSession(Eip_Session* session); |
| 63 | + |
| 64 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 65 | + private static extern Eip_Connection* _ConnectPLCOverCNET( |
| 66 | + Eip_Session* session, |
| 67 | + Plc_Type Plc, |
| 68 | + byte Priority, |
| 69 | + sbyte TimeOut_Ticks, |
| 70 | + uint TO_ConnID, //originator's CIP consumed session ID |
| 71 | + ushort ConnSerialNumber,// session serial number |
| 72 | + ushort OriginatorVendorID, |
| 73 | + uint OriginatorSerialNumber, |
| 74 | + sbyte TimeOutMultiplier, |
| 75 | + uint RPI,// originator to target packet rate in msec |
| 76 | + byte Transport, |
| 77 | + byte[] path, byte pathsize); |
| 78 | + public static Eip_Connection* ConnectPLCOverCNET(Eip_Session* session, Plc_Type Plc, byte[] path) |
| 79 | + { |
| 80 | + return _ConnectPLCOverCNET(session, Plc, _Priority, _TimeOut_Ticks, 0x12345678, 0x6789, |
| 81 | + _OriginatorVendorID, _OriginatorSerialNumber, _TimeOutMultiplier, 5000, _Transport, path, (byte)path.Length); |
| 82 | + } |
| 83 | + |
| 84 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 85 | + public static extern Eip_Connection* _ConnectPLCOverDHP( |
| 86 | + Eip_Session* session, |
| 87 | + Plc_Type Plc, |
| 88 | + byte Priority, |
| 89 | + sbyte TimeOut_Ticks, |
| 90 | + uint TO_ConnID, //originator's CIP consumed session ID |
| 91 | + ushort ConnSerialNumber,// session serial number |
| 92 | + ushort OriginatorVendorID, |
| 93 | + uint OriginatorSerialNumber, |
| 94 | + sbyte TimeOutMultiplier, |
| 95 | + uint RPI,// originator to target packet rate in msec |
| 96 | + byte Transport, |
| 97 | + DHP_Channel channel, |
| 98 | + byte[] path, byte pathsize); |
| 99 | + public static Eip_Connection* ConnectPLCOverDHP(Eip_Session* session, Plc_Type Plc, DHP_Channel channel, byte[] path) |
| 100 | + { |
| 101 | + return _ConnectPLCOverDHP(session, Plc, _Priority, _TimeOut_Ticks, 0x12345678, 0x6789, |
| 102 | + _OriginatorVendorID, _OriginatorSerialNumber, _TimeOutMultiplier, 5000, _Transport, channel, path, (byte)path.Length); |
| 103 | + } |
| 104 | + |
| 105 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 106 | + public static extern PLC_Read* _ReadPLCData( |
| 107 | + Eip_Session* session, |
| 108 | + Eip_Connection* connection, |
| 109 | + DHP_Header* dhp, |
| 110 | + byte* routepath, byte routepathsize, |
| 111 | + Plc_Type type, short tns, |
| 112 | + string address, short number); |
| 113 | + |
| 114 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 115 | + public static extern int _WritePLCData( |
| 116 | + Eip_Session* session, |
| 117 | + Eip_Connection* connection, |
| 118 | + DHP_Header* dhp, |
| 119 | + byte* routepath, byte routepathsize, |
| 120 | + Plc_Type type, short tns, |
| 121 | + string address, |
| 122 | + PLC_Data_Type datatype, |
| 123 | + void* data, |
| 124 | + short number); |
| 125 | + |
| 126 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 127 | + public static extern int _PCCC_GetValueAsBoolean(PLC_Read* reply, int index); |
| 128 | + |
| 129 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 130 | + public static extern float _PCCC_GetValueAsFloat(PLC_Read* reply, int index); |
| 131 | + |
| 132 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 133 | + public static extern int _PCCC_GetValueAsInteger(PLC_Read* reply, int index); |
| 134 | + |
| 135 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 136 | + public static extern int _GetLGXValueAsInteger(LGX_Read* reply, int index); |
| 137 | + |
| 138 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 139 | + public static extern float _GetLGXValueAsFloat(LGX_Read* reply, int index); |
| 140 | + |
| 141 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 142 | + public static extern int _Forward_Close(Eip_Connection* connection); |
| 143 | + |
| 144 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 145 | + public static extern LGX_Read* _ReadLgxData( |
| 146 | + Eip_Session* session, |
| 147 | + Eip_Connection* connection, |
| 148 | + string adress, |
| 149 | + ushort number); |
| 150 | + |
| 151 | + [DllImport(PATH, CallingConvention = CallingConvention.Cdecl)] |
| 152 | + public static extern int _WriteLgxData( |
| 153 | + Eip_Session* session, |
| 154 | + Eip_Connection* connection, |
| 155 | + string adress, |
| 156 | + LGX_Data_Type datatype, |
| 157 | + void* data, |
| 158 | + //int datasize, |
| 159 | + short number); |
| 160 | + |
| 161 | + } |
| 162 | +} |
0 commit comments