@litmusio/miravo-cli-linux-arm64 0.0.0-stage → 0.15.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/LICENSE +199 -0
  2. package/NOTICE +9 -0
  3. package/README.md +6 -2
  4. package/THIRD_PARTY_LICENSES.txt +15289 -0
  5. package/THIRD_PARTY_NOTICES.md +995 -0
  6. package/console-dist/assets/AnimatePresence-DWGUB15i.js +9 -0
  7. package/console-dist/assets/activity-DruTYzT1.js +1 -0
  8. package/console-dist/assets/api-V5Tf5xfh.js +57 -0
  9. package/console-dist/assets/arrow-up-right-DOENs04V.js +1 -0
  10. package/console-dist/assets/asset-detail-D_icTa2K.js +1 -0
  11. package/console-dist/assets/asset-detail-actions-DIYego9D.js +1 -0
  12. package/console-dist/assets/backup-power-Q84DVs_z.js +1 -0
  13. package/console-dist/assets/badge-CdRB4Nd_.js +1 -0
  14. package/console-dist/assets/boiler-s_q0j7rz.js +1 -0
  15. package/console-dist/assets/by-path-Cm2TEP7z.js +1 -0
  16. package/console-dist/assets/check-BTUv4z-H.js +1 -0
  17. package/console-dist/assets/chevron-down-CqA_2_CH.js +1 -0
  18. package/console-dist/assets/chevron-right-CVrgKBLR.js +1 -0
  19. package/console-dist/assets/chiller-r7DzAhJq.js +1 -0
  20. package/console-dist/assets/chunk-6KYCQHYJ-ga3Yekab.js +3 -0
  21. package/console-dist/assets/circle-x-BRkh0A1P.js +1 -0
  22. package/console-dist/assets/commercial-building-Cezt2R6W.js +1 -0
  23. package/console-dist/assets/compressor-ChZLMC94.js +1 -0
  24. package/console-dist/assets/console-panel-BnSjjW6l.js +1 -0
  25. package/console-dist/assets/dialog-BYkdndf9.js +1 -0
  26. package/console-dist/assets/district-heating-By_6QdGQ.js +1 -0
  27. package/console-dist/assets/dom-BxZbg3NX.js +1 -0
  28. package/console-dist/assets/download-CvjDP5mu.js +1 -0
  29. package/console-dist/assets/fira-code-cyrillic-ext-wght-normal-DhYMMuQd.woff2 +0 -0
  30. package/console-dist/assets/fira-code-cyrillic-wght-normal-Y3u8pIsh.woff2 +0 -0
  31. package/console-dist/assets/fira-code-greek-ext-wght-normal-wWus70Ix.woff2 +0 -0
  32. package/console-dist/assets/fira-code-greek-wght-normal-B2SviObF.woff2 +0 -0
  33. package/console-dist/assets/fira-code-latin-ext-wght-normal-Dvfvaomy.woff2 +0 -0
  34. package/console-dist/assets/fira-code-latin-wght-normal-CHoedHDv.woff2 +0 -0
  35. package/console-dist/assets/fira-code-symbols2-wght-normal-CE6EOz_n.woff2 +0 -0
  36. package/console-dist/assets/floor-page-DIUJBBhh.js +1 -0
  37. package/console-dist/assets/floor-page-E-0txA4z.css +1 -0
  38. package/console-dist/assets/format-YLUJ7AXt.js +1 -0
  39. package/console-dist/assets/genset-C6_EUoFo.js +1 -0
  40. package/console-dist/assets/index-BrlZMcSu.js +15 -0
  41. package/console-dist/assets/index-eZjXAdLh.css +2 -0
  42. package/console-dist/assets/loader-circle-w3T2fxvn.js +1 -0
  43. package/console-dist/assets/login-CpbaxAVs.js +136 -0
  44. package/console-dist/assets/not-found-CZjkulKD.js +1 -0
  45. package/console-dist/assets/pharma-cleanroom-CT2M_Mbh.js +1 -0
  46. package/console-dist/assets/preload-helper-7y9sWmk5.js +1 -0
  47. package/console-dist/assets/react-DRt7cCXI.js +1 -0
  48. package/console-dist/assets/rolldown-runtime-BWaJ3C7D.js +1 -0
  49. package/console-dist/assets/rtu-ByogcRPI.js +1 -0
  50. package/console-dist/assets/runtime-link-D88Db2wY.js +1 -0
  51. package/console-dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
  52. package/console-dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
  53. package/console-dist/assets/server-B0q1NVkV.js +1 -0
  54. package/console-dist/assets/shallow-mIK1Qu8X.js +1 -0
  55. package/console-dist/assets/smart-factory-gfMWfMWM.js +1 -0
  56. package/console-dist/assets/source-host-miravo-BW-SKMTe.js +1 -0
  57. package/console-dist/assets/stages-EYRT12Yp.js +1 -0
  58. package/console-dist/assets/status-DQYLUbXs.js +1 -0
  59. package/console-dist/assets/tank-Btg5piqB.js +1 -0
  60. package/console-dist/assets/tooltip-Dfmuq2yz.js +1 -0
  61. package/console-dist/assets/trash-2-BAh5-hxl.js +1 -0
  62. package/console-dist/assets/triangle-alert-3Ab-dBrN.js +1 -0
  63. package/console-dist/assets/use-engine-command-mutation-BmSUqrCp.js +1 -0
  64. package/console-dist/assets/use-transition-navigate-DIfW9U34.js +2 -0
  65. package/console-dist/assets/useQuery-yLeYL5Es.js +1 -0
  66. package/console-dist/assets/utils-D6wrZy-b.js +1 -0
  67. package/console-dist/assets/validation-DEb_QCoK.js +1 -0
  68. package/console-dist/assets/vendor-charts-Bquh2dWE.js +56 -0
  69. package/console-dist/assets/vendor-table-5YnGQUhO.js +4 -0
  70. package/console-dist/assets/vendor-ui-BGeFlRLR.js +1 -0
  71. package/console-dist/assets/water-treatment-DbSVGWoj.js +1 -0
  72. package/console-dist/icon.png +0 -0
  73. package/console-dist/index.html +53 -0
  74. package/console-dist/miravo-logo.webp +0 -0
  75. package/licenses/EPL-2.0.txt +80 -0
  76. package/licenses/GCC-exception-3.1.txt +33 -0
  77. package/licenses/GPL-2.0-only.txt +117 -0
  78. package/licenses/GPL-3.0-only.txt +232 -0
  79. package/licenses/LGPL-2.1-only.txt +176 -0
  80. package/licenses/MPL-2.0.txt +373 -0
  81. package/licenses/OFL-1.1.txt +93 -0
  82. package/licenses/overrides/@headlessui+vue@1.7.23.txt +24 -0
  83. package/licenses/overrides/@next+env@16.4.6.txt +24 -0
  84. package/licenses/overrides/@radix-ui+react-compose-refs@1.1.2.txt +24 -0
  85. package/licenses/overrides/@radix-ui+react-context@1.1.2.txt +24 -0
  86. package/licenses/overrides/@radix-ui+react-direction@1.1.1.txt +24 -0
  87. package/licenses/overrides/@radix-ui+react-id@1.1.1.txt +24 -0
  88. package/licenses/overrides/@radix-ui+react-use-callback-ref@1.1.1.txt +24 -0
  89. package/licenses/overrides/@radix-ui+react-use-escape-keydown@1.1.1.txt +24 -0
  90. package/licenses/overrides/@radix-ui+react-use-layout-effect@1.1.1.txt +24 -0
  91. package/licenses/overrides/@radix-ui+react-use-rect@1.1.1.txt +24 -0
  92. package/licenses/overrides/@radix-ui+react-use-size@1.1.1.txt +24 -0
  93. package/licenses/overrides/@radix-ui+rect@1.1.1.txt +24 -0
  94. package/licenses/overrides/@replit+codemirror-css-color-picker@6.3.0.txt +6 -0
  95. package/licenses/overrides/@scalar+openapi-types@0.8.0.txt +24 -0
  96. package/licenses/overrides/@scalar+openapi-types@0.9.10.txt +24 -0
  97. package/licenses/overrides/@scalar+openapi-types@0.9.7.txt +24 -0
  98. package/licenses/overrides/@scalar+openapi-types@0.9.9.txt +24 -0
  99. package/licenses/overrides/@scalar+postman-to-openapi@0.7.1.txt +24 -0
  100. package/licenses/overrides/@scalar+schemas@0.19.0.txt +24 -0
  101. package/licenses/overrides/@scalar+workspace-store@0.46.3.txt +24 -0
  102. package/licenses/overrides/@scalar+workspace-store@0.75.0.txt +24 -0
  103. package/licenses/overrides/assert-plus@1.0.0.txt +24 -0
  104. package/licenses/overrides/client-only@0.0.1.txt +24 -0
  105. package/licenses/overrides/embla-carousel-react@8.6.0.txt +24 -0
  106. package/licenses/overrides/embla-carousel-reactive-utils@8.6.0.txt +24 -0
  107. package/licenses/overrides/embla-carousel@8.6.0.txt +24 -0
  108. package/licenses/overrides/fastdom@1.0.12.txt +12 -0
  109. package/licenses/overrides/precond@0.2.3.txt +22 -0
  110. package/licenses/overrides/react-remove-scroll-bar@2.3.8.txt +24 -0
  111. package/licenses/overrides/strictdom@1.0.1.txt +12 -0
  112. package/licenses/overrides/victory-vendor@36.9.2.txt +33 -0
  113. package/licenses/runtime/Bun-1.4.3-LICENSE.md +85 -0
  114. package/licenses/runtime/ICU-78.1-LICENSE.txt +568 -0
  115. package/licenses/runtime/bun-components/boringssl.txt +238 -0
  116. package/licenses/runtime/bun-components/brotli.txt +19 -0
  117. package/licenses/runtime/bun-components/c-ares.txt +24 -0
  118. package/licenses/runtime/bun-components/highway.txt +371 -0
  119. package/licenses/runtime/bun-components/libarchive.txt +65 -0
  120. package/licenses/runtime/bun-components/libbase64.txt +28 -0
  121. package/licenses/runtime/bun-components/libdeflate.txt +22 -0
  122. package/licenses/runtime/bun-components/libjpeg-turbo.txt +147 -0
  123. package/licenses/runtime/bun-components/libspng.txt +25 -0
  124. package/licenses/runtime/bun-components/libuv.txt +19 -0
  125. package/licenses/runtime/bun-components/libwebp.txt +30 -0
  126. package/licenses/runtime/bun-components/llvm.txt +311 -0
  127. package/licenses/runtime/bun-components/lol-html.txt +27 -0
  128. package/licenses/runtime/bun-components/ls-hpack.txt +21 -0
  129. package/licenses/runtime/bun-components/ls-qpack.txt +21 -0
  130. package/licenses/runtime/bun-components/lsquic-chrome.txt +32 -0
  131. package/licenses/runtime/bun-components/lsquic.txt +21 -0
  132. package/licenses/runtime/bun-components/mimalloc.txt +21 -0
  133. package/licenses/runtime/bun-components/picohttpparser.txt +24 -0
  134. package/licenses/runtime/bun-components/simdutf-apache.txt +201 -0
  135. package/licenses/runtime/bun-components/simdutf-mit.txt +18 -0
  136. package/licenses/runtime/bun-components/tigerbeetle.txt +177 -0
  137. package/licenses/runtime/bun-components/tinycc.txt +504 -0
  138. package/licenses/runtime/bun-components/typescript-go.txt +55 -0
  139. package/licenses/runtime/bun-components/usockets.txt +201 -0
  140. package/licenses/runtime/bun-components/uucode.txt +30 -0
  141. package/licenses/runtime/bun-components/uwebsockets.txt +201 -0
  142. package/licenses/runtime/bun-components/xxhash.txt +31 -0
  143. package/licenses/runtime/bun-components/zlib-ng.txt +19 -0
  144. package/licenses/runtime/bun-components/zstd-copying.txt +339 -0
  145. package/licenses/runtime/bun-components/zstd.txt +30 -0
  146. package/licenses/runtime/musl-1.2.6-COPYRIGHT.txt +193 -0
  147. package/miravo +4 -0
  148. package/native/S7-NOTICES.txt +11 -0
  149. package/native/miravo-s7.node +0 -0
  150. package/native/s7-source/CMakeLists.txt +23 -0
  151. package/native/s7-source/NODE-SNAP7-LICENSE +24 -0
  152. package/native/s7-source/addon.cpp +124 -0
  153. package/native/s7-source/bridge.cpp +150 -0
  154. package/native/s7-source/node-api-headers/LICENSE +21 -0
  155. package/native/s7-source/node-api-headers/include/js_native_api.h +591 -0
  156. package/native/s7-source/node-api-headers/include/js_native_api_types.h +213 -0
  157. package/native/s7-source/node-api-headers/include/node_api.h +265 -0
  158. package/native/s7-source/node-api-headers/include/node_api_types.h +58 -0
  159. package/native/s7-source/node-api-windows.h +78 -0
  160. package/native/s7-source/snap7/HISTORY.txt +277 -0
  161. package/native/s7-source/snap7/gpl.txt +674 -0
  162. package/native/s7-source/snap7/lgpl-3.0.txt +165 -0
  163. package/native/s7-source/snap7/src/core/miravo_request_validation.h +72 -0
  164. package/native/s7-source/snap7/src/core/s7_client.cpp +503 -0
  165. package/native/s7-source/snap7/src/core/s7_client.h +104 -0
  166. package/native/s7-source/snap7/src/core/s7_firmware.h +1256 -0
  167. package/native/s7-source/snap7/src/core/s7_isotcp.cpp +541 -0
  168. package/native/s7-source/snap7/src/core/s7_isotcp.h +271 -0
  169. package/native/s7-source/snap7/src/core/s7_micro_client.cpp +3328 -0
  170. package/native/s7-source/snap7/src/core/s7_micro_client.h +364 -0
  171. package/native/s7-source/snap7/src/core/s7_partner.cpp +1178 -0
  172. package/native/s7-source/snap7/src/core/s7_partner.h +284 -0
  173. package/native/s7-source/snap7/src/core/s7_peer.cpp +121 -0
  174. package/native/s7-source/snap7/src/core/s7_peer.h +58 -0
  175. package/native/s7-source/snap7/src/core/s7_server.cpp +2161 -0
  176. package/native/s7-source/snap7/src/core/s7_server.h +261 -0
  177. package/native/s7-source/snap7/src/core/s7_text.cpp +788 -0
  178. package/native/s7-source/snap7/src/core/s7_text.h +49 -0
  179. package/native/s7-source/snap7/src/core/s7_types.h +1066 -0
  180. package/native/s7-source/snap7/src/lib/snap7.def +129 -0
  181. package/native/s7-source/snap7/src/lib/snap7_libmain.cpp +1196 -0
  182. package/native/s7-source/snap7/src/lib/snap7_libmain.h +201 -0
  183. package/native/s7-source/snap7/src/sys/snap_msgsock.cpp +958 -0
  184. package/native/s7-source/snap7/src/sys/snap_msgsock.h +339 -0
  185. package/native/s7-source/snap7/src/sys/snap_platform.h +152 -0
  186. package/native/s7-source/snap7/src/sys/snap_sysutils.cpp +73 -0
  187. package/native/s7-source/snap7/src/sys/snap_sysutils.h +39 -0
  188. package/native/s7-source/snap7/src/sys/snap_tcpsrvr.cpp +487 -0
  189. package/native/s7-source/snap7/src/sys/snap_tcpsrvr.h +247 -0
  190. package/native/s7-source/snap7/src/sys/snap_threads.cpp +162 -0
  191. package/native/s7-source/snap7/src/sys/snap_threads.h +45 -0
  192. package/native/s7-source/snap7/src/sys/sol_threads.h +208 -0
  193. package/native/s7-source/snap7/src/sys/unix_threads.h +228 -0
  194. package/native/s7-source/snap7/src/sys/win_threads.h +159 -0
  195. package/nodesets/.ignore +1 -0
  196. package/nodesets/1.02/FTNIR.NodeSet2.xml +819 -0
  197. package/nodesets/1.02/Opc.Ua.Adi.NodeSet2.xml +8301 -0
  198. package/nodesets/1.02/Opc.Ua.Di.NodeSet2.xml +1785 -0
  199. package/nodesets/Opc.ISA95.NodeSet2.xml +3589 -0
  200. package/nodesets/Opc.Ua.AMB.NodeSet2.xml +1395 -0
  201. package/nodesets/Opc.Ua.Adi.NodeSet2.xml +8301 -0
  202. package/nodesets/Opc.Ua.Adi.Types.xsd +104 -0
  203. package/nodesets/Opc.Ua.AutoID.NodeSet2.xml +6062 -0
  204. package/nodesets/Opc.Ua.CNC.NodeSet.xml +15875 -0
  205. package/nodesets/Opc.Ua.CommercialKitchenEquipment.NodeSet2.xml +12096 -0
  206. package/nodesets/Opc.Ua.Di.NodeSet2.xml +6042 -0
  207. package/nodesets/Opc.Ua.Di.Types.xsd +161 -0
  208. package/nodesets/Opc.Ua.Eumabois.Nodeset2.xml +2483 -0
  209. package/nodesets/Opc.Ua.Gds.NodeSet2.xml +8282 -0
  210. package/nodesets/Opc.Ua.Glass.NodeSet2.xml +5100 -0
  211. package/nodesets/Opc.Ua.IA.NodeSet2.xml +1782 -0
  212. package/nodesets/Opc.Ua.IOLink.NodeSet2.xml +4374 -0
  213. package/nodesets/Opc.Ua.IOLinkIODD.NodeSet2.xml +139 -0
  214. package/nodesets/Opc.Ua.IRDI.NodeSet2.xml +2243 -0
  215. package/nodesets/Opc.Ua.Ijt.Base.NodeSet2.xml +19459 -0
  216. package/nodesets/Opc.Ua.Ijt.Tightening.NodeSet2.xml +883 -0
  217. package/nodesets/Opc.Ua.LADS.NodeSet2.xml +7086 -0
  218. package/nodesets/Opc.Ua.MachineTool.NodeSet2.xml +7884 -0
  219. package/nodesets/Opc.Ua.MachineVision.NodeSet2.xml +17324 -0
  220. package/nodesets/Opc.Ua.Machinery.Jobs.Nodeset2.xml +1249 -0
  221. package/nodesets/Opc.Ua.Machinery.NodeSet2.xml +2099 -0
  222. package/nodesets/Opc.Ua.Machinery.ProcessValues.NodeSet2.xml +1858 -0
  223. package/nodesets/Opc.Ua.Machinery_Result.NodeSet2.xml +2640 -0
  224. package/nodesets/Opc.Ua.MetalForming.NodeSet2.xml +1577 -0
  225. package/nodesets/Opc.Ua.NodeSet2.xml +81975 -0
  226. package/nodesets/Opc.Ua.PADIM.NodeSet2.xml +14131 -0
  227. package/nodesets/Opc.Ua.PackML.NodeSet2.xml +3030 -0
  228. package/nodesets/Opc.Ua.Robotics.NodeSet2.xml +7409 -0
  229. package/nodesets/Opc.Ua.Scales.NodeSet2.xml +15817 -0
  230. package/nodesets/Opc.Ua.Types.bsd +3499 -0
  231. package/nodesets/Opc.Ua.Weihenstephan.NodeSet2.xml +821 -0
  232. package/nodesets/Opc.Ua.Woodworking.NodeSet2.xml +17831 -0
  233. package/nodesets/UANodeSet.xsd +497 -0
  234. package/nodesets/opc.ua.isa95-jobcontrol.nodeset2.xml +4594 -0
  235. package/package.json +37 -4
