snap7.net.cs 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  1. /*=============================================================================|
  2. | PROJECT SNAP7 1.2.0 |
  3. |==============================================================================|
  4. | Copyright (C) 2013, 2014 Davide Nardella |
  5. | All rights reserved. |
  6. |==============================================================================|
  7. | SNAP7 is free software: you can redistribute it and/or modify |
  8. | it under the terms of the Lesser GNU General Public License as published by |
  9. | the Free Software Foundation, either version 3 of the License, or |
  10. | (at your option) any later version. |
  11. | |
  12. | It means that you can distribute your commercial software linked with |
  13. | SNAP7 without the requirement to distribute the source code of your |
  14. | application and without the requirement that your application be itself |
  15. | distributed under LGPL. |
  16. | |
  17. | SNAP7 is distributed in the hope that it will be useful, |
  18. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  19. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  20. | Lesser GNU General Public License for more details. |
  21. | |
  22. | You should have received a copy of the GNU General Public License and a |
  23. | copy of Lesser GNU General Public License along with Snap7. |
  24. | If not, see http://www.gnu.org/licenses/ |
  25. |==============================================================================|
  26. | |
  27. | C# Interface classes. |
  28. | |
  29. |=============================================================================*/
  30. using System;
  31. using System.Runtime.InteropServices;
  32. using System.Text;
  33. namespace Snap7
  34. {
  35. public class S7Consts
  36. {
  37. public const string Snap7LibName = "snap7";
  38. //------------------------------------------------------------------------------
  39. // PARAMS LIST
  40. //------------------------------------------------------------------------------
  41. public static readonly Int32 p_u16_LocalPort = 1;
  42. public static readonly Int32 p_u16_RemotePort = 2;
  43. public static readonly Int32 p_i32_PingTimeout = 3;
  44. public static readonly Int32 p_i32_SendTimeout = 4;
  45. public static readonly Int32 p_i32_RecvTimeout = 5;
  46. public static readonly Int32 p_i32_WorkInterval = 6;
  47. public static readonly Int32 p_u16_SrcRef = 7;
  48. public static readonly Int32 p_u16_DstRef = 8;
  49. public static readonly Int32 p_u16_SrcTSap = 9;
  50. public static readonly Int32 p_i32_PDURequest = 10;
  51. public static readonly Int32 p_i32_MaxClients = 11;
  52. public static readonly Int32 p_i32_BSendTimeout = 12;
  53. public static readonly Int32 p_i32_BRecvTimeout = 13;
  54. public static readonly Int32 p_u32_RecoveryTime = 14;
  55. public static readonly Int32 p_u32_KeepAliveTime = 15;
  56. }
  57. public class S7Client
  58. {
  59. #region [Constants, private vars and TypeDefs]
  60. private const int MsgTextLen = 1024;
  61. // Error codes
  62. public static readonly uint errNegotiatingPDU = 0x00100000;
  63. public static readonly uint errCliInvalidParams = 0x00200000;
  64. public static readonly uint errCliJobPending = 0x00300000;
  65. public static readonly uint errCliTooManyItems = 0x00400000;
  66. public static readonly uint errCliInvalidWordLen = 0x00500000;
  67. public static readonly uint errCliPartialDataWritten = 0x00600000;
  68. public static readonly uint errCliSizeOverPDU = 0x00700000;
  69. public static readonly uint errCliInvalidPlcAnswer = 0x00800000;
  70. public static readonly uint errCliAddressOutOfRange = 0x00900000;
  71. public static readonly uint errCliInvalidTransportSize = 0x00A00000;
  72. public static readonly uint errCliWriteDataSizeMismatch = 0x00B00000;
  73. public static readonly uint errCliItemNotAvailable = 0x00C00000;
  74. public static readonly uint errCliInvalidValue = 0x00D00000;
  75. public static readonly uint errCliCannotStartPLC = 0x00E00000;
  76. public static readonly uint errCliAlreadyRun = 0x00F00000;
  77. public static readonly uint errCliCannotStopPLC = 0x01000000;
  78. public static readonly uint errCliCannotCopyRamToRom = 0x01100000;
  79. public static readonly uint errCliCannotCompress = 0x01200000;
  80. public static readonly uint errCliAlreadyStop = 0x01300000;
  81. public static readonly uint errCliFunNotAvailable = 0x01400000;
  82. public static readonly uint errCliUploadSequenceFailed = 0x01500000;
  83. public static readonly uint errCliInvalidDataSizeRecvd = 0x01600000;
  84. public static readonly uint errCliInvalidBlockType = 0x01700000;
  85. public static readonly uint errCliInvalidBlockNumber = 0x01800000;
  86. public static readonly uint errCliInvalidBlockSize = 0x01900000;
  87. public static readonly uint errCliDownloadSequenceFailed = 0x01A00000;
  88. public static readonly uint errCliInsertRefused = 0x01B00000;
  89. public static readonly uint errCliDeleteRefused = 0x01C00000;
  90. public static readonly uint errCliNeedPassword = 0x01D00000;
  91. public static readonly uint errCliInvalidPassword = 0x01E00000;
  92. public static readonly uint errCliNoPasswordToSetOrClear = 0x01F00000;
  93. public static readonly uint errCliJobTimeout = 0x02000000;
  94. public static readonly uint errCliPartialDataRead = 0x02100000;
  95. public static readonly uint errCliBufferTooSmall = 0x02200000;
  96. public static readonly uint errCliFunctionRefused = 0x02300000;
  97. public static readonly uint errCliDestroying = 0x02400000;
  98. public static readonly uint errCliInvalidParamNumber = 0x02500000;
  99. public static readonly uint errCliCannotChangeParam = 0x02600000;
  100. // Area ID
  101. public static readonly byte S7AreaPE = 0x81;
  102. public static readonly byte S7AreaPA = 0x82;
  103. public static readonly byte S7AreaMK = 0x83;
  104. public static readonly byte S7AreaDB = 0x84;
  105. public static readonly byte S7AreaCT = 0x1C;
  106. public static readonly byte S7AreaTM = 0x1D;
  107. // Word Length
  108. public static readonly int S7WLBit = 0x01;
  109. public static readonly int S7WLByte = 0x02;
  110. public static readonly int S7WLWord = 0x04;
  111. public static readonly int S7WLDWord = 0x06;
  112. public static readonly int S7WLReal = 0x08;
  113. public static readonly int S7WLCounter = 0x1C;
  114. public static readonly int S7WLTimer = 0x1D;
  115. // Block type
  116. public static readonly byte Block_OB = 0x38;
  117. public static readonly byte Block_DB = 0x41;
  118. public static readonly byte Block_SDB = 0x42;
  119. public static readonly byte Block_FC = 0x43;
  120. public static readonly byte Block_SFC = 0x44;
  121. public static readonly byte Block_FB = 0x45;
  122. public static readonly byte Block_SFB = 0x46;
  123. // Sub Block Type
  124. public static readonly byte SubBlk_OB = 0x08;
  125. public static readonly byte SubBlk_DB = 0x0A;
  126. public static readonly byte SubBlk_SDB = 0x0B;
  127. public static readonly byte SubBlk_FC = 0x0C;
  128. public static readonly byte SubBlk_SFC = 0x0D;
  129. public static readonly byte SubBlk_FB = 0x0E;
  130. public static readonly byte SubBlk_SFB = 0x0F;
  131. // Block languages
  132. public static readonly byte BlockLangAWL = 0x01;
  133. public static readonly byte BlockLangKOP = 0x02;
  134. public static readonly byte BlockLangFUP = 0x03;
  135. public static readonly byte BlockLangSCL = 0x04;
  136. public static readonly byte BlockLangDB = 0x05;
  137. public static readonly byte BlockLangGRAPH = 0x06;
  138. // Max number of vars (multiread/write)
  139. public static readonly int MaxVars = 20;
  140. // Client Connection Type
  141. public static readonly UInt16 CONNTYPE_PG = 0x01; // Connect to the PLC as a PG
  142. public static readonly UInt16 CONNTYPE_OP = 0x02; // Connect to the PLC as an OP
  143. public static readonly UInt16 CONNTYPE_BASIC = 0x03; // Basic connection
  144. // Job
  145. private const int JobComplete = 0;
  146. private const int JobPending = 1;
  147. private IntPtr Client;
  148. // Data Item
  149. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  150. public struct S7DataItem
  151. {
  152. public Int32 Area;
  153. public Int32 WordLen;
  154. public Int32 Result;
  155. public Int32 DBNumber;
  156. public Int32 Start;
  157. public Int32 Amount;
  158. public IntPtr pData;
  159. }
  160. // Block List
  161. [StructLayout(LayoutKind.Sequential, Pack = 1)] // <- "maybe" we don't need
  162. public struct S7BlocksList
  163. {
  164. public Int32 OBCount;
  165. public Int32 FBCount;
  166. public Int32 FCCount;
  167. public Int32 SFBCount;
  168. public Int32 SFCCount;
  169. public Int32 DBCount;
  170. public Int32 SDBCount;
  171. };
  172. // Packed Block Info
  173. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  174. protected struct US7BlockInfo
  175. {
  176. public Int32 BlkType;
  177. public Int32 BlkNumber;
  178. public Int32 BlkLang;
  179. public Int32 BlkFlags;
  180. public Int32 MC7Size; // The real size in bytes
  181. public Int32 LoadSize;
  182. public Int32 LocalData;
  183. public Int32 SBBLength;
  184. public Int32 CheckSum;
  185. public Int32 Version;
  186. // Chars info
  187. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
  188. public char[] CodeDate;
  189. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
  190. public char[] IntfDate;
  191. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
  192. public char[] Author;
  193. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
  194. public char[] Family;
  195. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
  196. public char[] Header;
  197. };
  198. private US7BlockInfo UBlockInfo;
  199. // Managed Block Info
  200. public struct S7BlockInfo
  201. {
  202. public int BlkType;
  203. public int BlkNumber;
  204. public int BlkLang;
  205. public int BlkFlags;
  206. public int MC7Size; // The real size in bytes
  207. public int LoadSize;
  208. public int LocalData;
  209. public int SBBLength;
  210. public int CheckSum;
  211. public int Version;
  212. // Chars info
  213. public string CodeDate;
  214. public string IntfDate;
  215. public string Author;
  216. public string Family;
  217. public string Header;
  218. };
  219. public ushort[] TS7BlocksOfType;
  220. // Packed Order Code + Version
  221. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  222. protected struct US7OrderCode
  223. {
  224. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]
  225. public char[] Code;
  226. public byte V1;
  227. public byte V2;
  228. public byte V3;
  229. };
  230. private US7OrderCode UOrderCode;
  231. // Managed Order Code + Version
  232. public struct S7OrderCode
  233. {
  234. public string Code;
  235. public byte V1;
  236. public byte V2;
  237. public byte V3;
  238. };
  239. // Packed CPU Info
  240. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  241. protected struct US7CpuInfo
  242. {
  243. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
  244. public char[] ModuleTypeName;
  245. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
  246. public char[] SerialNumber;
  247. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
  248. public char[] ASName;
  249. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 27)]
  250. public char[] Copyright;
  251. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
  252. public char[] ModuleName;
  253. };
  254. private US7CpuInfo UCpuInfo;
  255. // Managed CPU Info
  256. public struct S7CpuInfo
  257. {
  258. public string ModuleTypeName;
  259. public string SerialNumber;
  260. public string ASName;
  261. public string Copyright;
  262. public string ModuleName;
  263. }
  264. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  265. public struct S7CpInfo
  266. {
  267. public Int32 MaxPduLengt;
  268. public Int32 MaxConnections;
  269. public Int32 MaxMpiRate;
  270. public Int32 MaxBusRate;
  271. };
  272. // See §33.1 of "System Software for S7-300/400 System and Standard Functions"
  273. // and see SFC51 description too
  274. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  275. public struct SZL_HEADER
  276. {
  277. public UInt16 LENTHDR;
  278. public UInt16 N_DR;
  279. };
  280. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  281. public struct S7SZL
  282. {
  283. public SZL_HEADER Header;
  284. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x4000 - 4)]
  285. public byte[] Data;
  286. };
  287. // SZL List of available SZL IDs : same as SZL but List items are big-endian adjusted
  288. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  289. public struct S7SZLList
  290. {
  291. public SZL_HEADER Header;
  292. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x2000 - 2)]
  293. public UInt16[] Data;
  294. };
  295. // S7 Protection
  296. // See §33.19 of "System Software for S7-300/400 System and Standard Functions"
  297. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  298. public struct S7Protection // Packed S7Protection
  299. {
  300. public UInt16 sch_schal;
  301. public UInt16 sch_par;
  302. public UInt16 sch_rel;
  303. public UInt16 bart_sch;
  304. public UInt16 anl_sch;
  305. };
  306. // C++ time struct, functions to convert it from/to DateTime are provided ;-)
  307. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  308. protected struct cpp_tm
  309. {
  310. public Int32 tm_sec;
  311. public Int32 tm_min;
  312. public Int32 tm_hour;
  313. public Int32 tm_mday;
  314. public Int32 tm_mon;
  315. public Int32 tm_year;
  316. public Int32 tm_wday;
  317. public Int32 tm_yday;
  318. public Int32 tm_isdst;
  319. }
  320. private cpp_tm tm;
  321. #endregion
  322. #region [Class Control]
  323. [DllImport(S7Consts.Snap7LibName)]
  324. protected static extern IntPtr Cli_Create();
  325. public S7Client()
  326. {
  327. Client = Cli_Create();
  328. }
  329. [DllImport(S7Consts.Snap7LibName)]
  330. protected static extern int Cli_Destroy(ref IntPtr Client);
  331. ~S7Client()
  332. {
  333. Cli_Destroy(ref Client);
  334. }
  335. [DllImport(S7Consts.Snap7LibName)]
  336. protected static extern int Cli_Connect(IntPtr Client);
  337. public int Connect()
  338. {
  339. return Cli_Connect(Client);
  340. }
  341. [DllImport(S7Consts.Snap7LibName)]
  342. protected static extern int Cli_ConnectTo(IntPtr Client,
  343. [MarshalAs(UnmanagedType.LPStr)] string Address,
  344. int Rack,
  345. int Slot
  346. );
  347. public int ConnectTo(string Address, int Rack, int Slot)
  348. {
  349. return Cli_ConnectTo(Client, Address, Rack, Slot);
  350. }
  351. [DllImport(S7Consts.Snap7LibName)]
  352. protected static extern int Cli_SetConnectionParams(IntPtr Client,
  353. [MarshalAs(UnmanagedType.LPStr)] string Address,
  354. UInt16 LocalTSAP,
  355. UInt16 RemoteTSAP
  356. );
  357. public int SetConnectionParams(string Address, UInt16 LocalTSAP, UInt16 RemoteTSAP)
  358. {
  359. return Cli_SetConnectionParams(Client, Address, LocalTSAP, RemoteTSAP);
  360. }
  361. [DllImport(S7Consts.Snap7LibName)]
  362. protected static extern int Cli_SetConnectionType(IntPtr Client, UInt16 ConnectionType);
  363. public int SetConnectionType(UInt16 ConnectionType)
  364. {
  365. return Cli_SetConnectionType(Client, ConnectionType);
  366. }
  367. [DllImport(S7Consts.Snap7LibName)]
  368. protected static extern int Cli_Disconnect(IntPtr Client);
  369. public int Disconnect()
  370. {
  371. return Cli_Disconnect(Client);
  372. }
  373. // Get/SetParam needs a void* parameter, internally it decides the kind of pointer
  374. // in accord to ParamNumber.
  375. // To avoid the use of unsafe code we split the DLL functions and use overloaded methods.
  376. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_GetParam")]
  377. protected static extern int Cli_GetParam_i16(IntPtr Client, Int32 ParamNumber, ref Int16 IntValue);
  378. public int GetParam(Int32 ParamNumber, ref Int16 IntValue)
  379. {
  380. return Cli_GetParam_i16(Client, ParamNumber, ref IntValue);
  381. }
  382. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_GetParam")]
  383. protected static extern int Cli_GetParam_u16(IntPtr Client, Int32 ParamNumber, ref UInt16 IntValue);
  384. public int GetParam(Int32 ParamNumber, ref UInt16 IntValue)
  385. {
  386. return Cli_GetParam_u16(Client, ParamNumber, ref IntValue);
  387. }
  388. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_GetParam")]
  389. protected static extern int Cli_GetParam_i32(IntPtr Client, Int32 ParamNumber, ref Int32 IntValue);
  390. public int GetParam(Int32 ParamNumber, ref Int32 IntValue)
  391. {
  392. return Cli_GetParam_i32(Client, ParamNumber, ref IntValue);
  393. }
  394. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_GetParam")]
  395. protected static extern int Cli_GetParam_u32(IntPtr Client, Int32 ParamNumber, ref UInt32 IntValue);
  396. public int GetParam(Int32 ParamNumber, ref UInt32 IntValue)
  397. {
  398. return Cli_GetParam_u32(Client, ParamNumber, ref IntValue);
  399. }
  400. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_GetParam")]
  401. protected static extern int Cli_GetParam_i64(IntPtr Client, Int32 ParamNumber, ref Int64 IntValue);
  402. public int GetParam(Int32 ParamNumber, ref Int64 IntValue)
  403. {
  404. return Cli_GetParam_i64(Client, ParamNumber, ref IntValue);
  405. }
  406. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_GetParam")]
  407. protected static extern int Cli_GetParam_u64(IntPtr Client, Int32 ParamNumber, ref UInt64 IntValue);
  408. public int GetParam(Int32 ParamNumber, ref UInt64 IntValue)
  409. {
  410. return Cli_GetParam_u64(Client, ParamNumber, ref IntValue);
  411. }
  412. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_SetParam")]
  413. protected static extern int Cli_SetParam_i16(IntPtr Client, Int32 ParamNumber, ref Int16 IntValue);
  414. public int SetParam(Int32 ParamNumber, ref Int16 IntValue)
  415. {
  416. return Cli_SetParam_i16(Client, ParamNumber, ref IntValue);
  417. }
  418. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_SetParam")]
  419. protected static extern int Cli_SetParam_u16(IntPtr Client, Int32 ParamNumber, ref UInt16 IntValue);
  420. public int SetParam(Int32 ParamNumber, ref UInt16 IntValue)
  421. {
  422. return Cli_SetParam_u16(Client, ParamNumber, ref IntValue);
  423. }
  424. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_SetParam")]
  425. protected static extern int Cli_SetParam_i32(IntPtr Client, Int32 ParamNumber, ref Int32 IntValue);
  426. public int SetParam(Int32 ParamNumber, ref Int32 IntValue)
  427. {
  428. return Cli_SetParam_i32(Client, ParamNumber, ref IntValue);
  429. }
  430. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_SetParam")]
  431. protected static extern int Cli_SetParam_u32(IntPtr Client, Int32 ParamNumber, ref UInt32 IntValue);
  432. public int SetParam(Int32 ParamNumber, ref UInt32 IntValue)
  433. {
  434. return Cli_SetParam_u32(Client, ParamNumber, ref IntValue);
  435. }
  436. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_SetParam")]
  437. protected static extern int Cli_SetParam_i64(IntPtr Client, Int32 ParamNumber, ref Int64 IntValue);
  438. public int SetParam(Int32 ParamNumber, ref Int64 IntValue)
  439. {
  440. return Cli_SetParam_i64(Client, ParamNumber, ref IntValue);
  441. }
  442. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Cli_SetParam")]
  443. protected static extern int Cli_SetParam_u64(IntPtr Client, Int32 ParamNumber, ref UInt64 IntValue);
  444. public int SetParam(Int32 ParamNumber, ref UInt64 IntValue)
  445. {
  446. return Cli_SetParam_u64(Client, ParamNumber, ref IntValue);
  447. }
  448. public delegate void S7CliCompletion(IntPtr usrPtr, int opCode, int opResult);
  449. [DllImport(S7Consts.Snap7LibName)]
  450. protected static extern int Cli_SetAsCallback(IntPtr Client, S7CliCompletion Completion, IntPtr usrPtr);
  451. public int SetAsCallBack(S7CliCompletion Completion, IntPtr usrPtr)
  452. {
  453. return Cli_SetAsCallback(Client, Completion, usrPtr);
  454. }
  455. #endregion
  456. #region [Data I/O main functions]
  457. [DllImport(S7Consts.Snap7LibName)]
  458. protected static extern int Cli_ReadArea(IntPtr Client, int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer);
  459. public int ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
  460. {
  461. return Cli_ReadArea(Client, Area, DBNumber, Start, Amount, WordLen, Buffer);
  462. }
  463. [DllImport(S7Consts.Snap7LibName)]
  464. protected static extern int Cli_WriteArea(IntPtr Client, int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer);
  465. public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
  466. {
  467. return Cli_WriteArea(Client, Area, DBNumber, Start, Amount, WordLen, Buffer);
  468. }
  469. [DllImport(S7Consts.Snap7LibName)]
  470. protected static extern int Cli_ReadMultiVars(IntPtr Client, ref S7DataItem Item, int ItemsCount);
  471. public int ReadMultiVars(ref S7DataItem Item, int ItemsCount)
  472. {
  473. return Cli_ReadMultiVars(Client, ref Item, ItemsCount);
  474. }
  475. [DllImport(S7Consts.Snap7LibName)]
  476. protected static extern int Cli_WriteMultiVars(IntPtr Client, ref S7DataItem Item, int ItemsCount);
  477. public int WriteMultiVars(ref S7DataItem Item, int ItemsCount)
  478. {
  479. return Cli_WriteMultiVars(Client, ref Item, ItemsCount);
  480. }
  481. #endregion
  482. #region [Data I/O lean functions]
  483. [DllImport(S7Consts.Snap7LibName)]
  484. protected static extern int Cli_DBRead(IntPtr Client, int DBNumber, int Start, int Size, byte[] Buffer);
  485. public int DBRead(int DBNumber, int Start, int Size, byte[] Buffer)
  486. {
  487. return Cli_DBRead(Client, DBNumber, Start, Size, Buffer);
  488. }
  489. [DllImport(S7Consts.Snap7LibName)]
  490. protected static extern int Cli_DBWrite(IntPtr Client, int DBNumber, int Start, int Size, byte[] Buffer);
  491. public int DBWrite(int DBNumber, int Start, int Size, byte[] Buffer)
  492. {
  493. return Cli_DBWrite(Client, DBNumber, Start, Size, Buffer);
  494. }
  495. [DllImport(S7Consts.Snap7LibName)]
  496. protected static extern int Cli_MBRead(IntPtr Client, int Start, int Size, byte[] Buffer);
  497. public int MBRead(int Start, int Size, byte[] Buffer)
  498. {
  499. return Cli_MBRead(Client, Start, Size, Buffer);
  500. }
  501. [DllImport(S7Consts.Snap7LibName)]
  502. protected static extern int Cli_MBWrite(IntPtr Client, int Start, int Size, byte[] Buffer);
  503. public int MBWrite(int Start, int Size, byte[] Buffer)
  504. {
  505. return Cli_MBWrite(Client, Start, Size, Buffer);
  506. }
  507. [DllImport(S7Consts.Snap7LibName)]
  508. protected static extern int Cli_EBRead(IntPtr Client, int Start, int Size, byte[] Buffer);
  509. public int EBRead(int Start, int Size, byte[] Buffer)
  510. {
  511. return Cli_EBRead(Client, Start, Size, Buffer);
  512. }
  513. [DllImport(S7Consts.Snap7LibName)]
  514. protected static extern int Cli_EBWrite(IntPtr Client, int Start, int Size, byte[] Buffer);
  515. public int EBWrite(int Start, int Size, byte[] Buffer)
  516. {
  517. return Cli_EBWrite(Client, Start, Size, Buffer);
  518. }
  519. [DllImport(S7Consts.Snap7LibName)]
  520. protected static extern int Cli_ABRead(IntPtr Client, int Start, int Size, byte[] Buffer);
  521. public int ABRead(int Start, int Size, byte[] Buffer)
  522. {
  523. return Cli_ABRead(Client, Start, Size, Buffer);
  524. }
  525. [DllImport(S7Consts.Snap7LibName)]
  526. protected static extern int Cli_ABWrite(IntPtr Client, int Start, int Size, byte[] Buffer);
  527. public int ABWrite(int Start, int Size, byte[] Buffer)
  528. {
  529. return Cli_ABWrite(Client, Start, Size, Buffer);
  530. }
  531. [DllImport(S7Consts.Snap7LibName)]
  532. protected static extern int Cli_TMRead(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  533. public int TMRead(int Start, int Amount, ushort[] Buffer)
  534. {
  535. return Cli_TMRead(Client, Start, Amount, Buffer);
  536. }
  537. [DllImport(S7Consts.Snap7LibName)]
  538. protected static extern int Cli_TMWrite(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  539. public int TMWrite(int Start, int Amount, ushort[] Buffer)
  540. {
  541. return Cli_TMWrite(Client, Start, Amount, Buffer);
  542. }
  543. [DllImport(S7Consts.Snap7LibName)]
  544. protected static extern int Cli_CTRead(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  545. public int CTRead(int Start, int Amount, ushort[] Buffer)
  546. {
  547. return Cli_CTRead(Client, Start, Amount, Buffer);
  548. }
  549. [DllImport(S7Consts.Snap7LibName)]
  550. protected static extern int Cli_CTWrite(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  551. public int CTWrite(int Start, int Amount, ushort[] Buffer)
  552. {
  553. return Cli_CTWrite(Client, Start, Amount, Buffer);
  554. }
  555. #endregion
  556. #region [Directory functions]
  557. [DllImport(S7Consts.Snap7LibName)]
  558. protected static extern int Cli_ListBlocks(IntPtr Client, ref S7BlocksList List);
  559. public int ListBlocks(ref S7BlocksList List)
  560. {
  561. return Cli_ListBlocks(Client, ref List);
  562. }
  563. [DllImport(S7Consts.Snap7LibName)]
  564. protected static extern int Cli_GetAgBlockInfo(IntPtr Client, int BlockType, int BlockNum, ref US7BlockInfo Info);
  565. public int GetAgBlockInfo(IntPtr Client, int BlockType, int BlockNum, ref S7BlockInfo Info)
  566. {
  567. int res = Cli_GetAgBlockInfo(Client, BlockType, BlockNum, ref UBlockInfo);
  568. // Packed->Managed
  569. if (res == 0)
  570. {
  571. Info.BlkType = UBlockInfo.BlkType;
  572. Info.BlkNumber = UBlockInfo.BlkNumber;
  573. Info.BlkLang = UBlockInfo.BlkLang;
  574. Info.BlkFlags = UBlockInfo.BlkFlags;
  575. Info.MC7Size = UBlockInfo.MC7Size;
  576. Info.LoadSize = UBlockInfo.LoadSize;
  577. Info.LocalData = UBlockInfo.LocalData;
  578. Info.SBBLength = UBlockInfo.SBBLength;
  579. Info.CheckSum = UBlockInfo.CheckSum;
  580. Info.Version = UBlockInfo.Version;
  581. // Chars info
  582. Info.CodeDate = new string(UBlockInfo.CodeDate);
  583. Info.IntfDate = new string(UBlockInfo.IntfDate);
  584. Info.Author = new string(UBlockInfo.Author);
  585. Info.Family = new string(UBlockInfo.Family);
  586. Info.Header = new string(UBlockInfo.Header);
  587. }
  588. return res;
  589. }
  590. [DllImport(S7Consts.Snap7LibName)]
  591. protected static extern int Cli_GetPgBlockInfo(IntPtr Client, ref US7BlockInfo Info, byte[] Buffer, Int32 Size);
  592. public int GetPgBlockInfo(ref S7BlockInfo Info, byte[] Buffer, int Size)
  593. {
  594. int res = Cli_GetPgBlockInfo(Client, ref UBlockInfo, Buffer, Size);
  595. // Packed->Managed
  596. if (res == 0)
  597. {
  598. Info.BlkType = UBlockInfo.BlkType;
  599. Info.BlkNumber = UBlockInfo.BlkNumber;
  600. Info.BlkLang = UBlockInfo.BlkLang;
  601. Info.BlkFlags = UBlockInfo.BlkFlags;
  602. Info.MC7Size = UBlockInfo.MC7Size;
  603. Info.LoadSize = UBlockInfo.LoadSize;
  604. Info.LocalData = UBlockInfo.LocalData;
  605. Info.SBBLength = UBlockInfo.SBBLength;
  606. Info.CheckSum = UBlockInfo.CheckSum;
  607. Info.Version = UBlockInfo.Version;
  608. // Chars info
  609. Info.CodeDate = new string(UBlockInfo.CodeDate);
  610. Info.IntfDate = new string(UBlockInfo.IntfDate);
  611. Info.Author = new string(UBlockInfo.Author);
  612. Info.Family = new string(UBlockInfo.Family);
  613. Info.Header = new string(UBlockInfo.Header);
  614. }
  615. return res;
  616. }
  617. [DllImport(S7Consts.Snap7LibName)]
  618. protected static extern int Cli_ListBlocksOfType(IntPtr Client, Int32 BlockType, ushort[] List, ref int ItemsCount);
  619. public int ListBlocksOfType(int BlockType, ushort[] List, ref int ItemsCount)
  620. {
  621. return Cli_ListBlocksOfType(Client, BlockType, List, ref ItemsCount);
  622. }
  623. #endregion
  624. #region [Blocks functions]
  625. [DllImport(S7Consts.Snap7LibName)]
  626. protected static extern int Cli_Upload(IntPtr Client, int BlockType, int BlockNum, byte[] UsrData, ref int Size);
  627. public int Upload(int BlockType, int BlockNum, byte[] UsrData, ref int Size)
  628. {
  629. return Cli_Upload(Client, BlockType, BlockNum, UsrData, ref Size);
  630. }
  631. [DllImport(S7Consts.Snap7LibName)]
  632. protected static extern int Cli_FullUpload(IntPtr Client, int BlockType, int BlockNum, byte[] UsrData, ref int Size);
  633. public int FullUpload(int BlockType, int BlockNum, byte[] UsrData, ref int Size)
  634. {
  635. return Cli_FullUpload(Client, BlockType, BlockNum, UsrData, ref Size);
  636. }
  637. [DllImport(S7Consts.Snap7LibName)]
  638. protected static extern int Cli_Download(IntPtr Client, int BlockNum, byte[] UsrData, int Size);
  639. public int Download(int BlockNum, byte[] UsrData, int Size)
  640. {
  641. return Cli_Download(Client, BlockNum, UsrData, Size);
  642. }
  643. [DllImport(S7Consts.Snap7LibName)]
  644. protected static extern int Cli_Delete(IntPtr Client, int BlockType, int BlockNum);
  645. public int Delete(int BlockType, int BlockNum)
  646. {
  647. return Cli_Delete(Client, BlockType, BlockNum);
  648. }
  649. [DllImport(S7Consts.Snap7LibName)]
  650. protected static extern int Cli_DBGet(IntPtr Client, int DBNumber, byte[] UsrData, ref int Size);
  651. public int DBGet(int DBNumber, byte[] UsrData, ref int Size)
  652. {
  653. return Cli_DBGet(Client, DBNumber, UsrData, ref Size);
  654. }
  655. [DllImport(S7Consts.Snap7LibName)]
  656. protected static extern int Cli_DBFill(IntPtr Client, int DBNumber, int FillChar);
  657. public int DBFill(int DBNumber, int FillChar)
  658. {
  659. return Cli_DBFill(Client, DBNumber, FillChar);
  660. }
  661. #endregion
  662. #region [Date/Time functions]
  663. [DllImport(S7Consts.Snap7LibName)]
  664. protected static extern int Cli_GetPlcDateTime(IntPtr Client, ref cpp_tm tm);
  665. public int GetPlcDateTime(ref DateTime DT)
  666. {
  667. int res = Cli_GetPlcDateTime(Client, ref tm);
  668. if (res == 0)
  669. {
  670. // Packed->Managed
  671. DateTime PlcDT = new DateTime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  672. DT = PlcDT;
  673. }
  674. return res;
  675. }
  676. [DllImport(S7Consts.Snap7LibName)]
  677. protected static extern int Cli_SetPlcDateTime(IntPtr Client, ref cpp_tm tm);
  678. public int SetPlcDateTime(DateTime DT)
  679. {
  680. // Managed->Packed
  681. tm.tm_year = DT.Year - 1900;
  682. tm.tm_mon = DT.Month-1;
  683. tm.tm_mday = DT.Day;
  684. tm.tm_hour = DT.Hour;
  685. tm.tm_min = DT.Minute;
  686. tm.tm_sec = DT.Second;
  687. return Cli_SetPlcDateTime(Client, ref tm);
  688. }
  689. [DllImport(S7Consts.Snap7LibName)]
  690. protected static extern int Cli_SetPlcSystemDateTime(IntPtr Client);
  691. public int SetPlcSystemDateTime()
  692. {
  693. return Cli_SetPlcSystemDateTime(Client);
  694. }
  695. #endregion
  696. #region [System Info functions]
  697. [DllImport(S7Consts.Snap7LibName)]
  698. protected static extern int Cli_GetOrderCode(IntPtr Client, ref US7OrderCode Info);
  699. public int GetOrderCode(ref S7OrderCode Info)
  700. {
  701. int res = Cli_GetOrderCode(Client, ref UOrderCode);
  702. // Packed->Managed
  703. if (res == 0)
  704. {
  705. Info.Code = new string(UOrderCode.Code);
  706. Info.V1 = UOrderCode.V1;
  707. Info.V2 = UOrderCode.V2;
  708. Info.V3 = UOrderCode.V3;
  709. }
  710. return res;
  711. }
  712. [DllImport(S7Consts.Snap7LibName)]
  713. protected static extern int Cli_GetCpuInfo(IntPtr Client, ref US7CpuInfo Info);
  714. public int GetCpuInfo(ref S7CpuInfo Info)
  715. {
  716. int res = Cli_GetCpuInfo(Client, ref UCpuInfo);
  717. // Packed->Managed
  718. if (res == 0)
  719. {
  720. Info.ModuleTypeName = new string(UCpuInfo.ModuleTypeName);
  721. Info.SerialNumber = new string(UCpuInfo.SerialNumber);
  722. Info.ASName = new string(UCpuInfo.ASName);
  723. Info.Copyright = new string(UCpuInfo.Copyright);
  724. Info.ModuleName = new string(UCpuInfo.ModuleName);
  725. }
  726. return res;
  727. }
  728. [DllImport(S7Consts.Snap7LibName)]
  729. protected static extern int Cli_GetCpInfo(IntPtr Client, ref S7CpInfo Info);
  730. public int GetCpInfo(ref S7CpInfo Info)
  731. {
  732. return Cli_GetCpInfo(Client, ref Info);
  733. }
  734. [DllImport(S7Consts.Snap7LibName)]
  735. protected static extern int Cli_ReadSZL(IntPtr Client, int ID, int Index, ref S7SZL Data, ref Int32 Size);
  736. public int ReadSZL(int ID, int Index, ref S7SZL Data, ref Int32 Size)
  737. {
  738. return Cli_ReadSZL(Client, ID, Index, ref Data, ref Size);
  739. }
  740. [DllImport(S7Consts.Snap7LibName)]
  741. protected static extern int Cli_ReadSZLList(IntPtr Client, ref S7SZLList List, ref Int32 ItemsCount);
  742. public int ReadSZLList(ref S7SZLList List, ref Int32 ItemsCount)
  743. {
  744. return Cli_ReadSZLList(Client, ref List, ref ItemsCount);
  745. }
  746. #endregion
  747. #region [Control functions]
  748. [DllImport(S7Consts.Snap7LibName)]
  749. protected static extern int Cli_PlcHotStart(IntPtr Client);
  750. public int PlcHotStart()
  751. {
  752. return Cli_PlcHotStart(Client);
  753. }
  754. [DllImport(S7Consts.Snap7LibName)]
  755. protected static extern int Cli_PlcColdStart(IntPtr Client);
  756. public int PlcColdStart()
  757. {
  758. return Cli_PlcColdStart(Client);
  759. }
  760. [DllImport(S7Consts.Snap7LibName)]
  761. protected static extern int Cli_PlcStop(IntPtr Client);
  762. public int PlcStop()
  763. {
  764. return Cli_PlcStop(Client);
  765. }
  766. [DllImport(S7Consts.Snap7LibName)]
  767. protected static extern int Cli_CopyRamToRom(IntPtr Client, UInt32 Timeout);
  768. public int PlcCopyRamToRom(UInt32 Timeout)
  769. {
  770. return Cli_CopyRamToRom(Client, Timeout);
  771. }
  772. [DllImport(S7Consts.Snap7LibName)]
  773. protected static extern int Cli_Compress(IntPtr Client, UInt32 Timeout);
  774. public int PlcCompress(UInt32 Timeout)
  775. {
  776. return Cli_Compress(Client, Timeout);
  777. }
  778. [DllImport(S7Consts.Snap7LibName)]
  779. protected static extern int Cli_GetPlcStatus(IntPtr Client, ref Int32 Status);
  780. public int PlcGetStatus(ref Int32 Status)
  781. {
  782. return Cli_GetPlcStatus(Client, ref Status);
  783. }
  784. #endregion
  785. #region [Security functions]
  786. [DllImport(S7Consts.Snap7LibName)]
  787. protected static extern int Cli_GetProtection(IntPtr Client, ref S7Protection Protection);
  788. public int GetProtection(ref S7Protection Protection)
  789. {
  790. return Cli_GetProtection(Client, ref Protection);
  791. }
  792. [DllImport(S7Consts.Snap7LibName)]
  793. protected static extern int Cli_SetSessionPassword(IntPtr Client, [MarshalAs(UnmanagedType.LPStr)] string Password);
  794. public int SetSessionPassword(string Password)
  795. {
  796. return Cli_SetSessionPassword(Client, Password);
  797. }
  798. [DllImport(S7Consts.Snap7LibName)]
  799. protected static extern int Cli_ClearSessionPassword(IntPtr Client);
  800. public int ClearSessionPassword()
  801. {
  802. return Cli_ClearSessionPassword(Client);
  803. }
  804. #endregion
  805. #region [Low Level]
  806. [DllImport(S7Consts.Snap7LibName)]
  807. protected static extern int Cli_IsoExchangeBuffer(IntPtr Client, byte[] Buffer, ref Int32 Size);
  808. public int IsoExchangeBuffer(byte[] Buffer, ref Int32 Size)
  809. {
  810. return Cli_IsoExchangeBuffer(Client, Buffer, ref Size);
  811. }
  812. #endregion
  813. #region [Async functions]
  814. [DllImport(S7Consts.Snap7LibName)]
  815. protected static extern int Cli_AsReadArea(IntPtr Client, int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer);
  816. public int AsReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
  817. {
  818. return Cli_AsReadArea(Client, Area, DBNumber, Start, Amount, WordLen, Buffer);
  819. }
  820. [DllImport(S7Consts.Snap7LibName)]
  821. protected static extern int Cli_AsWriteArea(IntPtr Client, int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer);
  822. public int AsWriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
  823. {
  824. return Cli_AsWriteArea(Client, Area, DBNumber, Start, Amount, WordLen, Buffer);
  825. }
  826. [DllImport(S7Consts.Snap7LibName)]
  827. protected static extern int Cli_AsDBRead(IntPtr Client, int DBNumber, int Start, int Size, byte[] Buffer);
  828. public int AsDBRead(int DBNumber, int Start, int Size, byte[] Buffer)
  829. {
  830. return Cli_AsDBRead(Client, DBNumber, Start, Size, Buffer);
  831. }
  832. [DllImport(S7Consts.Snap7LibName)]
  833. protected static extern int Cli_AsDBWrite(IntPtr Client, int DBNumber, int Start, int Size, byte[] Buffer);
  834. public int AsDBWrite(int DBNumber, int Start, int Size, byte[] Buffer)
  835. {
  836. return Cli_AsDBWrite(Client, DBNumber, Start, Size, Buffer);
  837. }
  838. [DllImport(S7Consts.Snap7LibName)]
  839. protected static extern int Cli_AsMBRead(IntPtr Client, int Start, int Size, byte[] Buffer);
  840. public int AsMBRead(int Start, int Size, byte[] Buffer)
  841. {
  842. return Cli_AsMBRead(Client, Start, Size, Buffer);
  843. }
  844. [DllImport(S7Consts.Snap7LibName)]
  845. protected static extern int Cli_AsMBWrite(IntPtr Client, int Start, int Size, byte[] Buffer);
  846. public int AsMBWrite(int Start, int Size, byte[] Buffer)
  847. {
  848. return Cli_AsMBWrite(Client, Start, Size, Buffer);
  849. }
  850. [DllImport(S7Consts.Snap7LibName)]
  851. protected static extern int Cli_AsEBRead(IntPtr Client, int Start, int Size, byte[] Buffer);
  852. public int AsEBRead(int Start, int Size, byte[] Buffer)
  853. {
  854. return Cli_AsEBRead(Client, Start, Size, Buffer);
  855. }
  856. [DllImport(S7Consts.Snap7LibName)]
  857. protected static extern int Cli_AsEBWrite(IntPtr Client, int Start, int Size, byte[] Buffer);
  858. public int AsEBWrite(int Start, int Size, byte[] Buffer)
  859. {
  860. return Cli_AsEBWrite(Client, Start, Size, Buffer);
  861. }
  862. [DllImport(S7Consts.Snap7LibName)]
  863. protected static extern int Cli_AsABRead(IntPtr Client, int Start, int Size, byte[] Buffer);
  864. public int AsABRead(int Start, int Size, byte[] Buffer)
  865. {
  866. return Cli_AsABRead(Client, Start, Size, Buffer);
  867. }
  868. [DllImport(S7Consts.Snap7LibName)]
  869. protected static extern int Cli_AsABWrite(IntPtr Client, int Start, int Size, byte[] Buffer);
  870. public int AsABWrite(int Start, int Size, byte[] Buffer)
  871. {
  872. return Cli_AsABWrite(Client, Start, Size, Buffer);
  873. }
  874. [DllImport(S7Consts.Snap7LibName)]
  875. protected static extern int Cli_AsTMRead(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  876. public int AsTMRead(int Start, int Amount, ushort[] Buffer)
  877. {
  878. return Cli_AsTMRead(Client, Start, Amount, Buffer);
  879. }
  880. [DllImport(S7Consts.Snap7LibName)]
  881. protected static extern int Cli_AsTMWrite(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  882. public int AsTMWrite(int Start, int Amount, ushort[] Buffer)
  883. {
  884. return Cli_AsTMWrite(Client, Start, Amount, Buffer);
  885. }
  886. [DllImport(S7Consts.Snap7LibName)]
  887. protected static extern int Cli_AsCTRead(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  888. public int AsCTRead(int Start, int Amount, ushort[] Buffer)
  889. {
  890. return Cli_AsCTRead(Client, Start, Amount, Buffer);
  891. }
  892. [DllImport(S7Consts.Snap7LibName)]
  893. protected static extern int Cli_AsCTWrite(IntPtr Client, int Start, int Amount, ushort[] Buffer);
  894. public int AsCTWrite(int Start, int Amount, ushort[] Buffer)
  895. {
  896. return Cli_AsCTWrite(Client, Start, Amount, Buffer);
  897. }
  898. [DllImport(S7Consts.Snap7LibName)]
  899. protected static extern int Cli_AsListBlocksOfType(IntPtr Client, Int32 BlockType, ushort[] List);
  900. public int AsListBlocksOfType(int BlockType, ushort[] List)
  901. {
  902. return Cli_AsListBlocksOfType(Client, BlockType, List);
  903. }
  904. [DllImport(S7Consts.Snap7LibName)]
  905. protected static extern int Cli_AsReadSZL(IntPtr Client, int ID, int Index, ref S7SZL Data, ref Int32 Size);
  906. public int AsReadSZL(int ID, int Index, ref S7SZL Data, ref Int32 Size)
  907. {
  908. return Cli_AsReadSZL(Client, ID, Index, ref Data, ref Size);
  909. }
  910. [DllImport(S7Consts.Snap7LibName)]
  911. protected static extern int Cli_AsReadSZLList(IntPtr Client, ref S7SZLList List, ref Int32 ItemsCount);
  912. public int AsReadSZLList(ref S7SZLList List, ref Int32 ItemsCount)
  913. {
  914. return Cli_AsReadSZLList(Client, ref List, ref ItemsCount);
  915. }
  916. [DllImport(S7Consts.Snap7LibName)]
  917. protected static extern int Cli_AsUpload(IntPtr Client, int BlockType, int BlockNum, byte[] UsrData, ref int Size);
  918. public int AsUpload(int BlockType, int BlockNum, byte[] UsrData, ref int Size)
  919. {
  920. return Cli_AsUpload(Client, BlockType, BlockNum, UsrData, ref Size);
  921. }
  922. [DllImport(S7Consts.Snap7LibName)]
  923. protected static extern int Cli_AsFullUpload(IntPtr Client, int BlockType, int BlockNum, byte[] UsrData, ref int Size);
  924. public int AsFullUpload(int BlockType, int BlockNum, byte[] UsrData, ref int Size)
  925. {
  926. return Cli_AsFullUpload(Client, BlockType, BlockNum, UsrData, ref Size);
  927. }
  928. [DllImport(S7Consts.Snap7LibName)]
  929. protected static extern int Cli_AsDownload(IntPtr Client, int BlockNum, byte[] UsrData, int Size);
  930. public int ASDownload(int BlockNum, byte[] UsrData, int Size)
  931. {
  932. return Cli_AsDownload(Client, BlockNum, UsrData, Size);
  933. }
  934. [DllImport(S7Consts.Snap7LibName)]
  935. protected static extern int Cli_AsPlcCopyRamToRom(IntPtr Client, UInt32 Timeout);
  936. public int AsPlcCopyRamToRom(UInt32 Timeout)
  937. {
  938. return Cli_AsPlcCopyRamToRom(Client, Timeout);
  939. }
  940. [DllImport(S7Consts.Snap7LibName)]
  941. protected static extern int Cli_AsPlcCompress(IntPtr Client, UInt32 Timeout);
  942. public int AsPlcCompress(UInt32 Timeout)
  943. {
  944. return Cli_AsPlcCompress(Client, Timeout);
  945. }
  946. [DllImport(S7Consts.Snap7LibName)]
  947. protected static extern int Cli_AsDBGet(IntPtr Client, int DBNumber, byte[] UsrData, ref int Size);
  948. public int AsDBGet(int DBNumber, byte[] UsrData, ref int Size)
  949. {
  950. return Cli_AsDBGet(Client, DBNumber, UsrData, ref Size);
  951. }
  952. [DllImport(S7Consts.Snap7LibName)]
  953. protected static extern int Cli_AsDBFill(IntPtr Client, int DBNumber, int FillChar);
  954. public int AsDBFill(int DBNumber, int FillChar)
  955. {
  956. return Cli_AsDBFill(Client, DBNumber, FillChar);
  957. }
  958. [DllImport(S7Consts.Snap7LibName)]
  959. protected static extern int Cli_CheckAsCompletion(IntPtr Client, ref Int32 opResult);
  960. public bool CheckAsCompletion(ref int opResult)
  961. {
  962. return Cli_CheckAsCompletion(Client, ref opResult) == JobComplete;
  963. }
  964. [DllImport(S7Consts.Snap7LibName)]
  965. protected static extern int Cli_WaitAsCompletion(IntPtr Client, Int32 Timeout);
  966. public int WaitAsCompletion(int Timeout)
  967. {
  968. return Cli_WaitAsCompletion(Client, Timeout);
  969. }
  970. #endregion
  971. #region [Info Functions]
  972. [DllImport(S7Consts.Snap7LibName)]
  973. protected static extern int Cli_GetExecTime(IntPtr Client, ref UInt32 Time);
  974. public int ExecTime()
  975. {
  976. UInt32 Time = new UInt32();
  977. if (Cli_GetExecTime(Client, ref Time) == 0)
  978. return (int)(Time);
  979. else
  980. return -1;
  981. }
  982. [DllImport(S7Consts.Snap7LibName)]
  983. protected static extern int Cli_GetLastError(IntPtr Client, ref Int32 LastError);
  984. public int LastError()
  985. {
  986. Int32 ClientLastError = new Int32();
  987. if (Cli_GetLastError(Client, ref ClientLastError) == 0)
  988. return (int)ClientLastError;
  989. else
  990. return -1;
  991. }
  992. [DllImport(S7Consts.Snap7LibName)]
  993. protected static extern int Cli_GetPduLength(IntPtr Client, ref Int32 Requested, ref Int32 Negotiated);
  994. public int RequestedPduLength()
  995. {
  996. Int32 Requested = new Int32();
  997. Int32 Negotiated = new Int32();
  998. if (Cli_GetPduLength(Client, ref Requested, ref Negotiated) == 0)
  999. return Requested;
  1000. else
  1001. return -1;
  1002. }
  1003. public int NegotiatedPduLength()
  1004. {
  1005. Int32 Requested = new Int32();
  1006. Int32 Negotiated = new Int32();
  1007. if (Cli_GetPduLength(Client, ref Requested, ref Negotiated) == 0)
  1008. return Negotiated;
  1009. else
  1010. return -1;
  1011. }
  1012. [DllImport(S7Consts.Snap7LibName, CharSet = CharSet.Ansi)]
  1013. protected static extern int Cli_ErrorText(int Error, StringBuilder ErrMsg, int TextSize);
  1014. public string ErrorText(int Error)
  1015. {
  1016. StringBuilder Message = new StringBuilder(MsgTextLen);
  1017. Cli_ErrorText(Error, Message, MsgTextLen);
  1018. return Message.ToString();
  1019. }
  1020. [DllImport(S7Consts.Snap7LibName)]
  1021. protected static extern int Cli_GetConnected(IntPtr Client, ref UInt32 IsConnected);
  1022. public bool Connected()
  1023. {
  1024. UInt32 IsConnected = new UInt32();
  1025. if (Cli_GetConnected(Client, ref IsConnected) == 0)
  1026. return IsConnected!=0;
  1027. else
  1028. return false;
  1029. }
  1030. #endregion
  1031. }
  1032. public class S7Server
  1033. {
  1034. #region [Constants, private vars and TypeDefs]
  1035. private const int MsgTextLen = 1024;
  1036. private const int mkEvent = 0;
  1037. private const int mkLog = 1;
  1038. // Server Area ID (use with Register/unregister - Lock/unlock Area)
  1039. public static readonly int srvAreaPE = 0;
  1040. public static readonly int srvAreaPA = 1;
  1041. public static readonly int srvAreaMK = 2;
  1042. public static readonly int srvAreaCT = 3;
  1043. public static readonly int srvAreaTM = 4;
  1044. public static readonly int srvAreaDB = 5;
  1045. // Errors
  1046. public static readonly uint errSrvCannotStart = 0x00100000; // Server cannot start
  1047. public static readonly uint errSrvDBNullPointer = 0x00200000; // Passed null as PData
  1048. public static readonly uint errSrvAreaAlreadyExists = 0x00300000; // Area Re-registration
  1049. public static readonly uint errSrvUnknownArea = 0x00400000; // Unknown area
  1050. public static readonly uint errSrvInvalidParams = 0x00500000; // Invalid param(s) supplied
  1051. public static readonly uint errSrvTooManyDB = 0x00600000; // Cannot register DB
  1052. public static readonly uint errSrvInvalidParamNumber = 0x00700000; // Invalid param (srv_get/set_param)
  1053. public static readonly uint errSrvCannotChangeParam = 0x00800000; // Cannot change because running
  1054. // TCP Server Event codes
  1055. public static readonly uint evcServerStarted = 0x00000001;
  1056. public static readonly uint evcServerStopped = 0x00000002;
  1057. public static readonly uint evcListenerCannotStart = 0x00000004;
  1058. public static readonly uint evcClientAdded = 0x00000008;
  1059. public static readonly uint evcClientRejected = 0x00000010;
  1060. public static readonly uint evcClientNoRoom = 0x00000020;
  1061. public static readonly uint evcClientException = 0x00000040;
  1062. public static readonly uint evcClientDisconnected = 0x00000080;
  1063. public static readonly uint evcClientTerminated = 0x00000100;
  1064. public static readonly uint evcClientsDropped = 0x00000200;
  1065. public static readonly uint evcReserved_00000400 = 0x00000400; // actually unused
  1066. public static readonly uint evcReserved_00000800 = 0x00000800; // actually unused
  1067. public static readonly uint evcReserved_00001000 = 0x00001000; // actually unused
  1068. public static readonly uint evcReserved_00002000 = 0x00002000; // actually unused
  1069. public static readonly uint evcReserved_00004000 = 0x00004000; // actually unused
  1070. public static readonly uint evcReserved_00008000 = 0x00008000; // actually unused
  1071. // S7 Server Event Code
  1072. public static readonly uint evcPDUincoming = 0x00010000;
  1073. public static readonly uint evcDataRead = 0x00020000;
  1074. public static readonly uint evcDataWrite = 0x00040000;
  1075. public static readonly uint evcNegotiatePDU = 0x00080000;
  1076. public static readonly uint evcReadSZL = 0x00100000;
  1077. public static readonly uint evcClock = 0x00200000;
  1078. public static readonly uint evcUpload = 0x00400000;
  1079. public static readonly uint evcDownload = 0x00800000;
  1080. public static readonly uint evcDirectory = 0x01000000;
  1081. public static readonly uint evcSecurity = 0x02000000;
  1082. public static readonly uint evcControl = 0x04000000;
  1083. public static readonly uint evcReserved_08000000 = 0x08000000; // actually unused
  1084. public static readonly uint evcReserved_10000000 = 0x10000000; // actually unused
  1085. public static readonly uint evcReserved_20000000 = 0x20000000; // actually unused
  1086. public static readonly uint evcReserved_40000000 = 0x40000000; // actually unused
  1087. public static readonly uint evcReserved_80000000 = 0x80000000; // actually unused
  1088. // Masks to enable/disable all events
  1089. public static readonly uint evcAll = 0xFFFFFFFF;
  1090. public static readonly uint evcNone = 0x00000000;
  1091. // Event SubCodes
  1092. public static readonly ushort evsUnknown = 0x0000;
  1093. public static readonly ushort evsStartUpload = 0x0001;
  1094. public static readonly ushort evsStartDownload = 0x0001;
  1095. public static readonly ushort evsGetBlockList = 0x0001;
  1096. public static readonly ushort evsStartListBoT = 0x0002;
  1097. public static readonly ushort evsListBoT = 0x0003;
  1098. public static readonly ushort evsGetBlockInfo = 0x0004;
  1099. public static readonly ushort evsGetClock = 0x0001;
  1100. public static readonly ushort evsSetClock = 0x0002;
  1101. public static readonly ushort evsSetPassword = 0x0001;
  1102. public static readonly ushort evsClrPassword = 0x0002;
  1103. // Event Params : functions group
  1104. public static readonly ushort grProgrammer = 0x0041;
  1105. public static readonly ushort grCyclicData = 0x0042;
  1106. public static readonly ushort grBlocksInfo = 0x0043;
  1107. public static readonly ushort grSZL = 0x0044;
  1108. public static readonly ushort grPassword = 0x0045;
  1109. public static readonly ushort grBSend = 0x0046;
  1110. public static readonly ushort grClock = 0x0047;
  1111. public static readonly ushort grSecurity = 0x0045;
  1112. // Event Params : control codes
  1113. public static readonly ushort CodeControlUnknown = 0x0000;
  1114. public static readonly ushort CodeControlColdStart = 0x0001;
  1115. public static readonly ushort CodeControlWarmStart = 0x0002;
  1116. public static readonly ushort CodeControlStop = 0x0003;
  1117. public static readonly ushort CodeControlCompress = 0x0004;
  1118. public static readonly ushort CodeControlCpyRamRom = 0x0005;
  1119. public static readonly ushort CodeControlInsDel = 0x0006;
  1120. // Event Result
  1121. public static readonly ushort evrNoError = 0x0000;
  1122. public static readonly ushort evrFragmentRejected = 0x0001;
  1123. public static readonly ushort evrMalformedPDU = 0x0002;
  1124. public static readonly ushort evrSparseBytes = 0x0003;
  1125. public static readonly ushort evrCannotHandlePDU = 0x0004;
  1126. public static readonly ushort evrNotImplemented = 0x0005;
  1127. public static readonly ushort evrErrException = 0x0006;
  1128. public static readonly ushort evrErrAreaNotFound = 0x0007;
  1129. public static readonly ushort evrErrOutOfRange = 0x0008;
  1130. public static readonly ushort evrErrOverPDU = 0x0009;
  1131. public static readonly ushort evrErrTransportSize = 0x000A;
  1132. public static readonly ushort evrInvalidGroupUData = 0x000B;
  1133. public static readonly ushort evrInvalidSZL = 0x000C;
  1134. public static readonly ushort evrDataSizeMismatch = 0x000D;
  1135. public static readonly ushort evrCannotUpload = 0x000E;
  1136. public static readonly ushort evrCannotDownload = 0x000F;
  1137. public static readonly ushort evrUploadInvalidID = 0x0010;
  1138. public static readonly ushort evrResNotFound = 0x0011;
  1139. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  1140. public struct USrvEvent
  1141. {
  1142. public IntPtr EvtTime; // It's platform dependent (32 or 64 bit)
  1143. public Int32 EvtSender;
  1144. public UInt32 EvtCode;
  1145. public ushort EvtRetCode;
  1146. public ushort EvtParam1;
  1147. public ushort EvtParam2;
  1148. public ushort EvtParam3;
  1149. public ushort EvtParam4;
  1150. }
  1151. public struct SrvEvent
  1152. {
  1153. public DateTime EvtTime;
  1154. public Int32 EvtSender;
  1155. public UInt32 EvtCode;
  1156. public ushort EvtRetCode;
  1157. public ushort EvtParam1;
  1158. public ushort EvtParam2;
  1159. public ushort EvtParam3;
  1160. public ushort EvtParam4;
  1161. }
  1162. private IntPtr Server;
  1163. #endregion
  1164. #region [Class Control]
  1165. [DllImport(S7Consts.Snap7LibName)]
  1166. protected static extern IntPtr Srv_Create();
  1167. public S7Server()
  1168. {
  1169. Server = Srv_Create();
  1170. }
  1171. [DllImport(S7Consts.Snap7LibName)]
  1172. protected static extern int Srv_Destroy(ref IntPtr Server);
  1173. ~S7Server()
  1174. {
  1175. Srv_Destroy(ref Server);
  1176. }
  1177. [DllImport(S7Consts.Snap7LibName)]
  1178. protected static extern int Srv_StartTo(IntPtr Server, [MarshalAs(UnmanagedType.LPStr)] string Address);
  1179. public int StartTo(string Address)
  1180. {
  1181. return Srv_StartTo(Server, Address);
  1182. }
  1183. [DllImport(S7Consts.Snap7LibName)]
  1184. protected static extern int Srv_Start(IntPtr Server);
  1185. public int Start()
  1186. {
  1187. return Srv_Start(Server);
  1188. }
  1189. [DllImport(S7Consts.Snap7LibName)]
  1190. protected static extern int Srv_Stop(IntPtr Server);
  1191. public int Stop()
  1192. {
  1193. return Srv_Stop(Server);
  1194. }
  1195. // Get/SetParam needs a void* parameter, internally it decides the kind of pointer
  1196. // in accord to ParamNumber.
  1197. // To avoid the use of unsafe code we split the DLL functions and use overloaded methods.
  1198. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_GetParam")]
  1199. protected static extern int Srv_GetParam_i16(IntPtr Server, Int32 ParamNumber, ref Int16 IntValue);
  1200. public int GetParam(Int32 ParamNumber, ref Int16 IntValue)
  1201. {
  1202. return Srv_GetParam_i16(Server, ParamNumber, ref IntValue);
  1203. }
  1204. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_GetParam")]
  1205. protected static extern int Srv_GetParam_u16(IntPtr Server, Int32 ParamNumber, ref UInt16 IntValue);
  1206. public int GetParam(Int32 ParamNumber, ref UInt16 IntValue)
  1207. {
  1208. return Srv_GetParam_u16(Server, ParamNumber, ref IntValue);
  1209. }
  1210. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_GetParam")]
  1211. protected static extern int Srv_GetParam_i32(IntPtr Server, Int32 ParamNumber, ref Int32 IntValue);
  1212. public int GetParam(Int32 ParamNumber, ref Int32 IntValue)
  1213. {
  1214. return Srv_GetParam_i32(Server, ParamNumber, ref IntValue);
  1215. }
  1216. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_GetParam")]
  1217. protected static extern int Srv_GetParam_u32(IntPtr Server, Int32 ParamNumber, ref UInt32 IntValue);
  1218. public int GetParam(Int32 ParamNumber, ref UInt32 IntValue)
  1219. {
  1220. return Srv_GetParam_u32(Server, ParamNumber, ref IntValue);
  1221. }
  1222. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_GetParam")]
  1223. protected static extern int Srv_GetParam_i64(IntPtr Server, Int32 ParamNumber, ref Int64 IntValue);
  1224. public int GetParam(Int32 ParamNumber, ref Int64 IntValue)
  1225. {
  1226. return Srv_GetParam_i64(Server, ParamNumber, ref IntValue);
  1227. }
  1228. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_GetParam")]
  1229. protected static extern int Srv_GetParam_u64(IntPtr Server, Int32 ParamNumber, ref UInt64 IntValue);
  1230. public int GetParam(Int32 ParamNumber, ref UInt64 IntValue)
  1231. {
  1232. return Srv_GetParam_u64(Server, ParamNumber, ref IntValue);
  1233. }
  1234. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_SetParam")]
  1235. protected static extern int Srv_SetParam_i16(IntPtr Server, Int32 ParamNumber, ref Int16 IntValue);
  1236. public int SetParam(Int32 ParamNumber, ref Int16 IntValue)
  1237. {
  1238. return Srv_SetParam_i16(Server, ParamNumber, ref IntValue);
  1239. }
  1240. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_SetParam")]
  1241. protected static extern int Srv_SetParam_u16(IntPtr Server, Int32 ParamNumber, ref UInt16 IntValue);
  1242. public int SetParam(Int32 ParamNumber, ref UInt16 IntValue)
  1243. {
  1244. return Srv_SetParam_u16(Server, ParamNumber, ref IntValue);
  1245. }
  1246. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_SetParam")]
  1247. protected static extern int Srv_SetParam_i32(IntPtr Server, Int32 ParamNumber, ref Int32 IntValue);
  1248. public int SetParam(Int32 ParamNumber, ref Int32 IntValue)
  1249. {
  1250. return Srv_SetParam_i32(Server, ParamNumber, ref IntValue);
  1251. }
  1252. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_SetParam")]
  1253. protected static extern int Srv_SetParam_u32(IntPtr Server, Int32 ParamNumber, ref UInt32 IntValue);
  1254. public int SetParam(Int32 ParamNumber, ref UInt32 IntValue)
  1255. {
  1256. return Srv_SetParam_u32(Server, ParamNumber, ref IntValue);
  1257. }
  1258. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_SetParam")]
  1259. protected static extern int Srv_SetParam_i64(IntPtr Server, Int32 ParamNumber, ref Int64 IntValue);
  1260. public int SetParam(Int32 ParamNumber, ref Int64 IntValue)
  1261. {
  1262. return Srv_SetParam_i64(Server, ParamNumber, ref IntValue);
  1263. }
  1264. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Srv_SetParam")]
  1265. protected static extern int Srv_SetParam_u64(IntPtr Server, Int32 ParamNumber, ref UInt64 IntValue);
  1266. public int SetParam(Int32 ParamNumber, ref UInt64 IntValue)
  1267. {
  1268. return Srv_SetParam_u64(Server, ParamNumber, ref IntValue);
  1269. }
  1270. #endregion
  1271. #region [Data Areas functions]
  1272. [DllImport(S7Consts.Snap7LibName)]
  1273. protected static extern int Srv_RegisterArea(IntPtr Server, Int32 AreaCode, Int32 Index, byte[] pUsrData, Int32 Size);
  1274. public int RegisterArea(Int32 AreaCode, Int32 Index, byte[] pUsrData, Int32 Size)
  1275. {
  1276. return Srv_RegisterArea(Server, AreaCode, Index, pUsrData, Size);
  1277. }
  1278. [DllImport(S7Consts.Snap7LibName)]
  1279. protected static extern int Srv_UnregisterArea(IntPtr Server, Int32 AreaCode, Int32 Index);
  1280. public int UnregisterArea(Int32 AreaCode, Int32 Index)
  1281. {
  1282. return Srv_UnregisterArea(Server, AreaCode, Index);
  1283. }
  1284. [DllImport(S7Consts.Snap7LibName)]
  1285. protected static extern int Srv_LockArea(IntPtr Server, Int32 AreaCode, Int32 Index);
  1286. public int LockArea(Int32 AreaCode, Int32 Index)
  1287. {
  1288. return Srv_LockArea(Server, AreaCode, Index);
  1289. }
  1290. [DllImport(S7Consts.Snap7LibName)]
  1291. protected static extern int Srv_UnlockArea(IntPtr Server, Int32 AreaCode, Int32 Index);
  1292. public int UnlockArea(Int32 AreaCode, Int32 Index)
  1293. {
  1294. return Srv_UnlockArea(Server, AreaCode, Index);
  1295. }
  1296. #endregion
  1297. #region [Notification functions (Events)]
  1298. public delegate void TSrvCallback(IntPtr usrPtr, ref USrvEvent Event, int Size);
  1299. [DllImport(S7Consts.Snap7LibName)]
  1300. protected static extern int Srv_SetEventsCallback(IntPtr Server, TSrvCallback Callback, IntPtr usrPtr);
  1301. public int SetEventsCallBack(TSrvCallback Callback, IntPtr usrPtr)
  1302. {
  1303. return Srv_SetEventsCallback(Server, Callback, usrPtr);
  1304. }
  1305. [DllImport(S7Consts.Snap7LibName)]
  1306. protected static extern int Srv_SetReadEventsCallback(IntPtr Server, TSrvCallback Callback, IntPtr usrPtr);
  1307. public int SetReadEventsCallBack(TSrvCallback Callback, IntPtr usrPtr)
  1308. {
  1309. return Srv_SetReadEventsCallback(Server, Callback, usrPtr);
  1310. }
  1311. [DllImport(S7Consts.Snap7LibName)]
  1312. protected static extern int Srv_PickEvent(IntPtr Server, ref USrvEvent Event, ref Int32 EvtReady);
  1313. public bool PickEvent(ref USrvEvent Event)
  1314. {
  1315. Int32 EvtReady = new Int32();
  1316. if (Srv_PickEvent(Server, ref Event, ref EvtReady) == 0)
  1317. return EvtReady != 0;
  1318. else
  1319. return false;
  1320. }
  1321. [DllImport(S7Consts.Snap7LibName)]
  1322. protected static extern int Srv_ClearEvents(IntPtr Server);
  1323. public int ClearEvents()
  1324. {
  1325. return Srv_ClearEvents(Server);
  1326. }
  1327. [DllImport(S7Consts.Snap7LibName, CharSet = CharSet.Ansi)]
  1328. protected static extern int Srv_EventText(ref USrvEvent Event, StringBuilder EvtMsg, int TextSize);
  1329. public string EventText(ref USrvEvent Event)
  1330. {
  1331. StringBuilder Message = new StringBuilder(MsgTextLen);
  1332. Srv_EventText(ref Event, Message, MsgTextLen);
  1333. return Message.ToString();
  1334. }
  1335. public DateTime EvtTimeToDateTime(IntPtr TimeStamp)
  1336. {
  1337. DateTime UnixStartEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  1338. return UnixStartEpoch.AddSeconds(Convert.ToDouble(TimeStamp));
  1339. }
  1340. [DllImport(S7Consts.Snap7LibName)]
  1341. protected static extern int Srv_GetMask(IntPtr Server, Int32 MaskKind, ref UInt32 Mask);
  1342. [DllImport(S7Consts.Snap7LibName)]
  1343. protected static extern int Srv_SetMask(IntPtr Server, Int32 MaskKind, UInt32 Mask);
  1344. // Property LogMask R/W
  1345. public UInt32 LogMask {
  1346. get
  1347. {
  1348. UInt32 Mask = new UInt32();
  1349. if (Srv_GetMask(Server, S7Server.mkLog, ref Mask)==0)
  1350. return Mask;
  1351. else
  1352. return 0;
  1353. }
  1354. set
  1355. {
  1356. Srv_SetMask(Server, S7Server.mkLog, value);
  1357. }
  1358. }
  1359. // Property EventMask R/W
  1360. public UInt32 EventMask
  1361. {
  1362. get
  1363. {
  1364. UInt32 Mask = new UInt32();
  1365. if (Srv_GetMask(Server, S7Server.mkEvent, ref Mask) == 0)
  1366. return Mask;
  1367. else
  1368. return 0;
  1369. }
  1370. set
  1371. {
  1372. Srv_SetMask(Server, S7Server.mkEvent, value);
  1373. }
  1374. }
  1375. #endregion
  1376. #region [Info functions]
  1377. [DllImport(S7Consts.Snap7LibName)]
  1378. protected static extern int Srv_GetStatus(IntPtr Server, ref Int32 ServerStatus, ref Int32 CpuStatus, ref Int32 ClientsCount);
  1379. [DllImport(S7Consts.Snap7LibName)]
  1380. protected static extern int Srv_SetCpuStatus(IntPtr Server, Int32 CpuStatus);
  1381. // Property Virtual CPU status R/W
  1382. public int CpuStatus
  1383. {
  1384. get
  1385. {
  1386. Int32 CStatus = new Int32();
  1387. Int32 SStatus = new Int32();
  1388. Int32 CCount = new Int32();
  1389. if (Srv_GetStatus(Server, ref CStatus, ref SStatus, ref CCount) == 0)
  1390. return CStatus;
  1391. else
  1392. return -1;
  1393. }
  1394. set
  1395. {
  1396. Srv_SetCpuStatus(Server, value);
  1397. }
  1398. }
  1399. // Property Server Status Read Only
  1400. public int ServerStatus
  1401. {
  1402. get
  1403. {
  1404. Int32 CStatus = new Int32();
  1405. Int32 SStatus = new Int32();
  1406. Int32 CCount = new Int32();
  1407. if (Srv_GetStatus(Server, ref CStatus, ref SStatus, ref CCount) == 0)
  1408. return SStatus;
  1409. else
  1410. return -1;
  1411. }
  1412. }
  1413. // Property Clients Count Read Only
  1414. public int ClientsCount
  1415. {
  1416. get
  1417. {
  1418. Int32 CStatus = new Int32();
  1419. Int32 SStatus = new Int32();
  1420. Int32 CCount = new Int32();
  1421. if (Srv_GetStatus(Server, ref CStatus, ref SStatus, ref CCount) == 0)
  1422. return CCount;
  1423. else
  1424. return -1;
  1425. }
  1426. }
  1427. [DllImport(S7Consts.Snap7LibName, CharSet = CharSet.Ansi)]
  1428. protected static extern int Srv_ErrorText(int Error, StringBuilder ErrMsg, int TextSize);
  1429. public string ErrorText(int Error)
  1430. {
  1431. StringBuilder Message = new StringBuilder(MsgTextLen);
  1432. Srv_ErrorText(Error, Message, MsgTextLen);
  1433. return Message.ToString();
  1434. }
  1435. #endregion
  1436. }
  1437. public class S7Partner
  1438. {
  1439. #region [Constants, private vars and TypeDefs]
  1440. private const int MsgTextLen = 1024;
  1441. // Status
  1442. public static readonly int par_stopped = 0; // stopped
  1443. public static readonly int par_connecting = 1; // running and active connecting
  1444. public static readonly int par_waiting = 2; // running and waiting for a connection
  1445. public static readonly int par_linked = 3; // running and connected : linked
  1446. public static readonly int par_sending = 4; // sending data
  1447. public static readonly int par_receiving = 5; // receiving data
  1448. public static readonly int par_binderror = 6; // error starting passive server
  1449. // Errors
  1450. public static readonly uint errParAddressInUse = 0x00200000;
  1451. public static readonly uint errParNoRoom = 0x00300000;
  1452. public static readonly uint errServerNoRoom = 0x00400000;
  1453. public static readonly uint errParInvalidParams = 0x00500000;
  1454. public static readonly uint errParNotLinked = 0x00600000;
  1455. public static readonly uint errParBusy = 0x00700000;
  1456. public static readonly uint errParFrameTimeout = 0x00800000;
  1457. public static readonly uint errParInvalidPDU = 0x00900000;
  1458. public static readonly uint errParSendTimeout = 0x00A00000;
  1459. public static readonly uint errParRecvTimeout = 0x00B00000;
  1460. public static readonly uint errParSendRefused = 0x00C00000;
  1461. public static readonly uint errParNegotiatingPDU = 0x00D00000;
  1462. public static readonly uint errParSendingBlock = 0x00E00000;
  1463. public static readonly uint errParRecvingBlock = 0x00F00000;
  1464. public static readonly uint errBindError = 0x01000000;
  1465. public static readonly uint errParDestroying = 0x01100000;
  1466. public static readonly uint errParInvalidParamNumber = 0x01200000;
  1467. public static readonly uint errParCannotChangeParam = 0x01300000;
  1468. // Generic byte buffer structure, you may need to declare a more
  1469. // specialistic one in your program.
  1470. // It's used to cast the input pointer that cames from the callback.
  1471. // See the passive partned demo and the delegate S7ParRecvCallback.
  1472. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  1473. public struct S7Buffer
  1474. {
  1475. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x8000)]
  1476. public byte[] Data;
  1477. }
  1478. // Job status
  1479. private const int JobComplete = 0;
  1480. private const int JobPending = 1;
  1481. private IntPtr Partner;
  1482. private Int32 parBytesSent;
  1483. private Int32 parBytesRecv;
  1484. private Int32 parSendErrors;
  1485. private Int32 parRecvErrors;
  1486. #endregion
  1487. #region [Class Control]
  1488. [DllImport(S7Consts.Snap7LibName)]
  1489. protected static extern IntPtr Par_Create(Int32 ParActive);
  1490. public S7Partner(int Active)
  1491. {
  1492. Partner= Par_Create(Active);
  1493. }
  1494. [DllImport(S7Consts.Snap7LibName)]
  1495. protected static extern int Par_Destroy(ref IntPtr Partner);
  1496. ~S7Partner()
  1497. {
  1498. Par_Destroy(ref Partner);
  1499. }
  1500. [DllImport(S7Consts.Snap7LibName)]
  1501. protected static extern int Par_StartTo(
  1502. IntPtr Partner,
  1503. [MarshalAs(UnmanagedType.LPStr)] string LocalAddress,
  1504. [MarshalAs(UnmanagedType.LPStr)] string RemoteAddress,
  1505. UInt16 LocalTSAP,
  1506. UInt16 RemoteTSAP);
  1507. public int StartTo(
  1508. string LocalAddress,
  1509. string RemoteAddress,
  1510. UInt16 LocalTSAP,
  1511. UInt16 RemoteTSAP)
  1512. {
  1513. return Par_StartTo(Partner, LocalAddress, RemoteAddress, LocalTSAP, RemoteTSAP);
  1514. }
  1515. [DllImport(S7Consts.Snap7LibName)]
  1516. protected static extern int Par_Start(IntPtr Partner);
  1517. public int Start()
  1518. {
  1519. return Par_Start(Partner);
  1520. }
  1521. [DllImport(S7Consts.Snap7LibName)]
  1522. protected static extern int Par_Stop(IntPtr Partner);
  1523. public int Stop()
  1524. {
  1525. return Par_Stop(Partner);
  1526. }
  1527. // Get/SetParam needs a void* parameter, internally it decides the kind of pointer
  1528. // in accord to ParamNumber.
  1529. // To avoid the use of unsafe code we split the DLL functions and use overloaded methods.
  1530. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_GetParam")]
  1531. protected static extern int Par_GetParam_i16(IntPtr Partner, Int32 ParamNumber, ref Int16 IntValue);
  1532. public int GetParam(Int32 ParamNumber, ref Int16 IntValue)
  1533. {
  1534. return Par_GetParam_i16(Partner, ParamNumber, ref IntValue);
  1535. }
  1536. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_GetParam")]
  1537. protected static extern int Par_GetParam_u16(IntPtr Partner, Int32 ParamNumber, ref UInt16 IntValue);
  1538. public int GetParam(Int32 ParamNumber, ref UInt16 IntValue)
  1539. {
  1540. return Par_GetParam_u16(Partner, ParamNumber, ref IntValue);
  1541. }
  1542. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_GetParam")]
  1543. protected static extern int Par_GetParam_i32(IntPtr Partner, Int32 ParamNumber, ref Int32 IntValue);
  1544. public int GetParam(Int32 ParamNumber, ref Int32 IntValue)
  1545. {
  1546. return Par_GetParam_i32(Partner, ParamNumber, ref IntValue);
  1547. }
  1548. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_GetParam")]
  1549. protected static extern int Par_GetParam_u32(IntPtr Partner, Int32 ParamNumber, ref UInt32 IntValue);
  1550. public int GetParam(Int32 ParamNumber, ref UInt32 IntValue)
  1551. {
  1552. return Par_GetParam_u32(Partner, ParamNumber, ref IntValue);
  1553. }
  1554. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_GetParam")]
  1555. protected static extern int Par_GetParam_i64(IntPtr Partner, Int32 ParamNumber, ref Int64 IntValue);
  1556. public int GetParam(Int32 ParamNumber, ref Int64 IntValue)
  1557. {
  1558. return Par_GetParam_i64(Partner, ParamNumber, ref IntValue);
  1559. }
  1560. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_GetParam")]
  1561. protected static extern int Par_GetParam_u64(IntPtr Partner, Int32 ParamNumber, ref UInt64 IntValue);
  1562. public int GetParam(Int32 ParamNumber, ref UInt64 IntValue)
  1563. {
  1564. return Par_GetParam_u64(Partner, ParamNumber, ref IntValue);
  1565. }
  1566. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_SetParam")]
  1567. protected static extern int Par_SetParam_i16(IntPtr Partner, Int32 ParamNumber, ref Int16 IntValue);
  1568. public int SetParam(Int32 ParamNumber, ref Int16 IntValue)
  1569. {
  1570. return Par_SetParam_i16(Partner, ParamNumber, ref IntValue);
  1571. }
  1572. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_SetParam")]
  1573. protected static extern int Par_SetParam_u16(IntPtr Partner, Int32 ParamNumber, ref UInt16 IntValue);
  1574. public int SetParam(Int32 ParamNumber, ref UInt16 IntValue)
  1575. {
  1576. return Par_SetParam_u16(Partner, ParamNumber, ref IntValue);
  1577. }
  1578. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_SetParam")]
  1579. protected static extern int Par_SetParam_i32(IntPtr Partner, Int32 ParamNumber, ref Int32 IntValue);
  1580. public int SetParam(Int32 ParamNumber, ref Int32 IntValue)
  1581. {
  1582. return Par_SetParam_i32(Partner, ParamNumber, ref IntValue);
  1583. }
  1584. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_SetParam")]
  1585. protected static extern int Par_SetParam_u32(IntPtr Partner, Int32 ParamNumber, ref UInt32 IntValue);
  1586. public int SetParam(Int32 ParamNumber, ref UInt32 IntValue)
  1587. {
  1588. return Par_SetParam_u32(Partner, ParamNumber, ref IntValue);
  1589. }
  1590. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_SetParam")]
  1591. protected static extern int Par_SetParam_i64(IntPtr Partner, Int32 ParamNumber, ref Int64 IntValue);
  1592. public int SetParam(Int32 ParamNumber, ref Int64 IntValue)
  1593. {
  1594. return Par_SetParam_i64(Partner, ParamNumber, ref IntValue);
  1595. }
  1596. [DllImport(S7Consts.Snap7LibName, EntryPoint = "Par_SetParam")]
  1597. protected static extern int Par_SetParam_u64(IntPtr Partner, Int32 ParamNumber, ref UInt64 IntValue);
  1598. public int SetParam(Int32 ParamNumber, ref UInt64 IntValue)
  1599. {
  1600. return Par_SetParam_u64(Partner, ParamNumber, ref IntValue);
  1601. }
  1602. #endregion
  1603. #region [Data I/O functions : BSend]
  1604. [DllImport(S7Consts.Snap7LibName)]
  1605. protected static extern int Par_BSend(IntPtr Partner, UInt32 R_ID, byte[] Buffer, Int32 Size);
  1606. public int BSend(UInt32 R_ID, byte[] Buffer, Int32 Size)
  1607. {
  1608. return Par_BSend(Partner, R_ID, Buffer, Size);
  1609. }
  1610. [DllImport(S7Consts.Snap7LibName)]
  1611. protected static extern int Par_AsBSend(IntPtr Partner, UInt32 R_ID, byte[] Buffer, Int32 Size);
  1612. public int AsBSend(UInt32 R_ID, byte[] Buffer, Int32 Size)
  1613. {
  1614. return Par_AsBSend(Partner, R_ID, Buffer, Size);
  1615. }
  1616. [DllImport(S7Consts.Snap7LibName)]
  1617. protected static extern int Par_CheckAsBSendCompletion(IntPtr Partner, ref Int32 opResult);
  1618. public bool CheckAsBSendCompletion(ref int opResult)
  1619. {
  1620. return Par_CheckAsBSendCompletion(Partner, ref opResult)==JobComplete;
  1621. }
  1622. [DllImport(S7Consts.Snap7LibName)]
  1623. protected static extern int Par_WaitAsBSendCompletion(IntPtr Partner, Int32 Timeout);
  1624. public int WaitAsBSendCompletion(int Timeout)
  1625. {
  1626. return Par_WaitAsBSendCompletion(Partner, Timeout);
  1627. }
  1628. public delegate void S7ParSendCompletion(IntPtr usrPtr, int opResult);
  1629. [DllImport(S7Consts.Snap7LibName)]
  1630. protected static extern int Par_SetSendCallback(IntPtr Partner, S7ParSendCompletion Completion, IntPtr usrPtr);
  1631. public int SetSendCallBack(S7ParSendCompletion Completion, IntPtr usrPtr)
  1632. {
  1633. return Par_SetSendCallback(Partner, Completion, usrPtr);
  1634. }
  1635. #endregion
  1636. #region [Data I/O functions : BRecv]
  1637. [DllImport(S7Consts.Snap7LibName)]
  1638. protected static extern int Par_BRecv(IntPtr Partner, ref UInt32 R_ID, byte[] Buffer, ref Int32 Size, UInt32 Timeout);
  1639. public int BRecv(ref UInt32 R_ID, byte[] Buffer, ref Int32 Size, UInt32 Timeout)
  1640. {
  1641. return Par_BRecv(Partner, ref R_ID, Buffer, ref Size, Timeout);
  1642. }
  1643. [DllImport(S7Consts.Snap7LibName)]
  1644. protected static extern int Par_CheckAsBRecvCompletion(IntPtr Partner, ref Int32 opResult, ref UInt32 R_ID, byte[] Buffer, ref Int32 Size);
  1645. public bool CheckAsBRecvCompletion(ref Int32 opResult, ref UInt32 R_ID, byte[] Buffer, ref Int32 Size)
  1646. {
  1647. Par_CheckAsBRecvCompletion(Partner, ref opResult, ref R_ID, Buffer, ref Size);
  1648. return opResult == JobComplete;
  1649. }
  1650. public delegate void S7ParRecvCallback(IntPtr usrPtr, int opResult, uint R_ID, IntPtr pData, int Size);
  1651. [DllImport(S7Consts.Snap7LibName)]
  1652. protected static extern int Par_SetRecvCallback(IntPtr Partner, S7ParRecvCallback Callback, IntPtr usrPtr);
  1653. public int SetRecvCallback(S7ParRecvCallback Callback, IntPtr usrPtr)
  1654. {
  1655. return Par_SetRecvCallback(Partner, Callback, usrPtr);
  1656. }
  1657. #endregion
  1658. #region [Info functions]
  1659. [DllImport(S7Consts.Snap7LibName)]
  1660. protected static extern int Par_GetLastError(IntPtr Partner, ref Int32 LastError);
  1661. public int LastError(ref Int32 LastError)
  1662. {
  1663. Int32 PartnerLastError = new Int32();
  1664. if (Par_GetLastError(Partner, ref PartnerLastError) == 0)
  1665. return (int)PartnerLastError;
  1666. else
  1667. return -1;
  1668. }
  1669. [DllImport(S7Consts.Snap7LibName, CharSet = CharSet.Ansi)]
  1670. protected static extern int Par_ErrorText(int Error, StringBuilder ErrMsg, int TextSize);
  1671. public string ErrorText(int Error)
  1672. {
  1673. StringBuilder Message = new StringBuilder(MsgTextLen);
  1674. Par_ErrorText(Error, Message, MsgTextLen);
  1675. return Message.ToString();
  1676. }
  1677. [DllImport(S7Consts.Snap7LibName)]
  1678. protected static extern int Par_GetStats(IntPtr Partner, ref Int32 BytesSent, ref Int32 BytesRecv,
  1679. ref Int32 SendErrors, ref Int32 RecvErrors);
  1680. private void GetStatistics()
  1681. {
  1682. if (Par_GetStats(Partner, ref parBytesSent, ref parBytesRecv, ref parSendErrors, ref parRecvErrors) != 0)
  1683. {
  1684. parBytesSent = -1;
  1685. parBytesRecv = -1;
  1686. parSendErrors = -1;
  1687. parRecvErrors = -1;
  1688. }
  1689. }
  1690. public int BytesSent
  1691. {
  1692. get
  1693. {
  1694. GetStatistics();
  1695. return parBytesSent;
  1696. }
  1697. }
  1698. public int BytesRecv
  1699. {
  1700. get
  1701. {
  1702. GetStatistics();
  1703. return parBytesRecv;
  1704. }
  1705. }
  1706. public int SendErrors
  1707. {
  1708. get
  1709. {
  1710. GetStatistics();
  1711. return parSendErrors;
  1712. }
  1713. }
  1714. public int RecvErrors
  1715. {
  1716. get
  1717. {
  1718. GetStatistics();
  1719. return parRecvErrors;
  1720. }
  1721. }
  1722. [DllImport(S7Consts.Snap7LibName)]
  1723. protected static extern int Par_GetStatus(IntPtr Partner, ref Int32 Status);
  1724. public int Status
  1725. {
  1726. get
  1727. {
  1728. int ParStatus = new int();
  1729. if (Par_GetStatus(Partner, ref ParStatus) != 0)
  1730. return -1;
  1731. else
  1732. return ParStatus;
  1733. }
  1734. }
  1735. // simply useful
  1736. public bool Linked
  1737. {
  1738. get
  1739. {
  1740. return Status == par_linked;
  1741. }
  1742. }
  1743. #endregion
  1744. }
  1745. }