@@ -0,0 +1,3328 @@
1
+ /*=============================================================================|
2
+ | PROJECT SNAP7 1.3.0 |
3
+ |==============================================================================|
4
+ | Copyright (C) 2013, 2015 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
+ #include "s7_micro_client.h"
27
+ //---------------------------------------------------------------------------
28
+
29
+ TSnap7MicroClient::TSnap7MicroClient()
30
+ {
31
+ SrcRef =0x0100; // RFC0983 states that SrcRef and DetRef should be 0
32
+ // and, in any case, they are ignored.
33
+ // S7 instead requires a number != 0
34
+ // Libnodave uses 0x0100
35
+ // S7Manager uses 0x0D00
36
+ // TIA Portal V12 uses 0x1D00
37
+ // WinCC uses 0x0300
38
+ // Seems that every non zero value is good enough...
39
+ DstRef =0x0000;
40
+ SrcTSap =0x0100;
41
+ DstTSap =0x0000; // It's filled by connection functions
42
+ ConnectionType = CONNTYPE_PG; // Default connection type
43
+ memset(&Job,0,sizeof(TSnap7Job));
44
+ }
45
+ //---------------------------------------------------------------------------
46
+ TSnap7MicroClient::~TSnap7MicroClient()
47
+ {
48
+ Destroying = true;
49
+ }
50
+ //---------------------------------------------------------------------------
51
+ int TSnap7MicroClient::opReadArea()
52
+ {
53
+ PReqFunReadParams ReqParams;
54
+ PResFunReadParams ResParams;
55
+ PS7ResHeader23 Answer;
56
+ PResFunReadItem ResData;
57
+ word RPSize; // ReqParams size
58
+ int WordSize;
59
+ uintptr_t Offset;
60
+ pbyte Target;
61
+ int Address;
62
+ int IsoSize;
63
+ int Start;
64
+ int MaxElements; // Max elements that we can transfer in a PDU
65
+ word NumElements; // Num of elements that we are asking for this telegram
66
+ int TotElements; // Total elements requested
67
+ int Size;
68
+ int Result;
69
+
70
+ WordSize=DataSizeByte(Job.WordLen); // The size in bytes of an element that we are asking for
71
+ if (WordSize==0)
72
+ return errCliInvalidWordLen;
73
+ // First check : params bounds
74
+ if ((Job.Number<0) || (Job.Number>65535) || (Job.Start<0) || (Job.Amount<1))
75
+ return errCliInvalidParams;
76
+ // Second check : transport size
77
+ if ((Job.WordLen==S7WLBit) && (Job.Amount>1))
78
+ return errCliInvalidTransportSize;
79
+ // Request Params size
80
+ RPSize =sizeof(TReqFunReadItem)+2; // 1 item + FunRead + ItemsCount
81
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
82
+ ReqParams =PReqFunReadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
83
+ Answer =PS7ResHeader23(&PDU.Payload);
84
+ ResParams =PResFunReadParams(pbyte(Answer)+ResHeaderSize23);
85
+ ResData =PResFunReadItem(pbyte(ResParams)+sizeof(TResFunReadParams));
86
+ // Each packet cannot exceed the PDU length (in bytes) negotiated, and moreover
87
+ // we must ensure to transfer a "finite" number of item per PDU
88
+ MaxElements=(PDULength-sizeof(TS7ResHeader23)-sizeof(TResFunReadParams)-4) / WordSize;
89
+ TotElements=Job.Amount;
90
+ Start =Job.Start;
91
+ Offset =0;
92
+ Result =0;
93
+ while ((TotElements>0) && (Result==0))
94
+ {
95
+ NumElements=TotElements;
96
+ if (NumElements>MaxElements)
97
+ NumElements=MaxElements;
98
+
99
+ Target=pbyte(Job.pData)+Offset;
100
+ //----------------------------------------------- Read next slice-----
101
+ PDUH_out->P = 0x32; // Always 0x32
102
+ PDUH_out->PDUType = PduType_request; // 0x01
103
+ PDUH_out->AB_EX = 0x0000; // Always 0x0000
104
+ PDUH_out->Sequence = GetNextWord(); // AutoInc
105
+ PDUH_out->ParLen = SwapWord(RPSize); // 14 bytes params
106
+ PDUH_out->DataLen = 0x0000; // No data
107
+
108
+ ReqParams->FunRead = pduFuncRead; // 0x04
109
+ ReqParams->ItemsCount = 1;
110
+ ReqParams->Items[0].ItemHead[0] = 0x12;
111
+ ReqParams->Items[0].ItemHead[1] = 0x0A;
112
+ ReqParams->Items[0].ItemHead[2] = 0x10;
113
+ ReqParams->Items[0].TransportSize = Job.WordLen;
114
+ ReqParams->Items[0].Length = SwapWord(NumElements);
115
+ ReqParams->Items[0].Area = Job.Area;
116
+ if (Job.Area==S7AreaDB)
117
+ ReqParams->Items[0].DBNumber = SwapWord(Job.Number);
118
+ else
119
+ ReqParams->Items[0].DBNumber = 0x0000;
120
+ // Adjusts the offset
121
+ if ((Job.WordLen==S7WLBit) || (Job.WordLen==S7WLCounter) || (Job.WordLen==S7WLTimer))
122
+ Address = Start;
123
+ else
124
+ Address = Start*8;
125
+
126
+ ReqParams->Items[0].Address[2] = Address & 0x000000FF;
127
+ Address = Address >> 8;
128
+ ReqParams->Items[0].Address[1] = Address & 0x000000FF;
129
+ Address = Address >> 8;
130
+ ReqParams->Items[0].Address[0] = Address & 0x000000FF;
131
+
132
+ IsoSize = sizeof(TS7ReqHeader)+RPSize;
133
+ Result = isoExchangeBuffer(0,IsoSize);
134
+ // Get Data
135
+ if (Result==0) // 1St level Iso
136
+ {
137
+ Size = 0;
138
+ // Item level error
139
+ if (ResData->ReturnCode==0xFF) // <-- 0xFF means Result OK
140
+ {
141
+ // Calcs data size in bytes
142
+ Size = SwapWord(ResData->DataLength);
143
+ // Adjust Size in accord of TransportSize
144
+ if ((ResData->TransportSize != TS_ResOctet) && (ResData->TransportSize != TS_ResReal) && (ResData->TransportSize != TS_ResBit))
145
+ Size = Size >> 3;
146
+ memcpy(Target, &ResData->Data[0], Size);
147
+ }
148
+ else
149
+ Result = CpuError(ResData->ReturnCode);
150
+ Offset+=Size;
151
+ };
152
+ //--------------------------------------------------------------------
153
+ TotElements -= NumElements;
154
+ Start += NumElements*WordSize;
155
+ }
156
+ return Result;
157
+ }
158
+ //---------------------------------------------------------------------------
159
+ int TSnap7MicroClient::opWriteArea()
160
+ {
161
+ PReqFunWriteParams ReqParams;
162
+ PReqFunWriteDataItem ReqData; // only 1 item for WriteArea Function
163
+ PResFunWrite ResParams;
164
+ PS7ResHeader23 Answer;
165
+ word RPSize; // ReqParams size
166
+ word RHSize; // Request headers size
167
+ bool First = true;
168
+ pbyte Source;
169
+ pbyte Target;
170
+ int Address;
171
+ int IsoSize;
172
+ int WordSize;
173
+ word Size;
174
+ uintptr_t Offset = 0;
175
+ int Start; // where we are starting from for this telegram
176
+ int MaxElements; // Max elements that we can transfer in a PDU
177
+ word NumElements; // Num of elements that we are asking for this telegram
178
+ int TotElements; // Total elements requested
179
+ int Result = 0;
180
+
181
+ WordSize=DataSizeByte(Job.WordLen); // The size in bytes of an element that we are pushing
182
+ if (WordSize==0)
183
+ return errCliInvalidWordLen;
184
+ // First check : params bounds
185
+ if ((Job.Number<0) || (Job.Number>65535) || (Job.Start<0) || (Job.Amount<1))
186
+ return errCliInvalidParams;
187
+ // Second check : transport size
188
+ if ((Job.WordLen==S7WLBit) && (Job.Amount>1))
189
+ return errCliInvalidTransportSize;
190
+
191
+ RHSize =sizeof(TS7ReqHeader)+ // Request header
192
+ 2+ // FunWrite+ItemCount (of TReqFunWriteParams)
193
+ sizeof(TReqFunWriteItem)+// 1 item reference
194
+ 4; // ReturnCode+TransportSize+DataLength
195
+ RPSize =sizeof(TReqFunWriteItem)+2;
196
+
197
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
198
+ ReqParams=PReqFunWriteParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
199
+ ReqData =PReqFunWriteDataItem(pbyte(ReqParams)+sizeof(TReqFunWriteItem)+2); // 2 = FunWrite+ItemsCount
200
+ Target =pbyte(ReqData)+4; // 4 = ReturnCode+TransportSize+DataLength
201
+ Answer =PS7ResHeader23(&PDU.Payload);
202
+ ResParams=PResFunWrite(pbyte(Answer)+ResHeaderSize23);
203
+
204
+ // Each packet cannot exceed the PDU length (in bytes) negotiated, and moreover
205
+ // we must ensure to transfer a "finite" number of item per PDU
206
+ MaxElements=(PDULength-RHSize) / WordSize;
207
+ TotElements=Job.Amount;
208
+ Start =Job.Start;
209
+ while ((TotElements>0) && (Result==0))
210
+ {
211
+ NumElements=TotElements;
212
+ if (NumElements>MaxElements)
213
+ NumElements=MaxElements;
214
+ Source=pbyte(Job.pData)+Offset;
215
+
216
+ Size=NumElements * WordSize;
217
+ PDUH_out->P=0x32; // Always 0x32
218
+ PDUH_out->PDUType=PduType_request; // 0x01
219
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
220
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
221
+ PDUH_out->ParLen =SwapWord(RPSize); // 14 bytes params
222
+ PDUH_out->DataLen =SwapWord(Size+4);
223
+
224
+ ReqParams->FunWrite=pduFuncWrite; // 0x05
225
+ ReqParams->ItemsCount=1;
226
+ ReqParams->Items[0].ItemHead[0]=0x12;
227
+ ReqParams->Items[0].ItemHead[1]=0x0A;
228
+ ReqParams->Items[0].ItemHead[2]=0x10;
229
+ ReqParams->Items[0].TransportSize=Job.WordLen;
230
+ ReqParams->Items[0].Length=SwapWord(NumElements);
231
+ ReqParams->Items[0].Area=Job.Area;
232
+ if (Job.Area==S7AreaDB)
233
+ ReqParams->Items[0].DBNumber=SwapWord(Job.Number);
234
+ else
235
+ ReqParams->Items[0].DBNumber=0x0000;
236
+
237
+ // Adjusts the offset
238
+ if ((Job.WordLen==S7WLBit) || (Job.WordLen==S7WLCounter) || (Job.WordLen==S7WLTimer))
239
+ Address=Start;
240
+ else
241
+ Address=Start*8;
242
+
243
+ ReqParams->Items[0].Address[2]=Address & 0x000000FF;
244
+ Address=Address >> 8;
245
+ ReqParams->Items[0].Address[1]=Address & 0x000000FF;
246
+ Address=Address >> 8;
247
+ ReqParams->Items[0].Address[0]=Address & 0x000000FF;
248
+
249
+ ReqData->ReturnCode=0x00;
250
+
251
+ switch(Job.WordLen)
252
+ {
253
+ case S7WLBit:
254
+ ReqData->TransportSize=TS_ResBit;
255
+ break;
256
+ case S7WLInt:
257
+ case S7WLDInt:
258
+ ReqData->TransportSize=TS_ResInt;
259
+ break;
260
+ case S7WLReal:
261
+ ReqData->TransportSize=TS_ResReal;
262
+ break;
263
+ case S7WLChar :
264
+ case S7WLCounter:
265
+ case S7WLTimer:
266
+ ReqData->TransportSize=TS_ResOctet;
267
+ break;
268
+ default:
269
+ ReqData->TransportSize=TS_ResByte;
270
+ break;
271
+ };
272
+
273
+ if ((ReqData->TransportSize!=TS_ResOctet) && (ReqData->TransportSize!=TS_ResReal) && (ReqData->TransportSize!=TS_ResBit))
274
+ ReqData->DataLength=SwapWord(Size*8);
275
+ else
276
+ ReqData->DataLength=SwapWord(Size);
277
+
278
+ memcpy(Target, Source, Size);
279
+ IsoSize=RHSize + Size;
280
+ Result=isoExchangeBuffer(0,IsoSize);
281
+
282
+ if (Result==0) // 1St check : Iso result
283
+ {
284
+ Result=CpuError(SwapWord(Answer->Error)); // 2nd level global error
285
+ if (Result==0)
286
+ { // 2th check : item error
287
+ if (ResParams->Data[0] == 0xFF) // <-- 0xFF means Result OK
288
+ Result=0;
289
+ else
290
+ // Now we check the error : if it's the first part we report the cpu error
291
+ // otherwise we warn that the function failed but some data were written
292
+ if (First)
293
+ Result=CpuError(ResParams->Data[0]);
294
+ else
295
+ Result=errCliPartialDataWritten;
296
+ };
297
+ Offset+=Size;
298
+ };
299
+ First=false;
300
+
301
+ TotElements-=NumElements;
302
+ Start+=(NumElements*WordSize);
303
+ }
304
+ return Result;
305
+ }
306
+ //---------------------------------------------------------------------------
307
+ int TSnap7MicroClient::opReadMultiVars()
308
+ {
309
+ PS7DataItem Item;
310
+ PReqFunReadParams ReqParams;
311
+ PS7ResHeader23 Answer;
312
+ PResFunReadParams ResParams;
313
+ TResFunReadData ResData;
314
+
315
+ word RPSize; // ReqParams size
316
+ uintptr_t Offset =0 ;
317
+ word Slice;
318
+ longword Address;
319
+ int IsoSize;
320
+ pbyte P;
321
+ int ItemsCount, c, Result;
322
+
323
+ Item = PS7DataItem(Job.pData);
324
+ ItemsCount = Job.Amount;
325
+
326
+ // Some useful initial check to detail the errors (Since S7 CPU always answers
327
+ // with $05 if (something is wrong in params)
328
+ if (ItemsCount>MaxVars)
329
+ return errCliTooManyItems;
330
+
331
+ // Adjusts Word Length in case of timers and counters and clears results
332
+ for (c = 0; c < ItemsCount; c++)
333
+ {
334
+ Item->Result=0;
335
+ if (Item->Area==S7AreaCT)
336
+ Item->WordLen=S7WLCounter;
337
+ if (Item->Area==S7AreaTM)
338
+ Item->WordLen=S7WLTimer;
339
+ Item++;
340
+ };
341
+
342
+ // Let's build the PDU
343
+ RPSize = word(2 + ItemsCount * sizeof(TReqFunReadItem));
344
+ ReqParams = PReqFunReadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
345
+ Answer = PS7ResHeader23(&PDU.Payload);
346
+ ResParams = PResFunReadParams(pbyte(Answer)+ResHeaderSize23);
347
+ // Fill Header
348
+ PDUH_out->P=0x32; // Always 0x32
349
+ PDUH_out->PDUType=PduType_request; // 0x01
350
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
351
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
352
+ PDUH_out->ParLen=SwapWord(RPSize); // Request params size
353
+ PDUH_out->DataLen=0x0000; // No data in output
354
+
355
+ // Fill Params
356
+ ReqParams->FunRead=pduFuncRead; // 0x04
357
+ ReqParams->ItemsCount=ItemsCount;
358
+
359
+ Item = PS7DataItem(Job.pData);
360
+ for (c = 0; c < ItemsCount; c++)
361
+ {
362
+ ReqParams->Items[c].ItemHead[0]=0x12;
363
+ ReqParams->Items[c].ItemHead[1]=0x0A;
364
+ ReqParams->Items[c].ItemHead[2]=0x10;
365
+
366
+ ReqParams->Items[c].TransportSize=Item->WordLen;
367
+ ReqParams->Items[c].Length=SwapWord(Item->Amount);
368
+ ReqParams->Items[c].Area=Item->Area;
369
+ // Automatically drops DBNumber if (Area is not DB
370
+ if (Item->Area==S7AreaDB)
371
+ ReqParams->Items[c].DBNumber=SwapWord(Item->DBNumber);
372
+ else
373
+ ReqParams->Items[c].DBNumber=0x0000;
374
+ // Adjusts the offset
375
+ if ((Item->WordLen==S7WLBit) || (Item->WordLen==S7WLCounter) || (Item->WordLen==S7WLTimer))
376
+ Address=Item->Start;
377
+ else
378
+ Address=Item->Start*8;
379
+ // Builds the offset
380
+ ReqParams->Items[c].Address[2]=Address & 0x000000FF;
381
+ Address=Address >> 8;
382
+ ReqParams->Items[c].Address[1]=Address & 0x000000FF;
383
+ Address=Address >> 8;
384
+ ReqParams->Items[c].Address[0]=Address & 0x000000FF;
385
+ Item++;
386
+ };
387
+
388
+ IsoSize=RPSize+sizeof(TS7ReqHeader);
389
+ if (IsoSize>PDULength)
390
+ return errCliSizeOverPDU;
391
+ Result=isoExchangeBuffer(0,IsoSize);
392
+
393
+ if (Result!=0)
394
+ return Result;
395
+
396
+ // Function level error
397
+ if (Answer->Error!=0)
398
+ return CpuError(SwapWord(Answer->Error));
399
+
400
+ if (ResParams->ItemCount!=ItemsCount)
401
+ return errCliInvalidPlcAnswer;
402
+
403
+ P=pbyte(ResParams)+sizeof(TResFunReadParams);
404
+ Item = PS7DataItem(Job.pData);
405
+ for (c = 0; c < ItemsCount; c++)
406
+ {
407
+ ResData[c] =PResFunReadItem(pbyte(P)+Offset);
408
+ Slice=0;
409
+ // Item level error
410
+ if (ResData[c]->ReturnCode==0xFF) // <-- 0xFF means Result OK
411
+ {
412
+ // Calcs data size in bytes
413
+ Slice=SwapWord(ResData[c]->DataLength);
414
+ // Adjust Size in accord of TransportSize
415
+ if ((ResData[c]->TransportSize != TS_ResOctet) && (ResData[c]->TransportSize != TS_ResReal) && (ResData[c]->TransportSize != TS_ResBit))
416
+ Slice=Slice >> 3;
417
+
418
+ memcpy(Item->pdata, ResData[c]->Data, Slice);
419
+ Item->Result=0;
420
+ }
421
+ else
422
+ Item->Result=CpuError(ResData[c]->ReturnCode);
423
+
424
+ if ((Slice % 2)!=0)
425
+ Slice++; // Skip fill byte for Odd frame
426
+
427
+ Offset+=(4+Slice);
428
+ Item++;
429
+ };
430
+ return Result;
431
+ }
432
+ //---------------------------------------------------------------------------
433
+ int TSnap7MicroClient::opWriteMultiVars()
434
+ {
435
+ PS7DataItem Item;
436
+ PReqFunWriteParams ReqParams;
437
+ PResFunWrite ResParams;
438
+ TReqFunWriteData ReqData;
439
+ PS7ResHeader23 Answer;
440
+ pbyte P;
441
+ uintptr_t Offset;
442
+ longword Address;
443
+ int ItemsCount, c, IsoSize;
444
+ word RPSize; // ReqParams size
445
+ word Size; // Write data size
446
+ int WordSize, Result;
447
+
448
+ Item = PS7DataItem(Job.pData);
449
+ ItemsCount = Job.Amount;
450
+
451
+ // Some useful initial check to detail the errors (Since S7 CPU always answers
452
+ // with $05 if (something is wrong in params)
453
+ if (ItemsCount>MaxVars)
454
+ return errCliTooManyItems;
455
+
456
+ // Adjusts Word Length in case of timers and counters and clears results
457
+ for (c = 0; c < ItemsCount; c++)
458
+ {
459
+ Item->Result=0;
460
+ if (Item->Area==S7AreaCT)
461
+ Item->WordLen=S7WLCounter;
462
+ if (Item->Area==S7AreaTM)
463
+ Item->WordLen=S7WLTimer;
464
+ Item++;
465
+ };
466
+
467
+ // Let's build the PDU : setup pointers
468
+ RPSize = word(2 + ItemsCount * sizeof(TReqFunWriteItem));
469
+ ReqParams = PReqFunWriteParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
470
+ Answer = PS7ResHeader23(&PDU.Payload);
471
+ ResParams = PResFunWrite(pbyte(Answer)+ResHeaderSize23);
472
+ P=pbyte(ReqParams)+RPSize;
473
+
474
+ // Fill Header
475
+ PDUH_out->P=0x32; // Always 0x32
476
+ PDUH_out->PDUType=PduType_request; // 0x01
477
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
478
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
479
+ PDUH_out->ParLen=SwapWord(RPSize); // Request params size
480
+
481
+ // Fill Params
482
+ ReqParams->FunWrite=pduFuncWrite; // 0x05
483
+ ReqParams->ItemsCount=ItemsCount;
484
+
485
+ Offset=0;
486
+ Item = PS7DataItem(Job.pData);
487
+ for (c = 0; c < ItemsCount; c++)
488
+ {
489
+ // Items Params
490
+ ReqParams->Items[c].ItemHead[0]=0x12;
491
+ ReqParams->Items[c].ItemHead[1]=0x0A;
492
+ ReqParams->Items[c].ItemHead[2]=0x10;
493
+
494
+ ReqParams->Items[c].TransportSize=Item->WordLen;
495
+ ReqParams->Items[c].Length=SwapWord(Item->Amount);
496
+ ReqParams->Items[c].Area=Item->Area;
497
+
498
+ if (Item->Area==S7AreaDB)
499
+ ReqParams->Items[c].DBNumber=SwapWord(Item->DBNumber);
500
+ else
501
+ ReqParams->Items[c].DBNumber=0x0000;
502
+
503
+ // Adjusts the offset
504
+ if ((Item->WordLen==S7WLBit) || (Item->WordLen==S7WLCounter) || (Item->WordLen==S7WLTimer))
505
+ Address=Item->Start;
506
+ else
507
+ Address=Item->Start*8;
508
+ // Builds the offset
509
+ ReqParams->Items[c].Address[2]=Address & 0x000000FF;
510
+ Address=Address >> 8;
511
+ ReqParams->Items[c].Address[1]=Address & 0x000000FF;
512
+ Address=Address >> 8;
513
+ ReqParams->Items[c].Address[0]=Address & 0x000000FF;
514
+
515
+ // Items Data
516
+ ReqData[c]=PReqFunWriteDataItem(pbyte(P)+Offset);
517
+ ReqData[c]->ReturnCode=0x00;
518
+
519
+ switch (Item->WordLen)
520
+ {
521
+ case S7WLBit :
522
+ ReqData[c]->TransportSize=TS_ResBit;
523
+ break;
524
+ case S7WLInt :
525
+ case S7WLDInt :
526
+ ReqData[c]->TransportSize=TS_ResInt;
527
+ break;
528
+ case S7WLReal :
529
+ ReqData[c]->TransportSize=TS_ResReal;
530
+ break;
531
+ case S7WLChar :
532
+ case S7WLCounter :
533
+ case S7WLTimer : ReqData[c]->TransportSize=TS_ResOctet;
534
+ break;
535
+ default :
536
+ ReqData[c]->TransportSize=TS_ResByte; // byte/word/dword etc.
537
+ break;
538
+ };
539
+
540
+ WordSize=DataSizeByte(Item->WordLen);
541
+ Size=Item->Amount * WordSize;
542
+
543
+ if ((ReqData[c]->TransportSize!=TS_ResOctet) && (ReqData[c]->TransportSize!=TS_ResReal) && (ReqData[c]->TransportSize!=TS_ResBit))
544
+ ReqData[c]->DataLength=SwapWord(Size*8);
545
+ else
546
+ ReqData[c]->DataLength=SwapWord(Size);
547
+
548
+ memcpy(ReqData[c]->Data, Item->pdata, Size);
549
+
550
+ if ((Size % 2) != 0 && (ItemsCount - c != 1))
551
+ Size++; // Skip fill byte for Odd frame (except for the last one)
552
+
553
+ Offset+=(4+Size); // next item
554
+ Item++;
555
+ };
556
+
557
+ PDUH_out->DataLen=SwapWord(word(Offset));
558
+
559
+ IsoSize=RPSize+sizeof(TS7ReqHeader)+int(Offset);
560
+ if (IsoSize>PDULength)
561
+ return errCliSizeOverPDU;
562
+ Result=isoExchangeBuffer(0,IsoSize);
563
+
564
+ if (Result!=0)
565
+ return Result;
566
+
567
+ // Function level error
568
+ if (Answer->Error!=0)
569
+ return CpuError(SwapWord(Answer->Error));
570
+
571
+ if (ResParams->ItemCount!=ItemsCount)
572
+ return errCliInvalidPlcAnswer;
573
+
574
+ Item = PS7DataItem(Job.pData);
575
+ for (c = 0; c < ItemsCount; c++)
576
+ {
577
+ // Item level error
578
+ if (ResParams->Data[c]==0xFF) // <-- 0xFF means Result OK
579
+ Item->Result=0;
580
+ else
581
+ Item->Result=CpuError(ResParams->Data[c]);
582
+ Item++;
583
+ };
584
+ return Result;
585
+ }
586
+ //---------------------------------------------------------------------------
587
+ int TSnap7MicroClient::opListBlocks()
588
+ {
589
+ PReqFunGetBlockInfo ReqParams;
590
+ PReqDataFunBlocks ReqData;
591
+ PResFunGetBlockInfo ResParams;
592
+ PDataFunListAll ResData;
593
+ PS7ResHeader17 Answer;
594
+ PS7BlocksList List;
595
+ int IsoSize, Result;
596
+
597
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
598
+ ReqParams=PReqFunGetBlockInfo(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
599
+ ReqData =PReqDataFunBlocks(pbyte(ReqParams)+sizeof(TReqFunGetBlockInfo));
600
+ Answer =PS7ResHeader17(&PDU.Payload);
601
+ ResParams=PResFunGetBlockInfo(pbyte(Answer)+ResHeaderSize17);
602
+ ResData =PDataFunListAll(pbyte(ResParams)+sizeof(TResFunGetBlockInfo));
603
+ List =PS7BlocksList(Job.pData);
604
+ // Fill Header
605
+ PDUH_out->P=0x32; // Always 0x32
606
+ PDUH_out->PDUType=PduType_userdata; // 0x07
607
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
608
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
609
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunGetBlockInfo)); // 8 bytes params
610
+ PDUH_out->DataLen=SwapWord(sizeof(TReqDataFunBlocks)); // 4 bytes data
611
+ // Fill params (mostly constants)
612
+ ReqParams->Head[0]=0x00;
613
+ ReqParams->Head[1]=0x01;
614
+ ReqParams->Head[2]=0x12;
615
+ ReqParams->Plen =0x04;
616
+ ReqParams->Uk =0x11;
617
+ ReqParams->Tg =grBlocksInfo;
618
+ ReqParams->SubFun =SFun_ListAll;
619
+ ReqParams->Seq =0x00;
620
+ // Fill data
621
+ ReqData[0] =0x0A;
622
+ ReqData[1] =0x00;
623
+ ReqData[2] =0x00;
624
+ ReqData[3] =0x00;
625
+
626
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunGetBlockInfo)+sizeof(TReqDataFunBlocks);
627
+ Result=isoExchangeBuffer(0,IsoSize);
628
+ // Get Data
629
+ if (Result==0)
630
+ {
631
+ if (ResParams->ErrNo==0)
632
+ {
633
+ if (SwapWord(ResData->Length)!=28)
634
+ return errCliInvalidPlcAnswer;
635
+
636
+ for (int c = 0; c < 7; c++)
637
+ {
638
+ switch (ResData->Blocks[c].BType)
639
+ {
640
+ case Block_OB:
641
+ List->OBCount=SwapWord(ResData->Blocks[c].BCount);
642
+ break;
643
+ case Block_DB:
644
+ List->DBCount=SwapWord(ResData->Blocks[c].BCount);
645
+ break;
646
+ case Block_SDB:
647
+ List->SDBCount=SwapWord(ResData->Blocks[c].BCount);
648
+ break;
649
+ case Block_FC:
650
+ List->FCCount=SwapWord(ResData->Blocks[c].BCount);
651
+ break;
652
+ case Block_SFC:
653
+ List->SFCCount=SwapWord(ResData->Blocks[c].BCount);
654
+ break;
655
+ case Block_FB:
656
+ List->FBCount=SwapWord(ResData->Blocks[c].BCount);
657
+ break;
658
+ case Block_SFB:
659
+ List->SFBCount=SwapWord(ResData->Blocks[c].BCount);
660
+ break;
661
+ }
662
+ }
663
+ }
664
+ else
665
+ Result=CpuError(SwapWord(ResParams->ErrNo));
666
+ }
667
+ return Result;
668
+ }
669
+ //---------------------------------------------------------------------------
670
+ int TSnap7MicroClient::opListBlocksOfType()
671
+ {
672
+ PReqFunGetBlockInfo ReqParams;
673
+ PReqDataBlockOfType ReqData;
674
+
675
+ PS7ResHeader17 Answer;
676
+ PResFunGetBlockInfo ResParams;
677
+ PDataFunGetBot ResData;
678
+ longword *PadData;
679
+ word *List;
680
+ bool First;
681
+ bool Done = false;
682
+ byte BlockType, In_Seq;
683
+ int Count, Last, IsoSize, Result;
684
+ int c, CThis;
685
+ word DataLength;
686
+ bool RoomError = false;
687
+
688
+ BlockType=Job.Area;
689
+ List=(word*)(&opData);
690
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
691
+ ReqParams=PReqFunGetBlockInfo(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
692
+ Answer =PS7ResHeader17(&PDU.Payload);
693
+ ResParams=PResFunGetBlockInfo(pbyte(Answer)+ResHeaderSize17);
694
+ ResData =PDataFunGetBot(pbyte(ResParams)+sizeof(TResFunGetBlockInfo));
695
+ // Get Data
696
+ First =true;
697
+ In_Seq=0x00; // first group sequence, next will come from PLC
698
+ Count =0;
699
+ Last =0;
700
+ do
701
+ {
702
+ //<--------------------------------------------------------- Get next slice
703
+ // Fill Header
704
+ PDUH_out->P=0x32; // Always 0x32
705
+ PDUH_out->PDUType=PduType_userdata; // 0x07
706
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
707
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
708
+ if (First)
709
+ {
710
+ PDUH_out->ParLen=SwapWord(8); // 8 bytes params
711
+ PDUH_out->DataLen=SwapWord(6); // 6 bytes data
712
+ DataLength=14;
713
+ }
714
+ else
715
+ {
716
+ PDUH_out->ParLen=SwapWord(12); // 12 bytes params
717
+ PDUH_out->DataLen=SwapWord(4); // 4 bytes data
718
+ DataLength=16;
719
+ }
720
+
721
+ // Fill params (mostly constants)
722
+ ReqParams->Head[0]=0x00;
723
+ ReqParams->Head[1]=0x01;
724
+ ReqParams->Head[2]=0x12;
725
+
726
+ if (First)
727
+ ReqParams->Plen =0x04;
728
+ else
729
+ ReqParams->Plen =0x08;
730
+
731
+ if (First)
732
+ ReqParams->Uk = 0x11;
733
+ else
734
+ ReqParams->Uk = 0x12;
735
+
736
+ ReqParams->Tg =grBlocksInfo;
737
+ ReqParams->SubFun =SFun_ListBoT;
738
+ ReqParams->Seq =In_Seq;
739
+
740
+ // Fill data
741
+ if (First)
742
+ {
743
+ // overlap resvd and error to avoid another struct...
744
+ ReqData =PReqDataBlockOfType(pbyte(ReqParams)+sizeof(TReqFunGetBlockInfo));
745
+ ReqData->RetVal =0xFF;
746
+ ReqData->TSize =TS_ResOctet;
747
+ ReqData->Length =SwapWord(0x0002);
748
+ ReqData->Zero =0x30; // zero ascii '0'
749
+ ReqData->BlkType =BlockType;
750
+ }
751
+ else
752
+ {
753
+ PadData =(longword*)(pbyte(ReqParams)+sizeof(TReqFunGetBlockInfo));
754
+ ReqData =PReqDataBlockOfType(pbyte(ReqParams)+sizeof(TReqFunGetBlockInfo)+4);
755
+ *PadData =0x00000000;
756
+ ReqData->RetVal =0x0A;
757
+ ReqData->TSize =0x00;
758
+ ReqData->Length =0x0000;
759
+ ReqData->Zero =0x00;
760
+ ReqData->BlkType =0x00;
761
+ };
762
+
763
+ IsoSize=sizeof(TS7ReqHeader)+DataLength;
764
+ Result=isoExchangeBuffer(0,IsoSize);
765
+
766
+ if (Result==0)
767
+ {
768
+ if (ResParams->ErrNo==0)
769
+ {
770
+ if (ResData->RetVal==0xFF)
771
+ {
772
+ Done=((ResParams->Rsvd & 0xFF00) == 0); // Low order byte = 0x00 => the sequence is done
773
+ In_Seq=ResParams->Seq; // every next telegram must have this number
774
+ CThis=((SwapWord(ResData->DataLen) - 4 ) / 4) + 1; // Partial counter
775
+ for (c=0; c < CThis+1; c++)
776
+ {
777
+ *List=SwapWord(ResData->Items[c].BlockNum);
778
+ Last++;
779
+ List++;
780
+ if (Last==0x8000)
781
+ {
782
+ Done=true;
783
+ break;
784
+ };
785
+ };
786
+ Count+=CThis; // Total counter
787
+ List--;
788
+ }
789
+ else
790
+ Result=errCliItemNotAvailable;
791
+ }
792
+ else
793
+ Result=errCliItemNotAvailable;
794
+ };
795
+ First=false;
796
+ //---------------------------------------------------------> Get next slice
797
+ }
798
+ while ((!Done) && (Result==0));
799
+
800
+ *Job.pAmount=0;
801
+ if (Result==0)
802
+ {
803
+ if (Count>Job.Amount)
804
+ {
805
+ Count=Job.Amount;
806
+ RoomError=true;
807
+ }
808
+ memcpy(Job.pData, &opData, Count*2);
809
+ *Job.pAmount=Count;
810
+
811
+ if (RoomError) // Result==0 -> override if romerror
812
+ Result=errCliPartialDataRead;
813
+ };
814
+
815
+ return Result;
816
+ }
817
+ //---------------------------------------------------------------------------
818
+ void TSnap7MicroClient::FillTime(word SiemensTime, char *PTime)
819
+ {
820
+ // SiemensTime -> number of seconds after 1/1/1984
821
+ // This is not S7 date and time but is used only internally for block info
822
+ time_t TheDate = (SiemensTime * 86400)+ DeltaSecs;
823
+ struct tm * timeinfo = localtime (&TheDate);
824
+ if (timeinfo!=NULL) {
825
+ strftime(PTime,11,"%Y/%m/%d",timeinfo);
826
+ }
827
+ else
828
+ *PTime='\0';
829
+ }
830
+ //---------------------------------------------------------------------------
831
+ int TSnap7MicroClient::opAgBlockInfo()
832
+ {
833
+ PS7BlockInfo BlockInfo;
834
+ PReqFunGetBlockInfo ReqParams;
835
+ PReqDataBlockInfo ReqData;
836
+ PS7ResHeader17 Answer;
837
+ PResFunGetBlockInfo ResParams;
838
+ PResDataBlockInfo ResData;
839
+ byte BlockType;
840
+ int BlockNum, IsoSize, Result;
841
+
842
+ BlockType=Job.Area;
843
+ BlockNum =Job.Number;
844
+ BlockInfo=PS7BlockInfo(Job.pData);
845
+ memset(BlockInfo,0,sizeof(TS7BlockInfo));
846
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
847
+ ReqParams=PReqFunGetBlockInfo(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
848
+ ReqData =PReqDataBlockInfo(pbyte(ReqParams)+sizeof(TReqFunGetBlockInfo));
849
+ Answer =PS7ResHeader17(&PDU.Payload);
850
+ ResParams=PResFunGetBlockInfo(pbyte(Answer)+ResHeaderSize17);
851
+ ResData =PResDataBlockInfo(pbyte(ResParams)+sizeof(TResFunGetBlockInfo));
852
+ // Fill Header
853
+ PDUH_out->P=0x32; // Always 0x32
854
+ PDUH_out->PDUType=PduType_userdata; // 0x07
855
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
856
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
857
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunGetBlockInfo)); // 8 bytes params
858
+ PDUH_out->DataLen=SwapWord(sizeof(TReqDataBlockInfo)); // 4 bytes data
859
+ // Fill params (mostly constants)
860
+ ReqParams->Head[0]=0x00;
861
+ ReqParams->Head[1]=0x01;
862
+ ReqParams->Head[2]=0x12;
863
+ ReqParams->Plen =0x04;
864
+ ReqParams->Uk =0x11;
865
+ ReqParams->Tg =grBlocksInfo;
866
+ ReqParams->SubFun =SFun_BlkInfo;
867
+ ReqParams->Seq =0x00;
868
+ // Fill data
869
+ ReqData->RetVal =0xFF;
870
+ ReqData->TSize =TS_ResOctet;
871
+ ReqData->DataLen =SwapWord(0x0008);
872
+ ReqData->BlkPrfx =0x30;
873
+ ReqData->BlkType =BlockType;
874
+ ReqData->A =0x41;
875
+
876
+ ReqData->AsciiBlk[0]=(BlockNum / 10000)+0x30;
877
+ BlockNum=BlockNum % 10000;
878
+ ReqData->AsciiBlk[1]=(BlockNum / 1000)+0x30;
879
+ BlockNum=BlockNum % 1000;
880
+ ReqData->AsciiBlk[2]=(BlockNum / 100)+0x30;
881
+ BlockNum=BlockNum % 100;
882
+ ReqData->AsciiBlk[3]=(BlockNum / 10)+0x30;
883
+ BlockNum=BlockNum % 10;
884
+ ReqData->AsciiBlk[4]=(BlockNum / 1)+0x30;
885
+
886
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunGetBlockInfo)+sizeof(TReqDataBlockInfo);
887
+ Result=isoExchangeBuffer(0,IsoSize);
888
+
889
+ // Get Data
890
+ if (Result==0)
891
+ {
892
+ if (ResParams->ErrNo==0)
893
+ {
894
+ if (SwapWord(ResData->Length)<40) // 78
895
+ return errCliInvalidPlcAnswer;
896
+ if (ResData->RetVal==0xFF) // <-- 0xFF means Result OK
897
+ {
898
+ //<----------------------------------------------Fill block info
899
+ BlockInfo->BlkType=ResData->SubBlkType;
900
+ BlockInfo->BlkNumber=SwapWord(ResData->BlkNumber);
901
+ BlockInfo->BlkLang=ResData->BlkLang;
902
+ BlockInfo->BlkFlags=ResData->BlkFlags;
903
+ BlockInfo->MC7Size=SwapWord(ResData->MC7Len);
904
+ BlockInfo->LoadSize=SwapDWord(ResData->LenLoadMem);
905
+ BlockInfo->LocalData=SwapWord(ResData->LocDataLen);
906
+ BlockInfo->SBBLength=SwapWord(ResData->SbbLen);
907
+ BlockInfo->CheckSum=SwapWord(ResData->BlkChksum);
908
+ BlockInfo->Version=ResData->Version;
909
+ memcpy(BlockInfo->Author, ResData->Author, 8);
910
+ memcpy(BlockInfo->Family,ResData->Family,8);
911
+ memcpy(BlockInfo->Header,ResData->Header,8);
912
+ FillTime(SwapWord(ResData->CodeTime_dy),BlockInfo->CodeDate);
913
+ FillTime(SwapWord(ResData->IntfTime_dy),BlockInfo->IntfDate);
914
+ //---------------------------------------------->Fill block info
915
+ }
916
+ else
917
+ Result=CpuError(ResData->RetVal);
918
+ }
919
+ else
920
+ Result=CpuError(SwapWord(ResParams->ErrNo));
921
+ };
922
+ return Result;
923
+ }
924
+ //---------------------------------------------------------------------------
925
+ int TSnap7MicroClient::opDBGet()
926
+ {
927
+ TS7BlockInfo BI;
928
+ void * usrPData;
929
+ int * usrPSize;
930
+ int Result, Room;
931
+ bool RoomError = false;
932
+
933
+ // Stores user pointer
934
+ usrPData=Job.pData;
935
+ usrPSize=Job.pAmount;
936
+ Room =Job.Amount;
937
+
938
+ // 1 Pass : Get block info
939
+ Job.Area=Block_DB;
940
+ Job.pData=&BI;
941
+ Result=opAgBlockInfo();
942
+
943
+ // 2 Pass : Read the whole (MC7Size bytes) DB.
944
+ if (Result==0)
945
+ {
946
+ // Check user space
947
+ if (BI.MC7Size>Room)
948
+ {
949
+ Job.Amount=Room;
950
+ RoomError=true;
951
+ }
952
+ else
953
+ Job.Amount =BI.MC7Size;
954
+ // The data is read even if the buffer is small (the error is reported).
955
+ // Imagine that we want to read only a small amount of data at the
956
+ // beginning of a DB regardless it's size....
957
+ Job.Area =S7AreaDB;
958
+ Job.WordLen=S7WLByte;
959
+ Job.Start =0;
960
+ Job.pData =usrPData;
961
+ Result =opReadArea();
962
+ if (Result==0)
963
+ *usrPSize=Job.Amount;
964
+ }
965
+
966
+ if ((Result==0) && RoomError)
967
+ return errCliBufferTooSmall;
968
+ else
969
+ return Result;
970
+ }
971
+ //---------------------------------------------------------------------------
972
+ int TSnap7MicroClient::opDBFill()
973
+ {
974
+ TS7BlockInfo BI;
975
+ int Result;
976
+ // new op : get block info
977
+ Job.Op =s7opAgBlockInfo;
978
+ Job.Area =Block_DB;
979
+ Job.pData=&BI;
980
+ Result =opAgBlockInfo();
981
+ // Restore original op
982
+ Job.Op =s7opDBFill;
983
+ // Fill internal buffer then write it
984
+ if (Result==0)
985
+ {
986
+ Job.Amount =BI.MC7Size;
987
+ Job.Area =S7AreaDB;
988
+ Job.WordLen=S7WLByte;
989
+ Job.Start =0;
990
+ memset(&opData, byte(Job.IParam), Job.Amount);
991
+ Job.pData =&opData;
992
+ Result =opWriteArea();
993
+ }
994
+ return Result;
995
+ }
996
+ //---------------------------------------------------------------------------
997
+ int TSnap7MicroClient::opUpload()
998
+ {
999
+ PS7ResHeader23 Answer;
1000
+ int IsoSize;
1001
+ byte Upload_ID = 0; // not strictly needed, only to avoid warning
1002
+ byte BlockType;
1003
+ int BlockNum, BlockLength, Result;
1004
+ bool Done, Full; // if full==true, the data will be compatible to full download function
1005
+ uintptr_t Offset;
1006
+ bool RoomError = false;
1007
+
1008
+ BlockType=Job.Area;
1009
+ BlockNum =Job.Number;
1010
+ Full =Job.IParam==1;
1011
+ // Setup Answer (is the same for all Upload pdus)
1012
+ Answer= PS7ResHeader23(&PDU.Payload);
1013
+ // Init sequence
1014
+ Done =false;
1015
+ Offset=0;
1016
+ //<-------------------------------------------------------------StartUpload
1017
+ PReqFunStartUploadParams ReqParams;
1018
+ PResFunStartUploadParams ResParams;
1019
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1020
+ ReqParams=PReqFunStartUploadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1021
+ ResParams=PResFunStartUploadParams(pbyte(Answer)+ResHeaderSize23);
1022
+ // Init Header
1023
+ PDUH_out->P=0x32; // Always 0x32
1024
+ PDUH_out->PDUType=PduType_request; // 0x01
1025
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1026
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1027
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunStartUploadParams));// params size
1028
+ PDUH_out->DataLen=0x0000; // No data
1029
+ // Init Params
1030
+ ReqParams->FunSUpld=pduStartUpload;
1031
+ ReqParams->Uk6[0]=0x00;
1032
+ ReqParams->Uk6[1]=0x00;
1033
+ ReqParams->Uk6[2]=0x00;
1034
+ ReqParams->Uk6[3]=0x00;
1035
+ ReqParams->Uk6[4]=0x00;
1036
+ ReqParams->Uk6[5]=0x00;
1037
+ ReqParams->Upload_ID=Upload_ID; // At begining is 0) we will put upload id incoming from plc
1038
+ ReqParams->Len_1 =0x09; // 9 bytes from here
1039
+ ReqParams->Prefix=0x5F;
1040
+ ReqParams->BlkPrfx=0x30; // '0'
1041
+ ReqParams->BlkType=BlockType;
1042
+ // Block number
1043
+ ReqParams->AsciiBlk[0]=(BlockNum / 10000)+0x30;
1044
+ BlockNum=BlockNum % 10000;
1045
+ ReqParams->AsciiBlk[1]=(BlockNum / 1000)+0x30;
1046
+ BlockNum=BlockNum % 1000;
1047
+ ReqParams->AsciiBlk[2]=(BlockNum / 100)+0x30;
1048
+ BlockNum=BlockNum % 100;
1049
+ ReqParams->AsciiBlk[3]=(BlockNum / 10)+0x30;
1050
+ BlockNum=BlockNum % 10;
1051
+ ReqParams->AsciiBlk[4]=(BlockNum / 1)+0x30;
1052
+ ReqParams->A=0x41; // 'A'
1053
+
1054
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunStartUploadParams);
1055
+ Result=isoExchangeBuffer(0,IsoSize);
1056
+ // Get Upload Infos (only ID now)
1057
+ if (Result==0)
1058
+ {
1059
+ if (Answer->Error==0)
1060
+ Upload_ID=ResParams->Upload_ID;
1061
+ else
1062
+ Result=CpuError(SwapWord(Answer->Error));
1063
+ };
1064
+ //------------------------------------------------------------->StartUpload
1065
+ if (Result==0)
1066
+ {
1067
+ //<--------------------------------------------------------FirstUpload
1068
+ PReqFunUploadParams ReqParams;
1069
+ PResFunUploadParams ResParams;
1070
+ PResFunUploadDataHeaderFirst ResDataHeader;
1071
+ pbyte Source;
1072
+ pbyte Target;
1073
+ int Size;
1074
+
1075
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1076
+ ReqParams=PReqFunUploadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1077
+ // First upload pdu consists of params, block info header, data.
1078
+ ResParams=PResFunUploadParams(pbyte(Answer)+ResHeaderSize23);
1079
+ ResDataHeader=PResFunUploadDataHeaderFirst(pbyte(ResParams)+sizeof(TResFunUploadParams));
1080
+ if (Full)
1081
+ Source=pbyte(ResDataHeader)+4; // skip only the mini header
1082
+ else
1083
+ Source=pbyte(ResDataHeader)+sizeof(TResFunUploadDataHeaderFirst); // not full : skip the data header
1084
+ // Init Header
1085
+ PDUH_out->P=0x32; // Always 0x32
1086
+ PDUH_out->PDUType=PduType_request; // 0x01
1087
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1088
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1089
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunUploadParams));// params size
1090
+ PDUH_out->DataLen=0x0000; // No data
1091
+ // Init Params
1092
+ ReqParams->FunUpld=pduUpload;
1093
+ ReqParams->Uk6[0]=0x00;
1094
+ ReqParams->Uk6[1]=0x00;
1095
+ ReqParams->Uk6[2]=0x00;
1096
+ ReqParams->Uk6[3]=0x00;
1097
+ ReqParams->Uk6[4]=0x00;
1098
+ ReqParams->Uk6[5]=0x00;
1099
+ ReqParams->Upload_ID=Upload_ID; // At begining is 0) we will put upload id incoming from plc
1100
+
1101
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunUploadParams);
1102
+ Result=isoExchangeBuffer(0,IsoSize);
1103
+ // Get Upload Infos (only ID now)
1104
+ if (Result==0)
1105
+ {
1106
+ if (Answer->Error==0)
1107
+ {
1108
+ Done=ResParams->EoU==0;
1109
+ if (Full)
1110
+ Size=SwapWord(Answer->DataLen)-4; // Full data Size
1111
+ else
1112
+ Size=SwapWord(Answer->DataLen)-sizeof(TResFunUploadDataHeaderFirst); // Size of this data slice
1113
+
1114
+ BlockLength=SwapWord(ResDataHeader->MC7Len); // Full block size in byte
1115
+ Target=pbyte(&opData)+Offset;
1116
+ memcpy(Target, Source, Size);
1117
+ Offset+=Size;
1118
+ }
1119
+ else
1120
+ Result=errCliUploadSequenceFailed;
1121
+ };
1122
+ //-------------------------------------------------------->FirstUpload
1123
+ while (!Done && (Result==0))
1124
+ {
1125
+ //<----------------------------------------------------NextUpload
1126
+ PReqFunUploadParams ReqParams;
1127
+ PResFunUploadParams ResParams;
1128
+ PResFunUploadDataHeaderNext ResDataHeader;
1129
+ pbyte Source;
1130
+ pbyte Target;
1131
+ int Size;
1132
+
1133
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1134
+ ReqParams=PReqFunUploadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1135
+ // Next upload pdu consists of params, small info header, data.
1136
+ ResParams=PResFunUploadParams(pbyte(Answer)+ResHeaderSize23);
1137
+ ResDataHeader=PResFunUploadDataHeaderNext(pbyte(ResParams)+sizeof(TResFunUploadParams));
1138
+ Source=pbyte(ResDataHeader)+sizeof(TResFunUploadDataHeaderNext);
1139
+ // Init Header
1140
+ PDUH_out->P=0x32; // Always 0x32
1141
+ PDUH_out->PDUType=PduType_request; // 0x01
1142
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1143
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1144
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunUploadParams));// params size
1145
+ PDUH_out->DataLen=0x0000; // No data
1146
+ // Init Params
1147
+ ReqParams->FunUpld=pduUpload;
1148
+ ReqParams->Uk6[0]=0x00;
1149
+ ReqParams->Uk6[1]=0x00;
1150
+ ReqParams->Uk6[2]=0x00;
1151
+ ReqParams->Uk6[3]=0x00;
1152
+ ReqParams->Uk6[4]=0x00;
1153
+ ReqParams->Uk6[5]=0x00;
1154
+ ReqParams->Upload_ID=Upload_ID; // At begining is 0) we will put upload id incoming from plc
1155
+
1156
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunUploadParams);
1157
+ Result=isoExchangeBuffer(0,IsoSize);
1158
+ // Get Upload Infos (only ID now)
1159
+ if (Result==0)
1160
+ {
1161
+ if (Answer->Error==0)
1162
+ {
1163
+ Done=ResParams->EoU==0;
1164
+ Size=SwapWord(Answer->DataLen)-sizeof(TResFunUploadDataHeaderNext); // Size of this data slice
1165
+ Target=pbyte(&opData)+Offset;
1166
+ memcpy(Target, Source, Size);
1167
+ Offset+=Size;
1168
+ }
1169
+ else
1170
+ Result=errCliUploadSequenceFailed;
1171
+ };
1172
+ //---------------------------------------------------->NextUpload
1173
+ }
1174
+ if (Result==0)
1175
+ {
1176
+ //<----------------------------------------------------EndUpload;
1177
+ PReqFunEndUploadParams ReqParams;
1178
+ PResFunEndUploadParams ResParams;
1179
+
1180
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1181
+ ReqParams=PReqFunEndUploadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1182
+ ResParams=PResFunEndUploadParams(pbyte(Answer)+ResHeaderSize23);
1183
+ // Init Header
1184
+ PDUH_out->P=0x32; // Always 0x32
1185
+ PDUH_out->PDUType=PduType_request; // 0x01
1186
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1187
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1188
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunEndUploadParams));// params size
1189
+ PDUH_out->DataLen=0x0000; // No data
1190
+ // Init Params
1191
+ ReqParams->FunEUpld=pduEndUpload;
1192
+ ReqParams->Uk6[0]=0x00;
1193
+ ReqParams->Uk6[1]=0x00;
1194
+ ReqParams->Uk6[2]=0x00;
1195
+ ReqParams->Uk6[3]=0x00;
1196
+ ReqParams->Uk6[4]=0x00;
1197
+ ReqParams->Uk6[5]=0x00;
1198
+ ReqParams->Upload_ID=Upload_ID; // At begining is 0) we will put upload id incoming from plc
1199
+
1200
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunEndUploadParams);
1201
+ Result=isoExchangeBuffer(0,IsoSize);
1202
+
1203
+ // Get EndUpload Result
1204
+ if (Result==0)
1205
+ {
1206
+ if ((Answer->Error!=0) || (ResParams->FunEUpld!=pduEndUpload))
1207
+ Result=errCliUploadSequenceFailed;
1208
+ };
1209
+ //---------------------------------------------------->EndUpload;
1210
+ }
1211
+ };
1212
+
1213
+ *Job.pAmount=0;
1214
+ if (Result==0)
1215
+ {
1216
+ if (Full)
1217
+ {
1218
+ opSize=int(Offset);
1219
+ if (opSize<78)
1220
+ Result=errCliInvalidDataSizeRecvd;
1221
+ }
1222
+ else
1223
+ {
1224
+ opSize=BlockLength;
1225
+ if (opSize<1)
1226
+ Result=errCliInvalidDataSizeRecvd;
1227
+ };
1228
+ if (Result==0)
1229
+ {
1230
+ // Checks user space
1231
+ if (Job.Amount<opSize) {
1232
+ opSize=Job.Amount;
1233
+ RoomError = true;
1234
+ };
1235
+ memcpy(Job.pData, &opData, opSize);
1236
+ *Job.pAmount=opSize;
1237
+ if (RoomError) // Result==0 -> override if romerror
1238
+ Result=errCliPartialDataRead;
1239
+ };
1240
+ };
1241
+ return Result;
1242
+ }
1243
+ //---------------------------------------------------------------------------
1244
+ int TSnap7MicroClient::opDownload()
1245
+ {
1246
+ PS7CompactBlockInfo Info;
1247
+ PS7BlockFooter Footer;
1248
+ int BlockNum, StoreBlockNum, BlockAmount;
1249
+ int BlockSize, BlockSizeLd;
1250
+ int BlockType, Remainder;
1251
+ int Result, IsoSize;
1252
+ bool Done = false;
1253
+ uintptr_t Offset;
1254
+
1255
+ BlockAmount=Job.Amount;
1256
+ BlockNum =Job.Number;
1257
+ Result=CheckBlock(-1,-1,&opData,BlockAmount);
1258
+ if (Result==0)
1259
+ {
1260
+ Info=PS7CompactBlockInfo(&opData);
1261
+ // Gets blocktype
1262
+ BlockType=SubBlockToBlock(Info->SubBlkType);
1263
+
1264
+ if (BlockNum>=0)
1265
+ Info->BlkNum=SwapWord(BlockNum); // change the number
1266
+ else
1267
+ BlockNum=SwapWord(Info->BlkNum); // use the header's number
1268
+
1269
+ BlockSizeLd=BlockAmount; // load mem needed for this block
1270
+ BlockSize =SwapWord(Info->MC7Len); // net size
1271
+ Footer=PS7BlockFooter(pbyte(&opData)+BlockSizeLd-sizeof(TS7BlockFooter));
1272
+ Footer->Chksum=0x0000;
1273
+
1274
+ Offset=0;
1275
+ Remainder=BlockAmount;
1276
+ //<---------------------------------------------- Start Download request
1277
+ PReqStartDownloadParams ReqParams;
1278
+ PResStartDownloadParams ResParams;
1279
+ PS7ResHeader23 Answer;
1280
+
1281
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1282
+ ReqParams=PReqStartDownloadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1283
+ Answer =PS7ResHeader23(&PDU.Payload);
1284
+ ResParams=PResStartDownloadParams(pbyte(Answer)+ResHeaderSize23);
1285
+ // Init Header
1286
+ PDUH_out->P=0x32; // Always 0x32
1287
+ PDUH_out->PDUType=PduType_request; // 0x01
1288
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1289
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1290
+ PDUH_out->ParLen=SwapWord(sizeof(TReqStartDownloadParams));
1291
+ PDUH_out->DataLen=0x0000; // No data
1292
+ // Init Params
1293
+ ReqParams->FunSDwnld = pduReqDownload;
1294
+ ReqParams->Uk6[0]=0x00;
1295
+ ReqParams->Uk6[1]=0x01;
1296
+ ReqParams->Uk6[2]=0x00;
1297
+ ReqParams->Uk6[3]=0x00;
1298
+ ReqParams->Uk6[4]=0x00;
1299
+ ReqParams->Uk6[5]=0x00;
1300
+ ReqParams->Dwnld_ID=0x00;
1301
+ ReqParams->Len_1 =0x09;
1302
+ ReqParams->Prefix=0x5F;
1303
+ ReqParams->BlkPrfx=0x30;
1304
+ ReqParams->BlkType=BlockType;
1305
+ StoreBlockNum=BlockNum;
1306
+ ReqParams->AsciiBlk[0]=(BlockNum / 10000)+0x30;
1307
+ BlockNum=BlockNum % 10000;
1308
+ ReqParams->AsciiBlk[1]=(BlockNum / 1000)+0x30;
1309
+ BlockNum=BlockNum % 1000;
1310
+ ReqParams->AsciiBlk[2]=(BlockNum / 100)+0x30;
1311
+ BlockNum=BlockNum % 100;
1312
+ ReqParams->AsciiBlk[3]=(BlockNum / 10)+0x30;
1313
+ BlockNum=BlockNum % 10;
1314
+ ReqParams->AsciiBlk[4]=(BlockNum / 1)+0x30;
1315
+ ReqParams->P =0x50;
1316
+ ReqParams->Len_2=0x0D;
1317
+ ReqParams->Uk1 =0x31; // '1'
1318
+ BlockNum=StoreBlockNum;
1319
+ // Load memory
1320
+ ReqParams->AsciiLoad[0]=(BlockSizeLd / 100000)+0x30;
1321
+ BlockSizeLd=BlockSizeLd % 100000;
1322
+ ReqParams->AsciiLoad[1]=(BlockSizeLd / 10000)+0x30;
1323
+ BlockSizeLd=BlockSizeLd % 10000;
1324
+ ReqParams->AsciiLoad[2]=(BlockSizeLd / 1000)+0x30;
1325
+ BlockSizeLd=BlockSizeLd % 1000;
1326
+ ReqParams->AsciiLoad[3]=(BlockSizeLd / 100)+0x30;
1327
+ BlockSizeLd=BlockSizeLd % 100;
1328
+ ReqParams->AsciiLoad[4]=(BlockSizeLd / 10)+0x30;
1329
+ BlockSizeLd=BlockSizeLd % 10;
1330
+ ReqParams->AsciiLoad[5]=(BlockSizeLd / 1)+0x30;
1331
+ // MC7 memory
1332
+ ReqParams->AsciiMC7[0]=(BlockSize / 100000)+0x30;
1333
+ BlockSize=BlockSize % 100000;
1334
+ ReqParams->AsciiMC7[1]=(BlockSize / 10000)+0x30;
1335
+ BlockSize=BlockSize % 10000;
1336
+ ReqParams->AsciiMC7[2]=(BlockSize / 1000)+0x30;
1337
+ BlockSize=BlockSize % 1000;
1338
+ ReqParams->AsciiMC7[3]=(BlockSize / 100)+0x30;
1339
+ BlockSize=BlockSize % 100;
1340
+ ReqParams->AsciiMC7[4]=(BlockSize / 10)+0x30;
1341
+ BlockSize=BlockSize % 10;
1342
+ ReqParams->AsciiMC7[5]=(BlockSize / 1)+0x30;
1343
+
1344
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqStartDownloadParams);
1345
+ Result=isoExchangeBuffer(0,IsoSize);
1346
+
1347
+ // Get Result
1348
+ if (Result==0)
1349
+ {
1350
+ if (SwapWord(Answer->Error)!=Code7NeedPassword)
1351
+ {
1352
+ if ((Answer->Error!=0) || (*ResParams!=pduReqDownload))
1353
+ Result=errCliDownloadSequenceFailed;
1354
+ }
1355
+ else
1356
+ Result=errCliNeedPassword;
1357
+ }
1358
+ //----------------------------------------------> Start Download request
1359
+ if (Result==0)
1360
+ {
1361
+ do
1362
+ {
1363
+ //<-------------------------------- Download sequence (PLC requests)
1364
+ PReqDownloadParams ReqParams;
1365
+ PS7ResHeader23 Answer;
1366
+ PResDownloadParams ResParams;
1367
+ PResDownloadDataHeader ResData;
1368
+ int Slice, Size, MaxSlice;
1369
+ word Sequence;
1370
+ pbyte Source;
1371
+ pbyte Target;
1372
+
1373
+ ReqParams=PReqDownloadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1374
+ Answer =PS7ResHeader23(&PDU.Payload);
1375
+ ResParams=PResDownloadParams(pbyte(Answer)+ResHeaderSize23);
1376
+ ResData =PResDownloadDataHeader(pbyte(ResParams)+sizeof(TResDownloadParams));
1377
+ Target =pbyte(ResData)+sizeof(TResDownloadDataHeader);
1378
+ Source =pbyte(&opData)+Offset;
1379
+
1380
+ Result=isoRecvBuffer(0,Size);
1381
+ if (Result==0)
1382
+ {
1383
+ if ((u_int(Size)>sizeof(TS7ReqHeader)) && (ReqParams->Fun==pduDownload))
1384
+ {
1385
+ Sequence=PDUH_out->Sequence;
1386
+ // Max data slice that we can fit in this pdu
1387
+ MaxSlice=PDULength-ResHeaderSize23-sizeof(TResDownloadParams)-sizeof(TResDownloadDataHeader);
1388
+ Slice=Remainder;
1389
+ if (Slice>MaxSlice)
1390
+ Slice=MaxSlice;
1391
+ Remainder-=Slice;
1392
+ Offset+=Slice;
1393
+ Done=Remainder<=0;
1394
+ // Init Answer
1395
+ Answer->P=0x32;
1396
+ Answer->PDUType=PduType_response;
1397
+ Answer->AB_EX=0x0000;
1398
+ Answer->Sequence=Sequence;
1399
+ Answer->ParLen =SwapWord(sizeof(TResDownloadParams));
1400
+ Answer->DataLen=SwapWord(word(sizeof(TResDownloadDataHeader))+Slice);
1401
+ Answer->Error =0x0000;
1402
+
1403
+ // Init Params
1404
+ ResParams->FunDwnld=pduDownload;
1405
+ if (Remainder>0)
1406
+ ResParams->EoS=0x01;
1407
+ else
1408
+ ResParams->EoS=0x00;
1409
+
1410
+ // Init Data
1411
+ ResData->DataLen=SwapWord(Slice);
1412
+ ResData->FB_00=0xFB00;
1413
+ memcpy(Target, Source, Slice);
1414
+
1415
+ // Send the slice
1416
+ IsoSize=ResHeaderSize23+sizeof(TResDownloadParams)+sizeof(TResDownloadDataHeader)+Slice;
1417
+ Result=isoSendBuffer(0,IsoSize);
1418
+ }
1419
+ else
1420
+ Result=errCliDownloadSequenceFailed;
1421
+ };
1422
+ //--------------------------------> Download sequence (PLC requests)
1423
+ }
1424
+ while (!Done && (Result==0));
1425
+
1426
+ if (Result==0)
1427
+ {
1428
+ //<-------------------------------------------Perform Download Ended
1429
+ PReqDownloadParams ReqParams;
1430
+ PS7ResHeader23 Answer;
1431
+ PResEndDownloadParams ResParams;
1432
+ int Size;
1433
+ word Sequence;
1434
+
1435
+ ReqParams=PReqDownloadParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1436
+ Answer =PS7ResHeader23(&PDU.Payload);
1437
+ ResParams=PResEndDownloadParams(pbyte(Answer)+ResHeaderSize23);
1438
+
1439
+ Result=isoRecvBuffer(0,Size);
1440
+ if (Result==0)
1441
+ {
1442
+ if ((u_int(Size)>sizeof(TS7ReqHeader)) && (ReqParams->Fun==pduDownloadEnded))
1443
+ {
1444
+ Sequence=PDUH_out->Sequence;
1445
+ // Init Answer
1446
+ Answer->P=0x32;
1447
+ Answer->PDUType=PduType_response;
1448
+ Answer->AB_EX=0x0000;
1449
+ Answer->Sequence=Sequence;
1450
+ Answer->ParLen =SwapWord(sizeof(TResEndDownloadParams));
1451
+ Answer->DataLen=0x0000;
1452
+ Answer->Error =0x0000;
1453
+
1454
+ // Init Params
1455
+ *ResParams=pduDownloadEnded;
1456
+ IsoSize=ResHeaderSize23+sizeof(TResEndDownloadParams);
1457
+ Result=isoSendBuffer(0,IsoSize);
1458
+ }
1459
+ else
1460
+ Result=errCliDownloadSequenceFailed;
1461
+ };
1462
+ //------------------------------------------->Perform Download Ended
1463
+ if (Result==0)
1464
+ {
1465
+ //<----------------------------------- Insert block into the unit
1466
+ PReqControlBlockParams ReqParams;
1467
+ PS7ResHeader23 Answer;
1468
+ pbyte ResParams;
1469
+
1470
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1471
+ ReqParams=PReqControlBlockParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1472
+ Answer=PS7ResHeader23(&PDU.Payload);
1473
+ ResParams=pbyte(Answer)+ResHeaderSize23;
1474
+ // Init Header
1475
+ PDUH_out->P=0x32; // Always 0x32
1476
+ PDUH_out->PDUType=PduType_request; // 0x01
1477
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1478
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1479
+ PDUH_out->ParLen=SwapWord(sizeof(TReqControlBlockParams));
1480
+ PDUH_out->DataLen=0x0000; // No data
1481
+ // Init Params
1482
+ ReqParams->Fun = pduControl;
1483
+ ReqParams->Uk7[0]=0x00;
1484
+ ReqParams->Uk7[1]=0x00;
1485
+ ReqParams->Uk7[2]=0x00;
1486
+ ReqParams->Uk7[3]=0x00;
1487
+ ReqParams->Uk7[4]=0x00;
1488
+ ReqParams->Uk7[5]=0x00;
1489
+ ReqParams->Uk7[6]=0xFD;
1490
+ ReqParams->Len_1 =SwapWord(0x0A);
1491
+ ReqParams->NumOfBlocks=0x01;
1492
+ ReqParams->ByteZero =0x00;
1493
+ ReqParams->AsciiZero =0x30;
1494
+ ReqParams->BlkType=BlockType;
1495
+ ReqParams->AsciiBlk[0]=(BlockNum / 10000)+0x30;
1496
+ BlockNum=BlockNum % 10000;
1497
+ ReqParams->AsciiBlk[1]=(BlockNum / 1000)+0x30;
1498
+ BlockNum=BlockNum % 1000;
1499
+ ReqParams->AsciiBlk[2]=(BlockNum / 100)+0x30;
1500
+ BlockNum=BlockNum % 100;
1501
+ ReqParams->AsciiBlk[3]=(BlockNum / 10)+0x30;
1502
+ BlockNum=BlockNum % 10;
1503
+ ReqParams->AsciiBlk[4]=(BlockNum / 1)+0x30;
1504
+ ReqParams->SFun =SFun_Insert;
1505
+ ReqParams->Len_2=0x05;
1506
+ ReqParams->Cmd[0]='_';
1507
+ ReqParams->Cmd[1]='I';
1508
+ ReqParams->Cmd[2]='N';
1509
+ ReqParams->Cmd[3]='S';
1510
+ ReqParams->Cmd[4]='E';
1511
+
1512
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqControlBlockParams);
1513
+ Result=isoExchangeBuffer(0,IsoSize);
1514
+
1515
+ if (Result==0)
1516
+ {
1517
+ if ((Answer->Error!=0) || (*ResParams!=pduControl))
1518
+ Result=errCliInsertRefused;
1519
+ };
1520
+ //-----------------------------------> Insert block into the unit
1521
+ }
1522
+ };
1523
+ };
1524
+ };
1525
+ return Result;
1526
+ }
1527
+ //---------------------------------------------------------------------------
1528
+ int TSnap7MicroClient::opDelete()
1529
+ {
1530
+ PReqControlBlockParams ReqParams;
1531
+ PS7ResHeader23 Answer;
1532
+ pbyte ResParams;
1533
+ int IsoSize, BlockType, BlockNum, Result;
1534
+
1535
+ BlockType=Job.Area;
1536
+ BlockNum =Job.Number;
1537
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1538
+ ReqParams=PReqControlBlockParams(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1539
+ Answer =PS7ResHeader23(&PDU.Payload);
1540
+ ResParams=pbyte(Answer)+ResHeaderSize23;
1541
+ // Init Header
1542
+ PDUH_out->P=0x32; // Always 0x32
1543
+ PDUH_out->PDUType=PduType_request; // 0x01
1544
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1545
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1546
+ PDUH_out->ParLen=SwapWord(sizeof(TReqControlBlockParams));
1547
+ PDUH_out->DataLen=0x0000; // No data
1548
+ // Init Params
1549
+ ReqParams->Fun = pduControl;
1550
+ ReqParams->Uk7[0]=0x00;
1551
+ ReqParams->Uk7[1]=0x00;
1552
+ ReqParams->Uk7[2]=0x00;
1553
+ ReqParams->Uk7[3]=0x00;
1554
+ ReqParams->Uk7[4]=0x00;
1555
+ ReqParams->Uk7[5]=0x00;
1556
+ ReqParams->Uk7[6]=0xFD;
1557
+ ReqParams->Len_1 =SwapWord(0x0A);
1558
+ ReqParams->NumOfBlocks=0x01;
1559
+ ReqParams->ByteZero =0x00;
1560
+ ReqParams->AsciiZero =0x30;
1561
+ ReqParams->BlkType=BlockType;
1562
+ ReqParams->AsciiBlk[0]=(BlockNum / 10000)+0x30;
1563
+ BlockNum=BlockNum % 10000;
1564
+ ReqParams->AsciiBlk[1]=(BlockNum / 1000)+0x30;
1565
+ BlockNum=BlockNum % 1000;
1566
+ ReqParams->AsciiBlk[2]=(BlockNum / 100)+0x30;
1567
+ BlockNum=BlockNum % 100;
1568
+ ReqParams->AsciiBlk[3]=(BlockNum / 10)+0x30;
1569
+ BlockNum=BlockNum % 10;
1570
+ ReqParams->AsciiBlk[4]=(BlockNum / 1)+0x30;
1571
+ ReqParams->SFun =SFun_Delete;
1572
+ ReqParams->Len_2=0x05;
1573
+ ReqParams->Cmd[0]='_';
1574
+ ReqParams->Cmd[1]='D';
1575
+ ReqParams->Cmd[2]='E';
1576
+ ReqParams->Cmd[3]='L';
1577
+ ReqParams->Cmd[4]='E';
1578
+
1579
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqControlBlockParams);
1580
+ Result=isoExchangeBuffer(0,IsoSize);
1581
+
1582
+ if (Result==0)
1583
+ {
1584
+ if (SwapWord(Answer->Error)!=Code7NeedPassword)
1585
+ {
1586
+ if ((Answer->Error!=0) || (*ResParams!=pduControl))
1587
+ Result=errCliDeleteRefused;
1588
+ }
1589
+ else
1590
+ Result=errCliNeedPassword;
1591
+ }
1592
+ return Result;
1593
+ }
1594
+ //---------------------------------------------------------------------------
1595
+ int TSnap7MicroClient::opReadSZL()
1596
+ {
1597
+ PS7Answer17 Answer;
1598
+ PReqFunReadSZLFirst ReqParamsFirst;
1599
+ PReqFunReadSZLNext ReqParamsNext;
1600
+ PS7ReqSZLData ReqDataFirst;
1601
+ PS7ReqSZLData ReqDataNext;
1602
+ PS7ResParams7 ResParams;
1603
+ PS7ResSZLDataFirst ResDataFirst;
1604
+ PS7ResSZLDataNext ResDataNext;
1605
+ PSZL_HEADER Header;
1606
+ PS7SZLList Target;
1607
+ pbyte PDataFirst;
1608
+ pbyte PDataNext;
1609
+ word ID, Index;
1610
+ int IsoSize, DataSize, DataSZL, Result;
1611
+ bool First, Done;
1612
+ bool NoRoom = false;
1613
+ uintptr_t Offset =0;
1614
+ byte Seq_in =0x00;
1615
+
1616
+ ID=Job.ID;
1617
+ Index=Job.Index;
1618
+ opSize=0;
1619
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1620
+ ReqParamsFirst=PReqFunReadSZLFirst(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1621
+ ReqParamsNext =PReqFunReadSZLNext(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1622
+ ReqDataFirst =PS7ReqSZLData(pbyte(ReqParamsFirst)+sizeof(TReqFunReadSZLFirst));
1623
+ ReqDataNext =PS7ReqSZLData(pbyte(ReqParamsNext)+sizeof(TReqFunReadSZLNext));
1624
+
1625
+ Answer =PS7Answer17(&PDU.Payload);
1626
+ ResParams =PS7ResParams7(pbyte(Answer)+ResHeaderSize17);
1627
+ ResDataFirst =PS7ResSZLDataFirst(pbyte(ResParams)+sizeof(TS7Params7));
1628
+ ResDataNext =PS7ResSZLDataNext(pbyte(ResParams)+sizeof(TS7Params7));
1629
+ PDataFirst =pbyte(ResDataFirst)+8; // skip header
1630
+ PDataNext =pbyte(ResDataNext)+4; // skip header
1631
+ Header =PSZL_HEADER(&opData);
1632
+ First=true;
1633
+ Done =false;
1634
+ do
1635
+ {
1636
+ //<------------------------------------------------------- read slices
1637
+ if (First)
1638
+ {
1639
+ //<-------------------------------------------------- prepare first
1640
+ DataSize=sizeof(TS7ReqSZLData);
1641
+ // Fill Header
1642
+ PDUH_out->P=0x32; // Always 0x32
1643
+ PDUH_out->PDUType=PduType_userdata; // 0x07
1644
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1645
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1646
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunReadSZLFirst)); // 8 bytes params
1647
+ PDUH_out->DataLen=SwapWord(DataSize); // 8/4 bytes data
1648
+ // Fill Params
1649
+ ReqParamsFirst->Head[0]=0x00;
1650
+ ReqParamsFirst->Head[1]=0x01;
1651
+ ReqParamsFirst->Head[2]=0x12;
1652
+ ReqParamsFirst->Plen =0x04;
1653
+ ReqParamsFirst->Uk =0x11;
1654
+ ReqParamsFirst->Tg =grSZL;
1655
+ ReqParamsFirst->SubFun =SFun_ReadSZL; //0x03
1656
+ ReqParamsFirst->Seq =Seq_in;
1657
+ // Fill Data
1658
+ ReqDataFirst->Ret =0xFF;
1659
+ ReqDataFirst->TS =TS_ResOctet;
1660
+ ReqDataFirst->DLen =SwapWord(0x0004);
1661
+ ReqDataFirst->ID =SwapWord(ID);
1662
+ ReqDataFirst->Index =SwapWord(Index);
1663
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunReadSZLFirst)+DataSize;
1664
+ //--------------------------------------------------> prepare first
1665
+ }
1666
+ else
1667
+ {
1668
+ //<-------------------------------------------------- prepare next
1669
+ DataSize=sizeof(TS7ReqSZLData)-4;
1670
+ // Fill Header
1671
+ PDUH_out->P=0x32; // Always 0x32
1672
+ PDUH_out->PDUType=PduType_userdata; // 0x07
1673
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1674
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1675
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunReadSZLNext)); // 8 bytes params
1676
+ PDUH_out->DataLen=SwapWord(DataSize);// 8/4 bytes data
1677
+ // Fill Params
1678
+ ReqParamsNext->Head[0]=0x00;
1679
+ ReqParamsNext->Head[1]=0x01;
1680
+ ReqParamsNext->Head[2]=0x12;
1681
+ ReqParamsNext->Plen =0x08;
1682
+ ReqParamsNext->Uk =0x12;
1683
+ ReqParamsNext->Tg =grSZL;
1684
+ ReqParamsNext->SubFun =SFun_ReadSZL;
1685
+ ReqParamsNext->Seq =Seq_in;
1686
+ ReqParamsNext->Rsvd =0x0000;
1687
+ ReqParamsNext->ErrNo =0x0000;
1688
+ // Fill Data
1689
+ ReqDataNext->Ret =0x0A;
1690
+ ReqDataNext->TS =0x00;
1691
+ ReqDataNext->DLen =0x0000;
1692
+ ReqDataNext->ID =0x0000;
1693
+ ReqDataNext->Index =0x0000;
1694
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunReadSZLNext)+DataSize;
1695
+ //--------------------------------------------------> prepare next
1696
+ }
1697
+ Result=isoExchangeBuffer(0,IsoSize);
1698
+ // Get Data
1699
+ if (Result==0)
1700
+ {
1701
+ if (First)
1702
+ {
1703
+ //<------------------------------------------ get data first
1704
+ if (ResParams->Err==0)
1705
+ {
1706
+ if (ResDataFirst->Ret==0xFF) // <-- 0xFF means Result OK
1707
+ {
1708
+ // Gets Amount of this slice
1709
+ DataSZL=SwapWord(ResDataFirst->DLen)-4;// Skips extra params (ID, Index ...)
1710
+ // Gets end of Sequence Flag
1711
+ Done=(ResParams->resvd & 0xFF00) == 0; // Low order byte = 0x00 => the sequence is done
1712
+ // Gets Unit's function sequence
1713
+ Seq_in=ResParams->Seq;
1714
+ Target=PS7SZLList(pbyte(&opData)+Offset);
1715
+ memcpy(Target, PDataFirst, DataSZL);
1716
+ Offset+=DataSZL;
1717
+ }
1718
+ else
1719
+ Result=CpuError(ResDataFirst->Ret);
1720
+ }
1721
+ else
1722
+ Result=CpuError(ResDataFirst->Ret);
1723
+ //------------------------------------------> get data first
1724
+ }
1725
+ else
1726
+ {
1727
+ //<------------------------------------------ get data next
1728
+ if (ResParams->Err==0)
1729
+ {
1730
+ if (ResDataNext->Ret==0xFF) // <-- 0xFF means Result OK
1731
+ {
1732
+ // Gets Amount of this slice
1733
+ DataSZL=SwapWord(ResDataNext->DLen);
1734
+ // Gets end of Sequence Flag
1735
+ Done=(ResParams->resvd & 0xFF00) == 0; // Low order byte = 0x00 => the sequence is done
1736
+ // Gets Unit's function sequence
1737
+ Seq_in=ResParams->Seq;
1738
+ Target=PS7SZLList(pbyte(&opData)+Offset);
1739
+ memcpy(Target, PDataNext, DataSZL);
1740
+ Offset+=DataSZL;
1741
+ }
1742
+ else
1743
+ Result=CpuError(ResDataNext->Ret);
1744
+ }
1745
+ else
1746
+ Result=CpuError(ResDataNext->Ret);
1747
+ //------------------------------------------> get data next
1748
+ }
1749
+ First=false;
1750
+ }
1751
+ //-------------------------------------------------------> read slices
1752
+ }
1753
+ while ((!Done) && (Result==0));
1754
+
1755
+ // Check errors and adjust header
1756
+ if (Result==0)
1757
+ {
1758
+ // Adjust big endian header
1759
+ Header->LENTHDR=SwapWord(Header->LENTHDR);
1760
+ Header->N_DR =SwapWord(Header->N_DR);
1761
+ opSize=int(Offset);
1762
+
1763
+ if (Job.IParam==1) // if 1 data has to be copied into user buffer
1764
+ {
1765
+ // Check buffer size
1766
+ if (opSize>Job.Amount)
1767
+ {
1768
+ opSize=Job.Amount;
1769
+ NoRoom=true;
1770
+ }
1771
+ memcpy(Job.pData, &opData, opSize);
1772
+ *Job.pAmount=opSize;
1773
+ };
1774
+ };
1775
+ if ((Result==0)&& NoRoom)
1776
+ Result=errCliBufferTooSmall;
1777
+
1778
+ return Result;
1779
+ }
1780
+ //---------------------------------------------------------------------------
1781
+ int TSnap7MicroClient::opReadSZLList()
1782
+ {
1783
+ PS7SZLList usrSZLList, opDataList;
1784
+ int ItemsCount, ItemsCount_in, c, Result;
1785
+ bool NoRoom = false;
1786
+
1787
+ Job.ID =0x0000;
1788
+ Job.Index =0x0000;
1789
+ Job.IParam =0;
1790
+ ItemsCount_in=Job.Amount; // stores the room
1791
+ Job.Amount =sizeof(opData); // read into the internal buffer
1792
+
1793
+ Result =opReadSZL();
1794
+ if (Result==0)
1795
+ {
1796
+ opDataList=PS7SZLList(&opData); // Source
1797
+ usrSZLList=PS7SZLList(Job.pData); // Target
1798
+
1799
+ ItemsCount=(opSize-sizeof(SZL_HEADER)) / 2;
1800
+ // Check input size
1801
+ if (ItemsCount>ItemsCount_in)
1802
+ {
1803
+ ItemsCount=ItemsCount_in; // Trim itemscount
1804
+ NoRoom=true;
1805
+ }
1806
+ for (c = 0; c < ItemsCount; c++)
1807
+ usrSZLList->List[c]=SwapWord(opDataList->List[c]);
1808
+ *Job.pAmount=ItemsCount;
1809
+ }
1810
+ else
1811
+ *Job.pAmount=0;
1812
+
1813
+ if ((Result==0) && NoRoom)
1814
+ Result=errCliBufferTooSmall;
1815
+
1816
+ return Result;
1817
+ }
1818
+ //---------------------------------------------------------------------------
1819
+ byte TSnap7MicroClient::BCDtoByte(byte B)
1820
+ {
1821
+ return ((B >> 4) * 10) + (B & 0x0F);
1822
+ }
1823
+ //---------------------------------------------------------------------------
1824
+ byte TSnap7MicroClient::WordToBCD(word Value)
1825
+ {
1826
+ return ((Value / 10) << 4) | (Value % 10);
1827
+ }
1828
+ //---------------------------------------------------------------------------
1829
+ int TSnap7MicroClient::opGetDateTime()
1830
+ {
1831
+ PTimeStruct DateTime;
1832
+ PReqFunDateTime ReqParams;
1833
+ PReqDataGetDateTime ReqData;
1834
+ PS7ResParams7 ResParams;
1835
+ PResDataGetTime ResData;
1836
+ PS7ResHeader17 Answer;
1837
+ int IsoSize, Result;
1838
+ word AYear;
1839
+
1840
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1841
+ ReqParams=PReqFunDateTime(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1842
+ ReqData =PReqDataGetDateTime(pbyte(ReqParams)+sizeof(TReqFunDateTime));
1843
+ Answer =PS7ResHeader17(&PDU.Payload);
1844
+ ResParams=PS7ResParams7(pbyte(Answer)+ResHeaderSize17);
1845
+ ResData =PResDataGetTime(pbyte(ResParams)+sizeof(TS7Params7));
1846
+ DateTime =PTimeStruct(Job.pData);
1847
+ // Fill Header
1848
+ PDUH_out->P=0x32; // Always 0x32
1849
+ PDUH_out->PDUType=PduType_userdata; // 0x07
1850
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1851
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1852
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunDateTime)); // 8 bytes params
1853
+ PDUH_out->DataLen=SwapWord(sizeof(TReqDataGetDateTime)); // 4 bytes data
1854
+ // Fill params (mostly constants)
1855
+ ReqParams->Head[0]=0x00;
1856
+ ReqParams->Head[1]=0x01;
1857
+ ReqParams->Head[2]=0x12;
1858
+ ReqParams->Plen =0x04;
1859
+ ReqParams->Uk =0x11;
1860
+ ReqParams->Tg =grClock;
1861
+ ReqParams->SubFun =SFun_ReadClock;
1862
+ ReqParams->Seq =0x00;
1863
+ // Fill Data
1864
+ *ReqData =0x0000000A;
1865
+
1866
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunDateTime)+sizeof(TReqDataGetDateTime);
1867
+ Result=isoExchangeBuffer(0,IsoSize);
1868
+
1869
+ // Get Data
1870
+ if (Result==0)
1871
+ {
1872
+ if (ResParams->Err==0)
1873
+ {
1874
+ if (ResData->RetVal==0xFF) // <-- 0xFF means Result OK
1875
+ {
1876
+ // Decode Plc Date and Time
1877
+ AYear=BCDtoByte(ResData->Time[0]);
1878
+ if (AYear<90)
1879
+ AYear=AYear+100;
1880
+ DateTime->tm_year=AYear;
1881
+ DateTime->tm_mon =BCDtoByte(ResData->Time[1])-1;
1882
+ DateTime->tm_mday=BCDtoByte(ResData->Time[2]);
1883
+ DateTime->tm_hour=BCDtoByte(ResData->Time[3]);
1884
+ DateTime->tm_min =BCDtoByte(ResData->Time[4]);
1885
+ DateTime->tm_sec =BCDtoByte(ResData->Time[5]);
1886
+ DateTime->tm_wday=(ResData->Time[7] & 0x0F)-1;
1887
+ }
1888
+ else
1889
+ Result=CpuError(ResData->RetVal);
1890
+ }
1891
+ else
1892
+ Result=CpuError(ResData->RetVal);
1893
+ }
1894
+ return Result;
1895
+ }
1896
+ //---------------------------------------------------------------------------
1897
+ int TSnap7MicroClient::opSetDateTime()
1898
+ {
1899
+ PTimeStruct DateTime;
1900
+ PReqFunDateTime ReqParams;
1901
+ PReqDataSetTime ReqData;
1902
+ PS7ResParams7 ResParams;
1903
+ PS7ResHeader17 Answer;
1904
+ word AYear;
1905
+ int IsoSize, Result;
1906
+
1907
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
1908
+ ReqParams=PReqFunDateTime(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
1909
+ ReqData =PReqDataSetTime(pbyte(ReqParams)+sizeof(TReqFunDateTime));
1910
+ Answer =PS7ResHeader17(&PDU.Payload);
1911
+ ResParams=PS7ResParams7(pbyte(Answer)+ResHeaderSize17);
1912
+ DateTime =PTimeStruct(Job.pData);
1913
+ // Fill Header
1914
+ PDUH_out->P=0x32; // Always 0x32
1915
+ PDUH_out->PDUType=PduType_userdata; // 0x07
1916
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
1917
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
1918
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunDateTime)); // 8 bytes params
1919
+ PDUH_out->DataLen=SwapWord(sizeof(TReqDataSetTime)); // 4 bytes data
1920
+ // Fill params (mostly constants)
1921
+ ReqParams->Head[0]=0x00;
1922
+ ReqParams->Head[1]=0x01;
1923
+ ReqParams->Head[2]=0x12;
1924
+ ReqParams->Plen =0x04;
1925
+ ReqParams->Uk =0x11;
1926
+ ReqParams->Tg =grClock;
1927
+ ReqParams->SubFun =SFun_SetClock;
1928
+ ReqParams->Seq =0x00;
1929
+ // EncodeSiemensDateTime;
1930
+ if (DateTime->tm_year<100)
1931
+ AYear=DateTime->tm_year;
1932
+ else
1933
+ AYear=DateTime->tm_year-100;
1934
+
1935
+ ReqData->RetVal=0xFF;
1936
+ ReqData->TSize =TS_ResOctet;
1937
+ ReqData->Length=SwapWord(0x000A);
1938
+ ReqData->Rsvd =0x00;
1939
+ ReqData->HiYear=0x19; // *must* be 19 tough it's not the Hi part of the year...
1940
+
1941
+ ReqData->Time[0]=WordToBCD(AYear);
1942
+ ReqData->Time[1]=WordToBCD(DateTime->tm_mon+1);
1943
+ ReqData->Time[2]=WordToBCD(DateTime->tm_mday);
1944
+ ReqData->Time[3]=WordToBCD(DateTime->tm_hour);
1945
+ ReqData->Time[4]=WordToBCD(DateTime->tm_min);
1946
+ ReqData->Time[5]=WordToBCD(DateTime->tm_sec);
1947
+ ReqData->Time[6]=0;
1948
+ ReqData->Time[7]=DateTime->tm_wday+1;
1949
+
1950
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunDateTime)+sizeof(TReqDataSetTime);
1951
+ Result=isoExchangeBuffer(0,IsoSize);
1952
+
1953
+ // Get Result
1954
+ if (Result==0)
1955
+ {
1956
+ if (ResParams->Err!=0)
1957
+ Result=CpuError(SwapWord(ResParams->Err));
1958
+ };
1959
+ return Result;
1960
+ }
1961
+ //---------------------------------------------------------------------------
1962
+ int TSnap7MicroClient::opGetOrderCode()
1963
+ {
1964
+ PS7OrderCode OC;
1965
+ int Result;
1966
+
1967
+ Job.ID =0x0011;
1968
+ Job.Index =0x0000;
1969
+ Job.IParam =0;
1970
+ Result =opReadSZL();
1971
+ if (Result==0)
1972
+ {
1973
+ OC=PS7OrderCode(Job.pData);
1974
+ memset(OC,0,sizeof(TS7OrderCode));
1975
+ memcpy(OC->Code,&opData[6],20);
1976
+ OC->V1=opData[opSize-3];
1977
+ OC->V2=opData[opSize-2];
1978
+ OC->V3=opData[opSize-1];
1979
+ };
1980
+ return Result;
1981
+ }
1982
+ //---------------------------------------------------------------------------
1983
+ int TSnap7MicroClient::opGetCpuInfo()
1984
+ {
1985
+ PS7CpuInfo Info;
1986
+ int Result;
1987
+
1988
+ // Store Pointer
1989
+ Info=PS7CpuInfo(Job.pData);
1990
+ // Clear data in order to have the end of strings (\0) correctly setted
1991
+ memset(Info, 0, sizeof(TS7CpuInfo));
1992
+
1993
+ Job.ID =0x001C;
1994
+ Job.Index =0x0000;
1995
+ Job.IParam=0;
1996
+ Result =opReadSZL();
1997
+ if (Result==0)
1998
+ {
1999
+ memcpy(Info->ModuleTypeName,&opData[176],32);
2000
+ memcpy(Info->SerialNumber,&opData[142],24);
2001
+ memcpy(Info->ASName,&opData[6],24);
2002
+ memcpy(Info->Copyright,&opData[108],26);
2003
+ memcpy(Info->ModuleName,&opData[40],24);
2004
+ }
2005
+ return Result;
2006
+ }
2007
+ //---------------------------------------------------------------------------
2008
+ int TSnap7MicroClient::opGetCpInfo()
2009
+ {
2010
+ PS7CpInfo Info;
2011
+ int Result;
2012
+ // Store Pointer
2013
+ Info=PS7CpInfo(Job.pData);
2014
+ memset(Info,0,sizeof(TS7CpInfo));
2015
+ Job.ID =0x0131;
2016
+ Job.Index =0x0001;
2017
+ Job.IParam=0;
2018
+ Result =opReadSZL();
2019
+ if (Result==0)
2020
+ {
2021
+ Info->MaxPduLengt=opData[6]*256+opData[7];
2022
+ Info->MaxConnections=opData[8]*256+opData[9];
2023
+ Info->MaxMpiRate=DWordAt(&opData[10]);
2024
+ Info->MaxBusRate=DWordAt(&opData[14]);
2025
+ };
2026
+ return Result;
2027
+ }
2028
+ //---------------------------------------------------------------------------
2029
+ int TSnap7MicroClient::opGetPlcStatus()
2030
+ {
2031
+ int *Status;
2032
+ int Result;
2033
+
2034
+ Status =(int*)Job.pData;
2035
+ Job.ID =0x0424;
2036
+ Job.Index =0x0000;
2037
+ Job.IParam =0;
2038
+ Result =opReadSZL();
2039
+ if (Result==0)
2040
+ {
2041
+ switch (opData[7])
2042
+ {
2043
+ case S7CpuStatusUnknown :
2044
+ case S7CpuStatusRun :
2045
+ case S7CpuStatusStop : *Status=opData[7];
2046
+ break;
2047
+ default :
2048
+ // Since RUN status is always $08 for all CPUs and CPs, STOP status
2049
+ // sometime can be coded as $03 (especially for old cpu...)
2050
+ *Status=S7CpuStatusStop;
2051
+ }
2052
+ }
2053
+ else
2054
+ *Status=0;
2055
+ return Result;
2056
+ }
2057
+ //---------------------------------------------------------------------------
2058
+ int TSnap7MicroClient::opPlcStop()
2059
+ {
2060
+ PReqFunPlcStop ReqParams;
2061
+ PResFunCtrl ResParams;
2062
+ PS7ResHeader23 Answer;
2063
+ int IsoSize, Result;
2064
+
2065
+ char p_program[] = {'P','_','P','R','O','G','R','A','M'};
2066
+
2067
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2068
+ ReqParams=PReqFunPlcStop(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2069
+ Answer =PS7ResHeader23(&PDU.Payload);
2070
+ ResParams=PResFunCtrl(pbyte(Answer)+ResHeaderSize23);
2071
+ // Fill Header
2072
+ PDUH_out->P=0x32; // Always 0x32
2073
+ PDUH_out->PDUType=PduType_request; // 0x01
2074
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2075
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2076
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunPlcStop));
2077
+ PDUH_out->DataLen=0x0000; // No Data
2078
+ // Fill Params
2079
+ ReqParams->Fun=pduStop;
2080
+ memset(ReqParams->Uk_5,0,5);
2081
+ ReqParams->Len_2=0x09;
2082
+ memcpy(ReqParams->Cmd,&p_program,9);
2083
+
2084
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunPlcStop);
2085
+ Result=isoExchangeBuffer(0,IsoSize);
2086
+
2087
+ if (Result==0)
2088
+ {
2089
+ if (Answer->Error!=0)
2090
+ {
2091
+ if (ResParams->ResFun!=pduStop)
2092
+ Result=errCliCannotStopPLC;
2093
+ else
2094
+ if (ResParams->para ==0x07)
2095
+ Result=errCliAlreadyStop;
2096
+ else
2097
+ Result=errCliCannotStopPLC;
2098
+ };
2099
+ };
2100
+ return Result;
2101
+ }
2102
+ //---------------------------------------------------------------------------
2103
+ int TSnap7MicroClient::opPlcHotStart()
2104
+ {
2105
+ PReqFunPlcHotStart ReqParams;
2106
+ PResFunCtrl ResParams;
2107
+ PS7ResHeader23 Answer;
2108
+ int IsoSize, Result;
2109
+
2110
+ char p_program[] = {'P','_','P','R','O','G','R','A','M'};
2111
+
2112
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2113
+ ReqParams=PReqFunPlcHotStart(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2114
+ Answer =PS7ResHeader23(&PDU.Payload);
2115
+ ResParams=PResFunCtrl(pbyte(Answer)+ResHeaderSize23);
2116
+ // Fill Header
2117
+ PDUH_out->P=0x32; // Always 0x32
2118
+ PDUH_out->PDUType=PduType_request; // 0x01
2119
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2120
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2121
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunPlcHotStart)); // 16 bytes params
2122
+ PDUH_out->DataLen=0x0000; // No Data
2123
+ // Fill Params
2124
+ ReqParams->Fun=pduStart;
2125
+ ReqParams->Uk_7[0]=0x00;
2126
+ ReqParams->Uk_7[1]=0x00;
2127
+ ReqParams->Uk_7[2]=0x00;
2128
+ ReqParams->Uk_7[3]=0x00;
2129
+ ReqParams->Uk_7[4]=0x00;
2130
+ ReqParams->Uk_7[5]=0x00;
2131
+ ReqParams->Uk_7[6]=0xFD;
2132
+
2133
+ ReqParams->Len_1=0x0000;
2134
+ ReqParams->Len_2=0x09;
2135
+ memcpy(ReqParams->Cmd,&p_program,9);
2136
+
2137
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunPlcHotStart);
2138
+ Result=isoExchangeBuffer(0,IsoSize);
2139
+
2140
+ if (Result==0)
2141
+ {
2142
+ if ((Answer->Error!=0))
2143
+ {
2144
+ if ((ResParams->ResFun!=pduStart))
2145
+ Result=errCliCannotStartPLC;
2146
+ else
2147
+ {
2148
+ if (ResParams->para==0x03)
2149
+ Result=errCliAlreadyRun;
2150
+ else
2151
+ if (ResParams->para==0x02)
2152
+ Result=errCliCannotStartPLC;
2153
+ else
2154
+ Result=errCliCannotStartPLC;
2155
+ }
2156
+ }
2157
+ };
2158
+ return Result;
2159
+ }
2160
+ //---------------------------------------------------------------------------
2161
+ int TSnap7MicroClient::opPlcColdStart()
2162
+ {
2163
+ PReqFunPlcColdStart ReqParams;
2164
+ PResFunCtrl ResParams;
2165
+ PS7ResHeader23 Answer;
2166
+ int IsoSize, Result;
2167
+ char p_program[] = {'P','_','P','R','O','G','R','A','M'};
2168
+
2169
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2170
+ ReqParams=PReqFunPlcColdStart(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2171
+ Answer =PS7ResHeader23(&PDU.Payload);
2172
+ ResParams=PResFunCtrl(pbyte(Answer)+ResHeaderSize23);
2173
+ // Fill Header
2174
+ PDUH_out->P=0x32; // Always 0x32
2175
+ PDUH_out->PDUType=PduType_request; // 0x01
2176
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2177
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2178
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunPlcColdStart)); // 22 bytes params
2179
+ PDUH_out->DataLen=0x0000; // No Data
2180
+ // Fill Params
2181
+ ReqParams->Fun=pduStart;
2182
+ ReqParams->Uk_7[0]=0x00;
2183
+ ReqParams->Uk_7[1]=0x00;
2184
+ ReqParams->Uk_7[2]=0x00;
2185
+ ReqParams->Uk_7[3]=0x00;
2186
+ ReqParams->Uk_7[4]=0x00;
2187
+ ReqParams->Uk_7[5]=0x00;
2188
+ ReqParams->Uk_7[6]=0xFD;
2189
+
2190
+ ReqParams->Len_1=SwapWord(0x0002);
2191
+ ReqParams->SFun =SwapWord(0x4320); // Cold start
2192
+ ReqParams->Len_2=0x09;
2193
+ memcpy(ReqParams->Cmd,&p_program,9);
2194
+
2195
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunPlcColdStart);
2196
+ Result=isoExchangeBuffer(0,IsoSize);
2197
+
2198
+ if (Result==0)
2199
+ {
2200
+ if ((Answer->Error!=0))
2201
+ {
2202
+ if ((ResParams->ResFun!=pduStart))
2203
+ Result=errCliCannotStartPLC;
2204
+ else
2205
+ {
2206
+ if (ResParams->para==0x03)
2207
+ Result=errCliAlreadyRun;
2208
+ else
2209
+ if (ResParams->para==0x02)
2210
+ Result=errCliCannotStartPLC;
2211
+ else
2212
+ Result=errCliCannotStartPLC;
2213
+ }
2214
+ }
2215
+ };
2216
+ return Result;
2217
+ }
2218
+ //---------------------------------------------------------------------------
2219
+ int TSnap7MicroClient::opCopyRamToRom()
2220
+ {
2221
+ PReqFunCopyRamToRom ReqParams;
2222
+ PResFunCtrl ResParams;
2223
+ PS7ResHeader23 Answer;
2224
+ int IsoSize, CurTimeout, Result;
2225
+ char _modu[] = {'_','M','O','D','U'};
2226
+
2227
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2228
+ ReqParams=PReqFunCopyRamToRom(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2229
+ Answer =PS7ResHeader23(&PDU.Payload);
2230
+ ResParams=PResFunCtrl(pbyte(Answer)+ResHeaderSize23);
2231
+ // Fill Header
2232
+ PDUH_out->P=0x32; // Always 0x32
2233
+ PDUH_out->PDUType=PduType_request; // 0x01
2234
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2235
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2236
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunCopyRamToRom));
2237
+ PDUH_out->DataLen=0x0000; // No Data
2238
+ // Fill Params
2239
+ ReqParams->Fun=pduControl;
2240
+ ReqParams->Uk_7[0]=0x00;
2241
+ ReqParams->Uk_7[1]=0x00;
2242
+ ReqParams->Uk_7[2]=0x00;
2243
+ ReqParams->Uk_7[3]=0x00;
2244
+ ReqParams->Uk_7[4]=0x00;
2245
+ ReqParams->Uk_7[5]=0x00;
2246
+ ReqParams->Uk_7[6]=0xFD;
2247
+
2248
+ ReqParams->Len_1=SwapWord(0x0002);
2249
+ ReqParams->SFun =SwapWord(0x4550);
2250
+ ReqParams->Len_2=0x05;
2251
+ memcpy(ReqParams->Cmd,&_modu,5);
2252
+
2253
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunCopyRamToRom);
2254
+ // Changes the timeout
2255
+ CurTimeout=RecvTimeout;
2256
+ RecvTimeout=Job.IParam;
2257
+ Result=isoExchangeBuffer(0,IsoSize);
2258
+ // Restores the timeout
2259
+ RecvTimeout=CurTimeout;
2260
+
2261
+ if (Result==0)
2262
+ {
2263
+ if ((Answer->Error!=0) || (ResParams->ResFun!=pduControl))
2264
+ Result=errCliCannotCopyRamToRom;
2265
+ }
2266
+ return Result;
2267
+ }
2268
+ //---------------------------------------------------------------------------
2269
+ int TSnap7MicroClient::opCompress()
2270
+ {
2271
+ PReqFunCompress ReqParams;
2272
+ PResFunCtrl ResParams;
2273
+ PS7ResHeader23 Answer;
2274
+ int IsoSize, CurTimeout, Result;
2275
+ char _garb[] = {'_','G','A','R','B'};
2276
+
2277
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2278
+ ReqParams=PReqFunCompress(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2279
+ Answer =PS7ResHeader23(&PDU.Payload);
2280
+ ResParams=PResFunCtrl(pbyte(Answer)+ResHeaderSize23);
2281
+ // Fill Header
2282
+ PDUH_out->P=0x32; // Always 0x32
2283
+ PDUH_out->PDUType=PduType_request; // 0x01
2284
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2285
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2286
+ PDUH_out->ParLen=SwapWord(sizeof(TReqFunCompress));
2287
+ PDUH_out->DataLen=0x0000; // No Data
2288
+ // Fill Params
2289
+ ReqParams->Fun=pduControl;
2290
+ ReqParams->Uk_7[0]=0x00;
2291
+ ReqParams->Uk_7[1]=0x00;
2292
+ ReqParams->Uk_7[2]=0x00;
2293
+ ReqParams->Uk_7[3]=0x00;
2294
+ ReqParams->Uk_7[4]=0x00;
2295
+ ReqParams->Uk_7[5]=0x00;
2296
+ ReqParams->Uk_7[6]=0xFD;
2297
+
2298
+ ReqParams->Len_1=0x0000;
2299
+ ReqParams->Len_2=0x05;
2300
+ memcpy(ReqParams->Cmd,&_garb,5);
2301
+
2302
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunCompress);
2303
+ // Changes the timeout
2304
+ CurTimeout=RecvTimeout;
2305
+ RecvTimeout=Job.IParam;
2306
+ Result=isoExchangeBuffer(0,IsoSize);
2307
+ // Restores the timeout
2308
+ RecvTimeout=CurTimeout;
2309
+
2310
+ if (Result==0)
2311
+ {
2312
+ if (((Answer->Error!=0) || (ResParams->ResFun!=pduControl)))
2313
+ Result=errCliCannotCompress;
2314
+ }
2315
+ return Result;
2316
+ }
2317
+ //---------------------------------------------------------------------------
2318
+ int TSnap7MicroClient::opGetProtection()
2319
+ {
2320
+ PS7Protection Info, usrInfo;
2321
+ int Result;
2322
+
2323
+ // Store Pointer
2324
+ usrInfo=PS7Protection(Job.pData);
2325
+ memset(usrInfo, 0, sizeof(TS7Protection));
2326
+
2327
+ Job.ID =0x0232;
2328
+ Job.Index =0x0004;
2329
+ Job.IParam=0; // No copy in Usr Data pointed by Job.pData
2330
+ Result =opReadSZL();
2331
+ if (Result==0)
2332
+ {
2333
+ Info=PS7Protection(pbyte(&opData)+6);
2334
+ usrInfo->sch_schal=SwapWord(Info->sch_schal);
2335
+ usrInfo->sch_par =SwapWord(Info->sch_par);
2336
+ usrInfo->sch_rel =SwapWord(Info->sch_rel);
2337
+ usrInfo->bart_sch =SwapWord(Info->bart_sch);
2338
+ usrInfo->anl_sch =SwapWord(Info->anl_sch);
2339
+ }
2340
+ return Result;
2341
+ }
2342
+ //******************************************************************************
2343
+ // NOTE
2344
+ // PASSWORD HACKING IS VERY FAR FROM THE AIM OF THIS PROJECT
2345
+ // NEXT FUNCTION ONLY ENCODES THE ASCII PASSWORD TO BE DOWNLOADED IN THE PLC.
2346
+ //
2347
+ // MOREOVER **YOU NEED TO KNOW** THE CORRECT PASSWORD TO MEET THE CPU
2348
+ // SECURITY LEVEL
2349
+ //******************************************************************************
2350
+ int TSnap7MicroClient::opSetPassword()
2351
+ {
2352
+ PReqFunSecurity ReqParams;
2353
+ PReqDataSecurity ReqData;
2354
+ PResParamsSecurity ResParams;
2355
+ PS7ResHeader23 Answer;
2356
+ int c, IsoSize, Result;
2357
+
2358
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2359
+ ReqParams=PReqFunSecurity(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2360
+ ReqData =PReqDataSecurity(pbyte(ReqParams)+sizeof(TReqFunSecurity));
2361
+ Answer =PS7ResHeader23(&PDU.Payload);
2362
+ ResParams=PResParamsSecurity(pbyte(Answer)+ResHeaderSize17);
2363
+ // Fill Header
2364
+ PDUH_out->P=0x32; // Always 0x32
2365
+ PDUH_out->PDUType=PduType_userdata; // 0x07
2366
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2367
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2368
+ PDUH_out->ParLen =SwapWord(sizeof(TReqFunSecurity));
2369
+ PDUH_out->DataLen=SwapWord(sizeof(TReqDataSecurity));
2370
+ // Fill params (mostly constants)
2371
+ ReqParams->Head[0]=0x00;
2372
+ ReqParams->Head[1]=0x01;
2373
+ ReqParams->Head[2]=0x12;
2374
+ ReqParams->Plen =0x04;
2375
+ ReqParams->Uk =0x11;
2376
+ ReqParams->Tg =grSecurity;
2377
+ ReqParams->SubFun =SFun_EnterPwd;
2378
+ ReqParams->Seq =0x00;
2379
+ // Fill Data
2380
+ ReqData->Ret =0xFF;
2381
+ ReqData->TS =TS_ResOctet;
2382
+ ReqData->DLen =SwapWord(0x0008); // 8 bytes data : password
2383
+ // Encode the password
2384
+ ReqData->Pwd[0]=opData[0] ^ 0x55;
2385
+ ReqData->Pwd[1]=opData[1] ^ 0x55;
2386
+ for (c = 2; c < 8; c++){
2387
+ ReqData->Pwd[c]=opData[c] ^ 0x55 ^ ReqData->Pwd[c-2];
2388
+ };
2389
+
2390
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunSecurity)+sizeof(TReqDataSecurity);
2391
+ Result=isoExchangeBuffer(0,IsoSize);
2392
+
2393
+ // Get Return
2394
+ if (Result==0)
2395
+ {
2396
+ if (ResParams->Err!=0)
2397
+ Result=CpuError(SwapWord(ResParams->Err));
2398
+ };
2399
+
2400
+ return Result;
2401
+ }
2402
+ //---------------------------------------------------------------------------
2403
+ int TSnap7MicroClient::opClearPassword()
2404
+ {
2405
+ PReqFunSecurity ReqParams;
2406
+ PReqDataSecurity ReqData;
2407
+ PResParamsSecurity ResParams;
2408
+ PS7ResHeader23 Answer;
2409
+ int IsoSize, Result;
2410
+
2411
+ // Setup pointers (note : PDUH_out and PDU.Payload are the same pointer)
2412
+ ReqParams=PReqFunSecurity(pbyte(PDUH_out)+sizeof(TS7ReqHeader));
2413
+ ReqData =PReqDataSecurity(pbyte(ReqParams)+sizeof(TReqFunSecurity));
2414
+ Answer =PS7ResHeader23(&PDU.Payload);
2415
+ ResParams=PResParamsSecurity(pbyte(Answer)+ResHeaderSize17);
2416
+ // Fill Header
2417
+ PDUH_out->P=0x32; // Always 0x32
2418
+ PDUH_out->PDUType=PduType_userdata; // 0x07
2419
+ PDUH_out->AB_EX=0x0000; // Always 0x0000
2420
+ PDUH_out->Sequence=GetNextWord(); // AutoInc
2421
+ PDUH_out->ParLen =SwapWord(sizeof(TReqFunSecurity));
2422
+ PDUH_out->DataLen=SwapWord(0x0004); // We need only 4 bytes
2423
+ // Fill params (mostly constants)
2424
+ ReqParams->Head[0]=0x00;
2425
+ ReqParams->Head[1]=0x01;
2426
+ ReqParams->Head[2]=0x12;
2427
+ ReqParams->Plen =0x04;
2428
+ ReqParams->Uk =0x11;
2429
+ ReqParams->Tg =grSecurity;
2430
+ ReqParams->SubFun =SFun_CancelPwd;
2431
+ ReqParams->Seq =0x00;
2432
+ // Fill Data
2433
+ ReqData->Ret =0x0A;
2434
+ ReqData->TS =0x00;
2435
+ ReqData->DLen =0x0000;
2436
+
2437
+ IsoSize=sizeof(TS7ReqHeader)+sizeof(TReqFunSecurity)+4;
2438
+ Result=isoExchangeBuffer(0,IsoSize);
2439
+
2440
+ // Get Return
2441
+ if (Result==0)
2442
+ {
2443
+ if (ResParams->Err!=0)
2444
+ Result=CpuError(SwapWord(ResParams->Err));
2445
+ };
2446
+ return Result;
2447
+ }
2448
+ //---------------------------------------------------------------------------
2449
+ int TSnap7MicroClient::CpuError(int Error)
2450
+ {
2451
+ switch(Error)
2452
+ {
2453
+ case 0 : return 0;
2454
+ case Code7AddressOutOfRange : return errCliAddressOutOfRange;
2455
+ case Code7InvalidTransportSize : return errCliInvalidTransportSize;
2456
+ case Code7WriteDataSizeMismatch : return errCliWriteDataSizeMismatch;
2457
+ case Code7ResItemNotAvailable :
2458
+ case Code7ResItemNotAvailable1 : return errCliItemNotAvailable;
2459
+ case Code7DataOverPDU : return errCliSizeOverPDU;
2460
+ case Code7InvalidValue : return errCliInvalidValue;
2461
+ case Code7FunNotAvailable : return errCliFunNotAvailable;
2462
+ case Code7NeedPassword : return errCliNeedPassword;
2463
+ case Code7InvalidPassword : return errCliInvalidPassword;
2464
+ case Code7NoPasswordToSet :
2465
+ case Code7NoPasswordToClear : return errCliNoPasswordToSetOrClear;
2466
+ default:
2467
+ return errCliFunctionRefused;
2468
+ };
2469
+ }
2470
+ //---------------------------------------------------------------------------
2471
+ int TSnap7MicroClient::DataSizeByte(int WordLength)
2472
+ {
2473
+ switch (WordLength){
2474
+ case S7WLBit : return 1; // S7 sends 1 byte per bit
2475
+ case S7WLByte : return 1;
2476
+ case S7WLChar : return 1;
2477
+ case S7WLWord : return 2;
2478
+ case S7WLDWord : return 4;
2479
+ case S7WLInt : return 2;
2480
+ case S7WLDInt : return 4;
2481
+ case S7WLReal : return 4;
2482
+ case S7WLCounter : return 2;
2483
+ case S7WLTimer : return 2;
2484
+ default : return 0;
2485
+ }
2486
+ }
2487
+ //---------------------------------------------------------------------------
2488
+ longword TSnap7MicroClient::DWordAt(void * P)
2489
+ {
2490
+ longword DW;
2491
+ DW=*(longword*)P;
2492
+ return SwapDWord(DW);
2493
+ }
2494
+ //---------------------------------------------------------------------------
2495
+ int TSnap7MicroClient::CheckBlock(int BlockType, int BlockNum, void * pBlock, int Size)
2496
+ {
2497
+ PS7CompactBlockInfo Info = PS7CompactBlockInfo(pBlock);
2498
+
2499
+ if (BlockType>=0) // if (BlockType<0 the test is skipped
2500
+ {
2501
+ if ((BlockType!=Block_OB)&&(BlockType!=Block_DB)&&(BlockType!=Block_FB)&&
2502
+ (BlockType!=Block_FC)&&(BlockType!=Block_SDB)&&(BlockType!=Block_SFC)&&
2503
+ (BlockType!=Block_SFB))
2504
+ return errCliInvalidBlockType;
2505
+ }
2506
+
2507
+ if (BlockNum>=0) // if (BlockNum<0 the test is skipped
2508
+ {
2509
+ if (BlockNum>0xFFFF)
2510
+ return errCliInvalidBlockNumber;
2511
+ };
2512
+
2513
+ if (SwapDWord(Info->LenLoadMem)!=longword(Size))
2514
+ return errCliInvalidBlockSize;
2515
+
2516
+ // Check the presence of the footer
2517
+ if (SwapWord(Info->MC7Len)+sizeof(TS7CompactBlockInfo)>=u_int(Size))
2518
+ return errCliInvalidBlockSize;
2519
+
2520
+ return 0;
2521
+ }
2522
+ //---------------------------------------------------------------------------
2523
+ int TSnap7MicroClient::SubBlockToBlock(int SBB)
2524
+ {
2525
+ switch (SBB)
2526
+ {
2527
+ case SubBlk_OB : return Block_OB;
2528
+ case SubBlk_DB : return Block_DB;
2529
+ case SubBlk_SDB : return Block_SDB;
2530
+ case SubBlk_FC : return Block_FC;
2531
+ case SubBlk_SFC : return Block_SFC;
2532
+ case SubBlk_FB : return Block_FB;
2533
+ case SubBlk_SFB : return Block_SFB;
2534
+ default : return 0;
2535
+ };
2536
+ }
2537
+ //---------------------------------------------------------------------------
2538
+ int TSnap7MicroClient::PerformOperation()
2539
+ {
2540
+ ClrError();
2541
+ int Operation=Job.Op;
2542
+ switch(Operation)
2543
+ {
2544
+ case s7opNone:
2545
+ Job.Result=errCliInvalidParams;
2546
+ break;
2547
+ case s7opReadArea:
2548
+ Job.Result=opReadArea();
2549
+ break;
2550
+ case s7opWriteArea:
2551
+ Job.Result=opWriteArea();
2552
+ break;
2553
+ case s7opReadMultiVars:
2554
+ Job.Result=opReadMultiVars();
2555
+ break;
2556
+ case s7opWriteMultiVars:
2557
+ Job.Result=opWriteMultiVars();
2558
+ break;
2559
+ case s7opDBGet:
2560
+ Job.Result=opDBGet();
2561
+ break;
2562
+ case s7opDBFill:
2563
+ Job.Result=opDBFill();
2564
+ break;
2565
+ case s7opUpload:
2566
+ Job.Result=opUpload();
2567
+ break;
2568
+ case s7opDownload:
2569
+ Job.Result=opDownload();
2570
+ break;
2571
+ case s7opDelete:
2572
+ Job.Result=opDelete();
2573
+ break;
2574
+ case s7opListBlocks:
2575
+ Job.Result=opListBlocks();
2576
+ break;
2577
+ case s7opAgBlockInfo:
2578
+ Job.Result=opAgBlockInfo();
2579
+ break;
2580
+ case s7opListBlocksOfType:
2581
+ Job.Result=opListBlocksOfType();
2582
+ break;
2583
+ case s7opReadSzlList:
2584
+ Job.Result=opReadSZLList();
2585
+ break;
2586
+ case s7opReadSZL:
2587
+ Job.Result=opReadSZL();
2588
+ break;
2589
+ case s7opGetDateTime:
2590
+ Job.Result=opGetDateTime();
2591
+ break;
2592
+ case s7opSetDateTime:
2593
+ Job.Result=opSetDateTime();
2594
+ break;
2595
+ case s7opGetOrderCode:
2596
+ Job.Result=opGetOrderCode();
2597
+ break;
2598
+ case s7opGetCpuInfo:
2599
+ Job.Result=opGetCpuInfo();
2600
+ break;
2601
+ case s7opGetCpInfo:
2602
+ Job.Result=opGetCpInfo();
2603
+ break;
2604
+ case s7opGetPlcStatus:
2605
+ Job.Result=opGetPlcStatus();
2606
+ break;
2607
+ case s7opPlcHotStart:
2608
+ Job.Result=opPlcHotStart();
2609
+ break;
2610
+ case s7opPlcColdStart:
2611
+ Job.Result=opPlcColdStart();
2612
+ break;
2613
+ case s7opCopyRamToRom:
2614
+ Job.Result=opCopyRamToRom();
2615
+ break;
2616
+ case s7opCompress:
2617
+ Job.Result=opCompress();
2618
+ break;
2619
+ case s7opPlcStop:
2620
+ Job.Result=opPlcStop();
2621
+ break;
2622
+ case s7opGetProtection:
2623
+ Job.Result=opGetProtection();
2624
+ break;
2625
+ case s7opSetPassword:
2626
+ Job.Result=opSetPassword();
2627
+ break;
2628
+ case s7opClearPassword:
2629
+ Job.Result=opClearPassword();
2630
+ break;
2631
+ }
2632
+ Job.Time =SysGetTick()-JobStart;
2633
+ Job.Pending=false;
2634
+ return SetError(Job.Result);
2635
+ }
2636
+ //---------------------------------------------------------------------------
2637
+ int TSnap7MicroClient::Disconnect()
2638
+ {
2639
+ JobStart=SysGetTick();
2640
+ PeerDisconnect();
2641
+ Job.Time=SysGetTick()-JobStart;
2642
+ Job.Pending=false;
2643
+ return 0;
2644
+ }
2645
+ //---------------------------------------------------------------------------
2646
+ int TSnap7MicroClient::Reset(bool DoReconnect)
2647
+ {
2648
+ Job.Pending=false;
2649
+ if (DoReconnect) {
2650
+ Disconnect();
2651
+ return Connect();
2652
+ }
2653
+ else
2654
+ return 0;
2655
+ }
2656
+ //---------------------------------------------------------------------------
2657
+ int TSnap7MicroClient::Connect()
2658
+ {
2659
+ int Result;
2660
+ JobStart=SysGetTick();
2661
+ Result =PeerConnect();
2662
+ Job.Time=SysGetTick()-JobStart;
2663
+ return Result;
2664
+ }
2665
+ //---------------------------------------------------------------------------
2666
+ void TSnap7MicroClient::SetConnectionType(word ConnType)
2667
+ {
2668
+ ConnectionType=ConnType;
2669
+ }
2670
+ //---------------------------------------------------------------------------
2671
+ void TSnap7MicroClient::SetConnectionParams(const char *RemAddress, word LocalTSAP, word RemoteTSAP)
2672
+ {
2673
+ SrcTSap = LocalTSAP;
2674
+ DstTSap = RemoteTSAP;
2675
+ strncpy(RemoteAddress, RemAddress, 16);
2676
+ }
2677
+ //---------------------------------------------------------------------------
2678
+ int TSnap7MicroClient::ConnectTo(const char *RemAddress, int Rack, int Slot)
2679
+ {
2680
+ word RemoteTSAP = (ConnectionType<<8)+(Rack*0x20)+Slot;
2681
+ SetConnectionParams(RemAddress, SrcTSap, RemoteTSAP);
2682
+ return Connect();
2683
+ }
2684
+ //---------------------------------------------------------------------------
2685
+ int TSnap7MicroClient::GetParam(int ParamNumber, void *pValue)
2686
+ {
2687
+ switch (ParamNumber)
2688
+ {
2689
+ case p_u16_RemotePort:
2690
+ *Puint16_t(pValue)=RemotePort;
2691
+ break;
2692
+ case p_i32_PingTimeout:
2693
+ *Pint32_t(pValue)=PingTimeout;
2694
+ break;
2695
+ case p_i32_SendTimeout:
2696
+ *Pint32_t(pValue)=SendTimeout;
2697
+ break;
2698
+ case p_i32_RecvTimeout:
2699
+ *Pint32_t(pValue)=RecvTimeout;
2700
+ break;
2701
+ case p_i32_WorkInterval:
2702
+ *Pint32_t(pValue)=WorkInterval;
2703
+ break;
2704
+ case p_u16_SrcRef:
2705
+ *Puint16_t(pValue)=SrcRef;
2706
+ break;
2707
+ case p_u16_DstRef:
2708
+ *Puint16_t(pValue)=DstRef;
2709
+ break;
2710
+ case p_u16_SrcTSap:
2711
+ *Puint16_t(pValue)=SrcTSap;
2712
+ break;
2713
+ case p_i32_PDURequest:
2714
+ *Pint32_t(pValue)=PDURequest;
2715
+ break;
2716
+ default: return errCliInvalidParamNumber;
2717
+ }
2718
+ return 0;
2719
+ }
2720
+ //---------------------------------------------------------------------------
2721
+ int TSnap7MicroClient::SetParam(int ParamNumber, void *pValue)
2722
+ {
2723
+ switch (ParamNumber)
2724
+ {
2725
+ case p_u16_RemotePort:
2726
+ if (!Connected)
2727
+ RemotePort=*Puint16_t(pValue);
2728
+ else
2729
+ return errCliCannotChangeParam;
2730
+ break;
2731
+ case p_i32_PingTimeout:
2732
+ PingTimeout=*Pint32_t(pValue);
2733
+ break;
2734
+ case p_i32_SendTimeout:
2735
+ SendTimeout=*Pint32_t(pValue);
2736
+ break;
2737
+ case p_i32_RecvTimeout:
2738
+ RecvTimeout=*Pint32_t(pValue);
2739
+ break;
2740
+ case p_i32_WorkInterval:
2741
+ WorkInterval=*Pint32_t(pValue);
2742
+ break;
2743
+ case p_u16_SrcRef:
2744
+ SrcRef=*Puint16_t(pValue);
2745
+ break;
2746
+ case p_u16_DstRef:
2747
+ DstRef=*Puint16_t(pValue);
2748
+ break;
2749
+ case p_u16_SrcTSap:
2750
+ SrcTSap=*Puint16_t(pValue);
2751
+ break;
2752
+ case p_i32_PDURequest:
2753
+ PDURequest=*Pint32_t(pValue);
2754
+ break;
2755
+ default: return errCliInvalidParamNumber;
2756
+ }
2757
+ return 0;
2758
+ }
2759
+ //---------------------------------------------------------------------------
2760
+ // Data I/O functions
2761
+ int TSnap7MicroClient::ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void * pUsrData)
2762
+ {
2763
+ if (!Job.Pending)
2764
+ {
2765
+ Job.Pending = true;
2766
+ Job.Op = s7opReadArea;
2767
+ Job.Area = Area;
2768
+ Job.Number = DBNumber;
2769
+ Job.Start = Start;
2770
+ Job.Amount = Amount;
2771
+ Job.WordLen = WordLen;
2772
+ Job.pData = pUsrData;
2773
+ JobStart = SysGetTick();
2774
+ return PerformOperation();
2775
+ }
2776
+ else
2777
+ return SetError(errCliJobPending);
2778
+ }
2779
+ //---------------------------------------------------------------------------
2780
+ int TSnap7MicroClient::WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void * pUsrData)
2781
+ {
2782
+ if (!Job.Pending)
2783
+ {
2784
+ Job.Pending = true;
2785
+ Job.Op = s7opWriteArea;
2786
+ Job.Area = Area;
2787
+ Job.Number = DBNumber;
2788
+ Job.Start = Start;
2789
+ Job.Amount = Amount;
2790
+ Job.WordLen = WordLen;
2791
+ Job.pData = pUsrData;
2792
+ JobStart = SysGetTick();
2793
+ return PerformOperation();
2794
+ }
2795
+ else
2796
+ return SetError(errCliJobPending);
2797
+ }
2798
+ //---------------------------------------------------------------------------
2799
+ int TSnap7MicroClient::ReadMultiVars(PS7DataItem Item, int ItemsCount)
2800
+ {
2801
+ if (!Job.Pending)
2802
+ {
2803
+ Job.Pending =true;
2804
+ Job.Op =s7opReadMultiVars;
2805
+ Job.Amount =ItemsCount;
2806
+ Job.pData =Item;
2807
+ JobStart =SysGetTick();
2808
+ return PerformOperation();
2809
+ }
2810
+ else
2811
+ return SetError(errCliJobPending);
2812
+ }
2813
+ //---------------------------------------------------------------------------
2814
+ int TSnap7MicroClient::WriteMultiVars(PS7DataItem Item, int ItemsCount)
2815
+ {
2816
+ if (!Job.Pending)
2817
+ {
2818
+ Job.Pending =true;
2819
+ Job.Op =s7opWriteMultiVars;
2820
+ Job.Amount =ItemsCount;
2821
+ Job.pData =Item;
2822
+ JobStart =SysGetTick();
2823
+ return PerformOperation();
2824
+ }
2825
+ else
2826
+ return SetError(errCliJobPending);
2827
+ }
2828
+ //---------------------------------------------------------------------------
2829
+ int TSnap7MicroClient::DBRead(int DBNumber, int Start, int Size, void * pUsrData)
2830
+ {
2831
+ return ReadArea(S7AreaDB, DBNumber, Start, Size, S7WLByte, pUsrData);
2832
+ }
2833
+ //---------------------------------------------------------------------------
2834
+ int TSnap7MicroClient::DBWrite(int DBNumber, int Start, int Size, void * pUsrData)
2835
+ {
2836
+ return WriteArea(S7AreaDB, DBNumber, Start, Size, S7WLByte, pUsrData);
2837
+ }
2838
+ //---------------------------------------------------------------------------
2839
+ int TSnap7MicroClient::MBRead(int Start, int Size, void * pUsrData)
2840
+ {
2841
+ return ReadArea(S7AreaMK, 0, Start, Size, S7WLByte, pUsrData);
2842
+ }
2843
+ //---------------------------------------------------------------------------
2844
+ int TSnap7MicroClient::MBWrite(int Start, int Size, void * pUsrData)
2845
+ {
2846
+ return WriteArea(S7AreaMK, 0, Start, Size, S7WLByte, pUsrData);
2847
+ }
2848
+ //---------------------------------------------------------------------------
2849
+ int TSnap7MicroClient::EBRead(int Start, int Size, void * pUsrData)
2850
+ {
2851
+ return ReadArea(S7AreaPE, 0, Start, Size, S7WLByte, pUsrData);
2852
+ }
2853
+ //---------------------------------------------------------------------------
2854
+ int TSnap7MicroClient::EBWrite(int Start, int Size, void * pUsrData)
2855
+ {
2856
+ return WriteArea(S7AreaPE, 0, Start, Size, S7WLByte, pUsrData);
2857
+ }
2858
+ //---------------------------------------------------------------------------
2859
+ int TSnap7MicroClient::ABRead(int Start, int Size, void * pUsrData)
2860
+ {
2861
+ return ReadArea(S7AreaPA, 0, Start, Size, S7WLByte, pUsrData);
2862
+ }
2863
+ //---------------------------------------------------------------------------
2864
+ int TSnap7MicroClient::ABWrite(int Start, int Size, void * pUsrData)
2865
+ {
2866
+ return WriteArea(S7AreaPA, 0, Start, Size, S7WLByte, pUsrData);
2867
+ }
2868
+ //---------------------------------------------------------------------------
2869
+ int TSnap7MicroClient::TMRead(int Start, int Amount, void * pUsrData)
2870
+ {
2871
+ return ReadArea(S7AreaTM, 0, Start, Amount, S7WLTimer, pUsrData);
2872
+ }
2873
+ //---------------------------------------------------------------------------
2874
+ int TSnap7MicroClient::TMWrite(int Start, int Amount, void * pUsrData)
2875
+ {
2876
+ return WriteArea(S7AreaTM, 0, Start, Amount, S7WLTimer, pUsrData);
2877
+ }
2878
+ //---------------------------------------------------------------------------
2879
+ int TSnap7MicroClient::CTRead(int Start, int Amount, void * pUsrData)
2880
+ {
2881
+ return ReadArea(S7AreaCT, 0, Start, Amount, S7WLCounter, pUsrData);
2882
+ }
2883
+ //---------------------------------------------------------------------------
2884
+ int TSnap7MicroClient::CTWrite(int Start, int Amount, void * pUsrData)
2885
+ {
2886
+ return WriteArea(S7AreaCT, 0, Start, Amount, S7WLCounter, pUsrData);
2887
+ }
2888
+ //---------------------------------------------------------------------------
2889
+ int TSnap7MicroClient::ListBlocks(PS7BlocksList pUsrData)
2890
+ {
2891
+ if (!Job.Pending)
2892
+ {
2893
+ Job.Pending =true;
2894
+ Job.Op =s7opListBlocks;
2895
+ Job.pData =pUsrData;
2896
+ JobStart =SysGetTick();
2897
+ return PerformOperation();
2898
+ }
2899
+ else
2900
+ return SetError(errCliJobPending);
2901
+ }
2902
+ //---------------------------------------------------------------------------
2903
+ int TSnap7MicroClient::GetAgBlockInfo(int BlockType, int BlockNum, PS7BlockInfo pUsrData)
2904
+ {
2905
+ if (!Job.Pending)
2906
+ {
2907
+ Job.Pending =true;
2908
+ Job.Op =s7opAgBlockInfo;
2909
+ Job.Area =BlockType;
2910
+ Job.Number =BlockNum;
2911
+ Job.pData =pUsrData;
2912
+ JobStart =SysGetTick();
2913
+ return PerformOperation();
2914
+ }
2915
+ else
2916
+ return SetError(errCliJobPending);
2917
+ }
2918
+ //---------------------------------------------------------------------------
2919
+ int TSnap7MicroClient::GetPgBlockInfo(void * pBlock, PS7BlockInfo pUsrData, int Size)
2920
+ {
2921
+ PS7CompactBlockInfo Info;
2922
+ PS7BlockFooter Footer;
2923
+
2924
+ int Result=CheckBlock(-1,-1,pBlock,Size);
2925
+ if (Result==0)
2926
+ {
2927
+ Info=PS7CompactBlockInfo(pBlock);
2928
+ pUsrData->BlkType =Info->SubBlkType;
2929
+ pUsrData->BlkNumber=SwapWord(Info->BlkNum);
2930
+ pUsrData->BlkLang =Info->BlkLang;
2931
+ pUsrData->BlkFlags =Info->BlkFlags;
2932
+ pUsrData->MC7Size =SwapWord(Info->MC7Len);
2933
+ pUsrData->LoadSize =SwapDWord(Info->LenLoadMem);
2934
+ pUsrData->LocalData=SwapDWord(Info->LocDataLen);
2935
+ pUsrData->SBBLength=SwapDWord(Info->SbbLen);
2936
+ pUsrData->CheckSum =0; // this info is not available
2937
+ pUsrData->Version =0; // this info is not available
2938
+ FillTime(SwapWord(Info->CodeTime_dy),pUsrData->CodeDate);
2939
+ FillTime(SwapWord(Info->IntfTime_dy),pUsrData->IntfDate);
2940
+
2941
+ Footer=PS7BlockFooter(pbyte(Info)+pUsrData->LoadSize-sizeof(TS7BlockFooter));
2942
+
2943
+ memcpy(pUsrData->Author,Footer->Author,8);
2944
+ memcpy(pUsrData->Family,Footer->Family,8);
2945
+ memcpy(pUsrData->Header,Footer->Header,8);
2946
+ };
2947
+ return SetError(Result);
2948
+ }
2949
+ //---------------------------------------------------------------------------
2950
+ int TSnap7MicroClient::ListBlocksOfType(int BlockType, TS7BlocksOfType *pUsrData, int &ItemsCount)
2951
+ {
2952
+ if (!Job.Pending)
2953
+ {
2954
+ if (ItemsCount<1)
2955
+ return SetError(errCliInvalidBlockSize);
2956
+ Job.Pending =true;
2957
+ Job.Op =s7opListBlocksOfType;
2958
+ Job.Area =BlockType;
2959
+ Job.pData =pUsrData;
2960
+ Job.pAmount =&ItemsCount;
2961
+ Job.Amount =ItemsCount;
2962
+ JobStart =SysGetTick();
2963
+ return PerformOperation();
2964
+ }
2965
+ else
2966
+ return SetError(errCliJobPending);
2967
+ }
2968
+ //---------------------------------------------------------------------------
2969
+ int TSnap7MicroClient::Upload(int BlockType, int BlockNum, void * pUsrData, int & Size)
2970
+ {
2971
+ if (!Job.Pending)
2972
+ {
2973
+ if (Size<=0)
2974
+ return SetError(errCliInvalidBlockSize);
2975
+ Job.Pending =true;
2976
+ Job.Op =s7opUpload;
2977
+ Job.Area =BlockType;
2978
+ Job.pData =pUsrData;
2979
+ Job.pAmount =&Size;
2980
+ Job.Amount =Size;
2981
+ Job.Number =BlockNum;
2982
+ Job.IParam =0; // not full upload, only data
2983
+ JobStart =SysGetTick();
2984
+ return PerformOperation();
2985
+ }
2986
+ else
2987
+ return SetError(errCliJobPending);
2988
+ }
2989
+ //---------------------------------------------------------------------------
2990
+ int TSnap7MicroClient::FullUpload(int BlockType, int BlockNum, void * pUsrData, int & Size)
2991
+ {
2992
+ if (!Job.Pending)
2993
+ {
2994
+ if (Size<=0)
2995
+ return SetError(errCliInvalidBlockSize);
2996
+ Job.Pending =true;
2997
+ Job.Op =s7opUpload;
2998
+ Job.Area =BlockType;
2999
+ Job.pData =pUsrData;
3000
+ Job.pAmount =&Size;
3001
+ Job.Amount =Size;
3002
+ Job.Number =BlockNum;
3003
+ Job.IParam =1; // header + data + footer
3004
+ JobStart =SysGetTick();
3005
+ return PerformOperation();
3006
+ }
3007
+ else
3008
+ return SetError(errCliJobPending);
3009
+ }
3010
+ //---------------------------------------------------------------------------
3011
+ int TSnap7MicroClient::Download(int BlockNum, void * pUsrData, int Size)
3012
+ {
3013
+ if (!Job.Pending)
3014
+ {
3015
+ Job.Pending =true;
3016
+ Job.Op =s7opDownload;
3017
+ memcpy(&opData, pUsrData, Size);
3018
+ Job.Number =BlockNum;
3019
+ Job.Amount =Size;
3020
+ JobStart =SysGetTick();
3021
+ return PerformOperation();
3022
+ }
3023
+ else
3024
+ return SetError(errCliJobPending);
3025
+ }
3026
+ //---------------------------------------------------------------------------
3027
+ int TSnap7MicroClient::Delete(int BlockType, int BlockNum)
3028
+ {
3029
+ if (!Job.Pending)
3030
+ {
3031
+ Job.Pending =true;
3032
+ Job.Op =s7opDelete;
3033
+ Job.Area =BlockType;
3034
+ Job.Number =BlockNum;
3035
+ JobStart =SysGetTick();
3036
+ return PerformOperation();
3037
+ }
3038
+ else
3039
+ return SetError(errCliJobPending);
3040
+ }
3041
+ //---------------------------------------------------------------------------
3042
+ int TSnap7MicroClient::DBGet(int DBNumber, void * pUsrData, int & Size)
3043
+ {
3044
+ if (!Job.Pending)
3045
+ {
3046
+ if (Size<=0)
3047
+ return SetError(errCliInvalidBlockSize);
3048
+ Job.Pending =true;
3049
+ Job.Op =s7opDBGet;
3050
+ Job.Number =DBNumber;
3051
+ Job.pData =pUsrData;
3052
+ Job.pAmount =&Size;
3053
+ Job.Amount =Size;
3054
+ JobStart =SysGetTick();
3055
+ return PerformOperation();
3056
+ }
3057
+ else
3058
+ return SetError(errCliJobPending);
3059
+ }
3060
+ //---------------------------------------------------------------------------
3061
+ int TSnap7MicroClient::DBFill(int DBNumber, int FillChar)
3062
+ {
3063
+ if (!Job.Pending)
3064
+ {
3065
+ Job.Pending =true;
3066
+ Job.Op =s7opDBFill;
3067
+ Job.Number =DBNumber;
3068
+ Job.IParam =FillChar;
3069
+ JobStart =SysGetTick();
3070
+ return PerformOperation();
3071
+ }
3072
+ else
3073
+ return SetError(errCliJobPending);
3074
+ }
3075
+ //---------------------------------------------------------------------------
3076
+ int TSnap7MicroClient::GetPlcDateTime(tm &DateTime)
3077
+ {
3078
+ if (!Job.Pending)
3079
+ {
3080
+ Job.Pending =true;
3081
+ Job.Op =s7opGetDateTime;
3082
+ Job.pData =&DateTime;
3083
+ JobStart =SysGetTick();
3084
+ return PerformOperation();
3085
+ }
3086
+ else
3087
+ return SetError(errCliJobPending);
3088
+ }
3089
+ //---------------------------------------------------------------------------
3090
+ int TSnap7MicroClient::SetPlcDateTime(tm * DateTime)
3091
+ {
3092
+ if (!Job.Pending)
3093
+ {
3094
+ Job.Pending =true;
3095
+ Job.Op =s7opSetDateTime;
3096
+ Job.pData =DateTime;
3097
+ JobStart =SysGetTick();
3098
+ return PerformOperation();
3099
+ }
3100
+ else
3101
+ return SetError(errCliJobPending);
3102
+ }
3103
+ //---------------------------------------------------------------------------
3104
+ int TSnap7MicroClient::SetPlcSystemDateTime()
3105
+ {
3106
+ time_t Now;
3107
+ time(&Now);
3108
+ struct tm * DateTime = localtime (&Now);
3109
+ return SetPlcDateTime(DateTime);
3110
+ }
3111
+ //---------------------------------------------------------------------------
3112
+ int TSnap7MicroClient::GetOrderCode(PS7OrderCode pUsrData)
3113
+ {
3114
+ if (!Job.Pending)
3115
+ {
3116
+ Job.Pending =true;
3117
+ Job.Op =s7opGetOrderCode;
3118
+ Job.pData =pUsrData;
3119
+ JobStart =SysGetTick();
3120
+ return PerformOperation();
3121
+ }
3122
+ else
3123
+ return SetError(errCliJobPending);
3124
+ }
3125
+ //---------------------------------------------------------------------------
3126
+ int TSnap7MicroClient::GetCpuInfo(PS7CpuInfo pUsrData)
3127
+ {
3128
+ if (!Job.Pending)
3129
+ {
3130
+ Job.Pending =true;
3131
+ Job.Op =s7opGetCpuInfo;
3132
+ Job.pData =pUsrData;
3133
+ JobStart =SysGetTick();
3134
+ return PerformOperation();
3135
+ }
3136
+ else
3137
+ return SetError(errCliJobPending);
3138
+ }
3139
+ //---------------------------------------------------------------------------
3140
+ int TSnap7MicroClient::GetCpInfo(PS7CpInfo pUsrData)
3141
+ {
3142
+ if (!Job.Pending)
3143
+ {
3144
+ Job.Pending =true;
3145
+ Job.Op =s7opGetCpInfo;
3146
+ Job.pData =pUsrData;
3147
+ JobStart =SysGetTick();
3148
+ return PerformOperation();
3149
+ }
3150
+ else
3151
+ return SetError(errCliJobPending);
3152
+ }
3153
+ //---------------------------------------------------------------------------
3154
+ int TSnap7MicroClient::ReadSZL(int ID, int Index, PS7SZL pUsrData, int &Size)
3155
+ {
3156
+ if (!Job.Pending)
3157
+ {
3158
+ Job.Pending =true;
3159
+ Job.Op =s7opReadSZL;
3160
+ Job.ID =ID;
3161
+ Job.Index =Index;
3162
+ Job.pData =pUsrData;
3163
+ Job.pAmount =&Size;
3164
+ Job.Amount =Size;
3165
+ Job.IParam =1; // Data has to be copied into user buffer
3166
+ JobStart =SysGetTick();
3167
+ return PerformOperation();
3168
+ }
3169
+ else
3170
+ return SetError(errCliJobPending);
3171
+ }
3172
+ //---------------------------------------------------------------------------
3173
+ int TSnap7MicroClient::ReadSZLList(PS7SZLList pUsrData, int &ItemsCount)
3174
+ {
3175
+ if (!Job.Pending)
3176
+ {
3177
+ Job.Pending =true;
3178
+ Job.Op =s7opReadSzlList;
3179
+ Job.pData =pUsrData;
3180
+ Job.pAmount =&ItemsCount;
3181
+ Job.Amount =ItemsCount;
3182
+ JobStart =SysGetTick();
3183
+ return PerformOperation();
3184
+ }
3185
+ else
3186
+ return SetError(errCliJobPending);
3187
+ }
3188
+ //---------------------------------------------------------------------------
3189
+ int TSnap7MicroClient::PlcHotStart()
3190
+ {
3191
+ if (!Job.Pending)
3192
+ {
3193
+ Job.Pending =true;
3194
+ Job.Op =s7opPlcHotStart;
3195
+ JobStart =SysGetTick();
3196
+ return PerformOperation();
3197
+ }
3198
+ else
3199
+ return SetError(errCliJobPending);
3200
+ }
3201
+ //---------------------------------------------------------------------------
3202
+ int TSnap7MicroClient::PlcColdStart()
3203
+ {
3204
+ if (!Job.Pending)
3205
+ {
3206
+ Job.Pending =true;
3207
+ Job.Op =s7opPlcColdStart;
3208
+ JobStart =SysGetTick();
3209
+ return PerformOperation();
3210
+ }
3211
+ else
3212
+ return SetError(errCliJobPending);
3213
+ }
3214
+ //---------------------------------------------------------------------------
3215
+ int TSnap7MicroClient::PlcStop()
3216
+ {
3217
+ if (!Job.Pending)
3218
+ {
3219
+ Job.Pending =true;
3220
+ Job.Op =s7opPlcStop;
3221
+ JobStart =SysGetTick();
3222
+ return PerformOperation();
3223
+ }
3224
+ else
3225
+ return SetError(errCliJobPending);
3226
+ }
3227
+ //---------------------------------------------------------------------------
3228
+ int TSnap7MicroClient::CopyRamToRom(int Timeout)
3229
+ {
3230
+ if (!Job.Pending)
3231
+ {
3232
+ if (Timeout>0)
3233
+ {
3234
+ Job.Pending =true;
3235
+ Job.Op =s7opCopyRamToRom;
3236
+ Job.IParam =Timeout;
3237
+ JobStart =SysGetTick();
3238
+ return PerformOperation();
3239
+ }
3240
+ else
3241
+ return SetError(errCliInvalidParams);
3242
+ }
3243
+ else
3244
+ return SetError(errCliJobPending);
3245
+ }
3246
+ //---------------------------------------------------------------------------
3247
+ int TSnap7MicroClient::Compress(int Timeout)
3248
+ {
3249
+ if (!Job.Pending)
3250
+ {
3251
+ if (Timeout>0)
3252
+ {
3253
+ Job.Pending =true;
3254
+ Job.Op =s7opCompress;
3255
+ Job.IParam =Timeout;
3256
+ JobStart =SysGetTick();
3257
+ return PerformOperation();
3258
+ }
3259
+ else
3260
+ return SetError(errCliInvalidParams);
3261
+ }
3262
+ else
3263
+ return SetError(errCliJobPending);
3264
+ }
3265
+ //---------------------------------------------------------------------------
3266
+ int TSnap7MicroClient::GetPlcStatus(int & Status)
3267
+ {
3268
+ if (!Job.Pending)
3269
+ {
3270
+ Job.Pending =true;
3271
+ Job.Op =s7opGetPlcStatus;
3272
+ Job.pData =&Status;
3273
+ JobStart =SysGetTick();
3274
+ return PerformOperation();
3275
+ }
3276
+ else
3277
+ return SetError(errCliJobPending);
3278
+ }
3279
+ //---------------------------------------------------------------------------
3280
+ int TSnap7MicroClient::GetProtection(PS7Protection pUsrData)
3281
+ {
3282
+ if (!Job.Pending)
3283
+ {
3284
+ Job.Pending =true;
3285
+ Job.Op =s7opGetProtection;
3286
+ Job.pData =pUsrData;
3287
+ JobStart =SysGetTick();
3288
+ return PerformOperation();
3289
+ }
3290
+ else
3291
+ return SetError(errCliJobPending);
3292
+ }
3293
+ //---------------------------------------------------------------------------
3294
+ int TSnap7MicroClient::SetSessionPassword(char *Password)
3295
+ {
3296
+ if (!Job.Pending)
3297
+ {
3298
+ size_t L = strlen(Password);
3299
+ // checks the len
3300
+ if ((L<1) || (L>8))
3301
+ return SetError(errCliInvalidParams);
3302
+ Job.Pending =true;
3303
+ // prepares an 8 char string filled with spaces
3304
+ memset(&opData,0x20,8);
3305
+ // copies
3306
+ strncpy((char*)&opData,Password,L);
3307
+ Job.Op =s7opSetPassword;
3308
+ JobStart =SysGetTick();
3309
+ return PerformOperation();
3310
+ }
3311
+ else
3312
+ return SetError(errCliJobPending);
3313
+ }
3314
+ //---------------------------------------------------------------------------
3315
+ int TSnap7MicroClient::ClearSessionPassword()
3316
+ {
3317
+ if (!Job.Pending)
3318
+ {
3319
+ Job.Pending =true;
3320
+ Job.Op =s7opClearPassword;
3321
+ JobStart =SysGetTick();
3322
+ return PerformOperation();
3323
+ }
3324
+ else
3325
+ return SetError(errCliJobPending);
3326
+ }
3327
+ //---------------------------------------------------------------------------
3328
+