ads-client 1.14.3 → 2.0.0-beta.1

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.
@@ -0,0 +1,1247 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.amsAddressToString = exports.encodeStringToPlcWstringBuffer = exports.decodePlcWstringBuffer = exports.encodeStringToPlcStringBuffer = exports.decodePlcStringBuffer = exports.trimPlcString = exports.amsNetIdStrToByteArray = exports.byteArrayToAmsNetIdStr = exports.BASE_DATA_TYPES = exports.AMS_ROUTER_STATE = exports.ADS_RCP_METHOD_PARAM_FLAGS = exports.ADS_RCP_METHOD_FLAGS = exports.ADS_DATA_TYPES = exports.ADS_DATA_TYPE_FLAGS = exports.ADS_SYMBOL_FLAGS_2 = exports.ADS_SYMBOL_FLAGS = exports.ADS_RESERVED_INDEX_GROUPS = exports.ADS_STATE = exports.ADS_TRANS_MODE = exports.ADS_ERROR = exports.ADS_STATE_FLAGS = exports.ADS_COMMAND = exports.ADS_RESERVED_PORTS = exports.AMS_HEADER_FLAG = exports.LOOPBACK_AMS_NET_ID = exports.ADS_DEFAULT_TCP_PORT = exports.ADS_INVOKE_ID_MAX_VALUE = exports.ADS_INDEX_GROUP_LENGTH = exports.ADS_INDEX_OFFSET_LENGTH = exports.AMS_NET_ID_LENGTH = exports.AMS_HEADER_LENGTH = exports.AMS_TCP_HEADER_LENGTH = void 0;
7
+ /*
8
+ Copyright (c) Jussi Isotalo <j.isotalo91@gmail.com>
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ */
28
+ const iconv_lite_1 = __importDefault(require("iconv-lite"));
29
+ /**
30
+ * AMS/TCP header length
31
+ */
32
+ exports.AMS_TCP_HEADER_LENGTH = 6;
33
+ /**
34
+ * AMS header length
35
+ */
36
+ exports.AMS_HEADER_LENGTH = 32;
37
+ /**
38
+ * AmsNetId length
39
+ */
40
+ exports.AMS_NET_ID_LENGTH = 6;
41
+ /**
42
+ * ADS index offset length
43
+ */
44
+ exports.ADS_INDEX_OFFSET_LENGTH = 4;
45
+ /**
46
+ * ADS index group length
47
+ */
48
+ exports.ADS_INDEX_GROUP_LENGTH = 4;
49
+ /**
50
+ * ADS invoke ID maximum value (32bit unsigned integer)
51
+ */
52
+ exports.ADS_INVOKE_ID_MAX_VALUE = 4294967295;
53
+ /**
54
+ * Default ADS server TCP port for incoming connections
55
+ */
56
+ exports.ADS_DEFAULT_TCP_PORT = 48898;
57
+ /**
58
+ * Loopback (localhost) AmsNetId
59
+ */
60
+ exports.LOOPBACK_AMS_NET_ID = '127.0.0.1.1.1';
61
+ /**
62
+ * AMS header flag (AMS command)
63
+ */
64
+ exports.AMS_HEADER_FLAG = {
65
+ /** 0x0000 - Used for ADS commands */
66
+ AMS_TCP_PORT_AMS_CMD: 0,
67
+ /** 0x0001 - Port close command*/
68
+ AMS_TCP_PORT_CLOSE: 1,
69
+ /** 0x1000 - Port connect command */
70
+ AMS_TCP_PORT_CONNECT: 4096,
71
+ /** 0x1001 - Router notification */
72
+ AMS_TCP_PORT_ROUTER_NOTE: 4097,
73
+ /** 0x1002 - Requests local AmsNetId */
74
+ GET_LOCAL_NETID: 4098,
75
+ /** Returns the corresponding key as string by given value (number) */
76
+ toString: function (value) {
77
+ const val = Object.keys(this).find((key) => this[key] == value);
78
+ return (val !== undefined ? val : 'UNKNOWN');
79
+ }
80
+ };
81
+ /**
82
+ * Reserved/known ADS ports
83
+ *
84
+ * Source: TwinCAT.Ads.dll By Beckhoff
85
+ */
86
+ exports.ADS_RESERVED_PORTS = {
87
+ None: 0,
88
+ /** AMS Router (Port 1) */
89
+ Router: 1,
90
+ /** AMS Debugger (Port 2) */
91
+ Debugger: 2,
92
+ /** The TCom Server. Dpc or passive level. */
93
+ R0_TComServer: 10, // 0x0000000A
94
+ /** TCom Server Task. RT context. */
95
+ R0_TComServerTask: 11, // 0x0000000B
96
+ /** TCom Serve Task. Passive level. */
97
+ R0_TComServer_PL: 12, // 0x0000000C
98
+ /** TwinCAT Debugger */
99
+ R0_TcDebugger: 20, // 0x00000014
100
+ /** TwinCAT Debugger Task */
101
+ R0_TcDebuggerTask: 21, // 0x00000015
102
+ /** The License Server (Port 30) */
103
+ R0_LicenseServer: 30, // 0x0000001E
104
+ /** Logger (Port 100) */
105
+ Logger: 100, // 0x00000064
106
+ /** Event Logger (Port 110) */
107
+ EventLog: 110, // 0x0000006E
108
+ /** application for coupler (EK), gateway (EL), etc. */
109
+ DeviceApplication: 120, // 0x00000078
110
+ /** Event Logger UM */
111
+ EventLog_UM: 130, // 0x00000082
112
+ /** Event Logger RT */
113
+ EventLog_RT: 131, // 0x00000083
114
+ /** Event Logger Publisher */
115
+ EventLogPublisher: 132, // 0x00000084
116
+ /** R0 Realtime (Port 200) */
117
+ R0_Realtime: 200, // 0x000000C8
118
+ /** R0 Trace (Port 290) */
119
+ R0_Trace: 290, // 0x00000122
120
+ /** R0 IO (Port 300) */
121
+ R0_IO: 300, // 0x0000012C
122
+ /** NC (R0) (Port 500) */
123
+ R0_NC: 500, // 0x000001F4
124
+ /** R0 Satzausführung (Port 501) */
125
+ R0_NCSAF: 501, // 0x000001F5
126
+ /** R0 Satzvorbereitung (Port 511) */
127
+ R0_NCSVB: 511, // 0x000001FF
128
+ /** Preconfigured Nc2-Nc3-Instance */
129
+ R0_NCINSTANCE: 520, // 0x00000208
130
+ /** R0 ISG (Port 550) */
131
+ R0_ISG: 550, // 0x00000226
132
+ /** R0 CNC (Port 600) */
133
+ R0_CNC: 600, // 0x00000258
134
+ /** R0 Line (Port 700) */
135
+ R0_LINE: 700, // 0x000002BC
136
+ /** R0 PLC (Port 800) */
137
+ R0_PLC: 800, // 0x00000320
138
+ /** Tc2 PLC RuntimeSystem 1 (Port 801) */
139
+ Tc2_Plc1: 801, // 0x00000321
140
+ /** Tc2 PLC RuntimeSystem 2 (Port 811) */
141
+ Tc2_Plc2: 811, // 0x0000032B
142
+ /** Tc2 PLC RuntimeSystem 3 (Port 821) */
143
+ Tc2_Plc3: 821, // 0x00000335
144
+ /** Tc2 PLC RuntimeSystem 4 (Port 831) */
145
+ Tc2_Plc4: 831, // 0x0000033F
146
+ /** R0 RTS (Port 850) */
147
+ R0_RTS: 850, // 0x00000352
148
+ /** Tc3 PLC RuntimeSystem 1 (Port 851) */
149
+ Tc3_Plc1: 851,
150
+ /** Tc3 PLC RuntimeSystem 2 (Port 852) */
151
+ Tc3_Plc2: 852,
152
+ /** Tc3 PLC RuntimeSystem 3 (Port 853) */
153
+ Tc3_Plc3: 853,
154
+ /** Tc3 PLC RuntimeSystem 4 (Port 854) */
155
+ Tc3_Plc4: 854,
156
+ /** Tc3 PLC RuntimeSystem 5 (Port 855) */
157
+ Tc3_Plc5: 855,
158
+ /** Camshaft Controller (R0) (Port 900) */
159
+ CamshaftController: 900, // 0x00000384
160
+ /** R0 CAM Tool (Port 950) */
161
+ R0_CAMTOOL: 950, // 0x000003B6
162
+ /** R0 User (Port 2000) */
163
+ R0_USER: 2000, // 0x000007D0
164
+ /** (Port 10000) */
165
+ R3_CTRLPROG: 10000, // 0x00002710
166
+ /** System Service (AMSPORT_R3_SYSSERV, 10000) */
167
+ SystemService: 10000, // 0x00002710
168
+ /** (Port 10001) */
169
+ R3_SYSCTRL: 10001, // 0x00002711
170
+ /** Port 10100 */
171
+ R3_SYSSAMPLER: 10100, // 0x00002774
172
+ /** Port 10200 */
173
+ R3_TCPRAWCONN: 10200, // 0x000027D8
174
+ /** Port 10201 */
175
+ R3_TCPIPSERVER: 10201, // 0x000027D9
176
+ /** Port 10300 */
177
+ R3_SYSMANAGER: 10300, // 0x0000283C
178
+ /** Port 10400 */
179
+ R3_SMSSERVER: 10400, // 0x000028A0
180
+ /** Port 10500 */
181
+ R3_MODBUSSERVER: 10500, // 0x00002904
182
+ /** Port 10502 */
183
+ R3_AMSLOGGER: 10502, // 0x00002906
184
+ /** Port 10600 */
185
+ R3_XMLDATASERVER: 10600, // 0x00002968
186
+ /** Port 10700 */
187
+ R3_AUTOCONFIG: 10700, // 0x000029CC
188
+ /** Port 10800 */
189
+ R3_PLCCONTROL: 10800, // 0x00002A30
190
+ /** Port 10900 */
191
+ R3_FTPCLIENT: 10900, // 0x00002A94
192
+ /** Port 11000 */
193
+ R3_NCCTRL: 11000, // 0x00002AF8
194
+ /** Port 11500 */
195
+ R3_NCINTERPRETER: 11500, // 0x00002CEC
196
+ /** Port 11600 */
197
+ R3_GSTINTERPRETER: 11600, // 0x00002D50
198
+ /** Port 12000 */
199
+ R3_STRECKECTRL: 12000, // 0x00002EE0
200
+ /** Port 13000 */
201
+ R3_CAMCTRL: 13000, // 0x000032C8
202
+ /** Port 14000 */
203
+ R3_SCOPE: 14000, // 0x000036B0
204
+ /** Port 14100 */
205
+ R3_CONDITIONMON: 14100, // 0x00003714
206
+ /** Port 15000 */
207
+ R3_SINECH1: 15000, // 0x00003A98
208
+ /** Port 16000 */
209
+ R3_CONTROLNET: 16000, // 0x00003E80
210
+ /** Port 17000 */
211
+ R3_OPCSERVER: 17000, // 0x00004268
212
+ /** Port 17500 */
213
+ R3_OPCCLIENT: 17500, // 0x0000445C
214
+ /** Port 18000 */
215
+ R3_MAILSERVER: 18000, // 0x00004650
216
+ /** Port 19000 */
217
+ R3_EL60XX: 19000, // 0x00004A38
218
+ /** Port 19100 */
219
+ R3_MANAGEMENT: 19100, // 0x00004A9C
220
+ /** Port 19200 */
221
+ R3_MIELEHOME: 19200, // 0x00004B00
222
+ /** Port 19300 */
223
+ R3_CPLINK3: 19300, // 0x00004B64
224
+ /** Port 19500 */
225
+ R3_VNSERVICE: 19500, // 0x00004C2C
226
+ /** Multiuser (Port 19600) */
227
+ R3_MULTIUSER: 19600, // 0x00004C90
228
+ /** Default (AMS router assigns) */
229
+ USEDEFAULT: 65535, // 0x0000FFFF
230
+ };
231
+ /**
232
+ * ADS command
233
+ *
234
+ * Source: TwinCAT.Ads.dll By Beckhoff
235
+ */
236
+ exports.ADS_COMMAND = {
237
+ /** Invalid */
238
+ Invalid: 0,
239
+ /** None / Uninitialized */
240
+ None: 0,
241
+ /** ReadDeviceInfo command */
242
+ ReadDeviceInfo: 1,
243
+ /** Read Command */
244
+ Read: 2,
245
+ /** Write Command */
246
+ Write: 3,
247
+ /** ReadState Command */
248
+ ReadState: 4,
249
+ /** WriteControl Command */
250
+ WriteControl: 5,
251
+ /** AddNotification Command */
252
+ AddNotification: 6,
253
+ /** DeleteNotification Command */
254
+ DeleteNotification: 7,
255
+ /** Notification event. */
256
+ Notification: 8,
257
+ /** ReadWrite Command */
258
+ ReadWrite: 9,
259
+ /** Returns the corresponding key as string by given value (number) */
260
+ toString: function (value) {
261
+ const val = Object.keys(this).find((key) => this[key] == value);
262
+ return (val !== undefined ? val : 'UNKNOWN');
263
+ }
264
+ };
265
+ /**
266
+ * ADS state flags
267
+ *
268
+ * Source: TwinCAT.Ads.dll By Beckhoff
269
+ */
270
+ exports.ADS_STATE_FLAGS = {
271
+ //The response (AMSCMDSF_RESPONSE)
272
+ Response: 1,
273
+ /** (AMSCMDSF_NORETURN) */
274
+ NoReturn: 2,
275
+ /** AdsCommand */
276
+ AdsCommand: 4,
277
+ /** Internal generated cmds (AMSCMDSF_SYSCMD) */
278
+ SysCommand: 8,
279
+ /** High Priority (R0 to R0 checked at task begin, AMSCMDSF_HIGHPRIO) */
280
+ HighPriority: 16, // 0x0010
281
+ /** (cbData um 8 Byte vergrößert, AMSCMDSF_TIMESTAMPADDED) */
282
+ TimeStampAdded: 32, // 0x0020
283
+ /** (UDP instead of TCP, AMSCMDSF_UDP) */
284
+ Udp: 64, // 0x0040
285
+ /** (command during init phase of TwinCAT, AMSCMDSF_INITCMD) */
286
+ InitCmd: 128, // 0x0080
287
+ /** (AMSCMDSF_BROADCAST) */
288
+ Broadcast: 32768, // 0x8000
289
+ /** Returns the flags as comma separated list by given flag value (number) */
290
+ toString: function (value) {
291
+ const flags = [];
292
+ for (const key of Object.keys(this)) {
293
+ const typedKey = key;
294
+ if (typeof this[typedKey] !== 'number')
295
+ continue;
296
+ if ((value & this[typedKey]) === this[typedKey])
297
+ flags.push(key);
298
+ }
299
+ //Specials: For helping debugging
300
+ if (!flags.includes('Udp'))
301
+ flags.push('Tcp');
302
+ if (!flags.includes('Response'))
303
+ flags.push('Request');
304
+ return flags.join(', ');
305
+ }
306
+ };
307
+ /**
308
+ * ADS error code
309
+ *
310
+ * Source: Beckhoff InfoSys
311
+ */
312
+ exports.ADS_ERROR = {
313
+ 0: 'No error',
314
+ 1: 'Internal error',
315
+ 2: 'No Rtime',
316
+ 3: 'Allocation locked memory error',
317
+ 4: 'Insert mailbox error',
318
+ 5: 'Wrong receive HMSG',
319
+ 6: 'Target port not found',
320
+ 7: 'Target machine not found',
321
+ 8: 'Unknown command ID',
322
+ 9: 'Bad task ID',
323
+ 10: 'No IO',
324
+ 11: 'Unknown ADS command',
325
+ 12: 'Win 32 error',
326
+ 13: 'Port not connected',
327
+ 14: 'Invalid ADS length',
328
+ 15: 'Invalid AMS Net ID',
329
+ 16: 'Low Installation level',
330
+ 17: 'No debug available',
331
+ 18: 'Port disabled',
332
+ 19: 'Port already connected',
333
+ 20: 'ADS Sync Win32 error',
334
+ 21: 'ADS Sync Timeout',
335
+ 22: 'ADS Sync AMS error',
336
+ 23: 'ADS Sync no index map',
337
+ 24: 'Invalid ADS port',
338
+ 25: 'No memory',
339
+ 26: 'TCP send error',
340
+ 27: 'Host unreachable',
341
+ 28: 'Invalid AMS fragment',
342
+ 1280: 'No locked memory can be allocated',
343
+ 1281: 'The size of the router memory could not be changed',
344
+ 1282: 'The mailbox has reached the maximum number of possible messages. The current sent message was rejected',
345
+ 1283: 'The mailbox has reached the maximum number of possible messages.',
346
+ 1284: 'Unknown port type',
347
+ 1285: 'Router is not initialized',
348
+ 1286: 'The desired port number is already assigned',
349
+ 1287: 'Port not registered',
350
+ 1288: 'The maximum number of Ports reached',
351
+ 1289: 'Invalid port',
352
+ 1290: 'TwinCAT Router not active',
353
+ 1792: 'General device error',
354
+ 1793: 'Service is not supported by server',
355
+ 1794: 'Invalid index group',
356
+ 1795: 'Invalid index offset',
357
+ 1796: 'Reading/writing not permitted',
358
+ 1797: 'Parameter size not correct',
359
+ 1798: 'Invalid parameter value(s)',
360
+ 1799: 'Device is not in a ready state',
361
+ 1800: 'Device is busy',
362
+ 1801: 'Invalid context (must be in Windows)',
363
+ 1802: 'Out of memory',
364
+ 1803: 'Invalid parameter value(s)',
365
+ 1804: 'Not found (files, ...)',
366
+ 1805: 'Syntax error in command or file',
367
+ 1806: 'Objects do not match',
368
+ 1807: 'Object already exists',
369
+ 1808: 'Symbol not found',
370
+ 1809: 'Symbol version invalid',
371
+ 1810: 'Server is in invalid state',
372
+ 1811: 'AdsTransMode not supported',
373
+ 1812: 'Notification handle is invalid',
374
+ 1813: 'Notification client not registered',
375
+ 1814: 'No more notification handles',
376
+ 1815: 'Size for watch too big',
377
+ 1816: 'Device not initialized',
378
+ 1817: 'Device has a timeout',
379
+ 1818: 'Query interface failed',
380
+ 1819: 'Wrong interface required',
381
+ 1820: 'Class ID is invalid',
382
+ 1821: 'Object ID is invalid',
383
+ 1822: 'Request is pending',
384
+ 1823: 'Request is aborted',
385
+ 1824: 'Signal warning',
386
+ 1825: 'Invalid array index',
387
+ 1826: 'Symbol not active',
388
+ 1827: 'Access denied',
389
+ 1828: 'Missing license',
390
+ 1829: 'License expired',
391
+ 1830: 'License exceeded',
392
+ 1831: 'License invalid',
393
+ 1832: 'License invalid system id',
394
+ 1833: 'License not time limited',
395
+ 1834: 'License issue time in the future',
396
+ 1835: 'License time period to long',
397
+ 1836: 'Exception occured during system start',
398
+ 1837: 'License file read twice',
399
+ 1838: 'Invalid signature',
400
+ 1839: 'Public key certificate',
401
+ 1856: 'Error class <client error>',
402
+ 1857: 'Invalid parameter at service',
403
+ 1858: 'Polling list is empty',
404
+ 1859: 'Var connection already in use',
405
+ 1860: 'Invoke ID in use',
406
+ 1861: 'Timeout elapsed',
407
+ 1862: 'Error in win32 subsystem',
408
+ 1863: 'Invalid client timeout value',
409
+ 1864: 'Ads-port not opened',
410
+ 1872: 'Internal error in ads sync',
411
+ 1873: 'Hash table overflow',
412
+ 1874: 'Key not found in hash',
413
+ 1875: 'No more symbols in cache',
414
+ 1876: 'Invalid response received',
415
+ 1877: 'Sync port is locked',
416
+ 4096: 'Internal fatal error in the TwinCAT real-time system',
417
+ 4097: 'Timer value not vaild',
418
+ 4098: 'Task pointer has the invalid value ZERO',
419
+ 4099: 'Task stack pointer has the invalid value ZERO',
420
+ 4100: 'The demand task priority is already assigned',
421
+ 4101: 'No more free TCB (Task Control Block) available. Maximum number of TCBs is 64',
422
+ 4102: 'No more free semaphores available. Maximum number of semaphores is 64',
423
+ 4103: 'No more free queue available. Maximum number of queue is 64',
424
+ 4109: 'An external synchronization interrupt is already applied',
425
+ 4110: 'No external synchronization interrupt applied',
426
+ 4111: 'The apply of the external synchronization interrupt failed',
427
+ 4112: 'Call of a service function in the wrong context',
428
+ 4119: 'Intel VT-x extension is not supported',
429
+ 4120: 'Intel VT-x extension is not enabled in system BIOS',
430
+ 4121: 'Missing function in Intel VT-x extension',
431
+ 4122: 'Enabling Intel VT-x fails',
432
+ };
433
+ /**
434
+ * ADS notification transmission mode
435
+ *
436
+ * Source: TwinCAT.Ads.dll By Beckhoff
437
+ */
438
+ exports.ADS_TRANS_MODE = {
439
+ None: 0,
440
+ ClientCycle: 1,
441
+ ClientOnChange: 2,
442
+ Cyclic: 3,
443
+ OnChange: 4,
444
+ CyclicInContext: 5,
445
+ OnChangeInContext: 6,
446
+ /** Returns the corresponding key as string by given value (number) */
447
+ toString: function (value) {
448
+ const val = Object.keys(this).find((key) => this[key] == value);
449
+ return (val !== undefined ? val : 'UNKNOWN');
450
+ }
451
+ };
452
+ /**
453
+ * ADS state
454
+ *
455
+ * Source: TwinCAT.Ads.dll By Beckhoff
456
+ */
457
+ exports.ADS_STATE = {
458
+ Invalid: 0,
459
+ Idle: 1,
460
+ Reset: 2,
461
+ Initialize: 3,
462
+ Start: 4,
463
+ Run: 5,
464
+ Stop: 6,
465
+ SaveConfig: 7,
466
+ LoadConfig: 8,
467
+ PowerFailure: 9,
468
+ PowerGood: 10,
469
+ Error: 11,
470
+ Shutdown: 12,
471
+ Suspend: 13,
472
+ Resume: 14,
473
+ Config: 15,
474
+ Reconfig: 16,
475
+ Stopping: 17,
476
+ Incompatible: 18,
477
+ Exception: 19,
478
+ /** Returns the corresponding key as string by given value (number) */
479
+ toString: function (value) {
480
+ const val = Object.keys(this).find((key) => this[key] == value);
481
+ return (val !== undefined ? val : 'UNKNOWN');
482
+ }
483
+ };
484
+ /**
485
+ * Reserved ADS index groups
486
+ *
487
+ * Source: TwinCAT.Ads.dll By Beckhoff
488
+ */
489
+ exports.ADS_RESERVED_INDEX_GROUPS = {
490
+ /** PlcRWIB (0x4000, 16384) */
491
+ PlcRWIB: 16384, // 0x00004000
492
+ /** PlcRWOB (0x4010, 16400) */
493
+ PlcRWOB: 16400, // 0x00004010
494
+ /** PlcRWMB (0x4020, 16416) */
495
+ PlcRWMB: 16416, // 0x00004020
496
+ /** PlcRWRB (0x4030, 16432) */
497
+ PlcRWRB: 16432, // 0x00004030
498
+ /** PlcRWDB (0x4040,16448) */
499
+ PlcRWDB: 16448, // 0x00004040
500
+ /** SymbolTable (0xF000, 61440) */
501
+ SymbolTable: 61440, // 0x0000F000
502
+ /** SymbolName (0xF001, 61441) */
503
+ SymbolName: 61441, // 0x0000F001
504
+ /** SymbolValue (0xF002, 61442) */
505
+ SymbolValue: 61442, // 0x0000F002
506
+ /** SymbolHandleByName (0xF003, 61443) */
507
+ SymbolHandleByName: 61443, // 0x0000F003
508
+ /** SymbolValueByName (0xF004, 61444) */
509
+ SymbolValueByName: 61444, // 0x0000F004
510
+ /** SymbolValueByHandle (0xF005, 61445) */
511
+ SymbolValueByHandle: 61445, // 0x0000F005
512
+ /** SymbolReleaseHandle (0xF006, 61446) */
513
+ SymbolReleaseHandle: 61446, // 0x0000F006
514
+ /** SymbolInfoByName (0xF007, 61447) */
515
+ SymbolInfoByName: 61447, // 0x0000F007
516
+ /** SymbolVersion (0xF008, 61448) */
517
+ SymbolVersion: 61448, // 0x0000F008
518
+ /** SymbolInfoByNameEx (0xF009, 61449) */
519
+ SymbolInfoByNameEx: 61449, // 0x0000F009
520
+ /** SymbolDownload (F00A, 61450) */
521
+ SymbolDownload: 61450, // 0x0000F00A
522
+ /** SymbolUpload (F00B, 61451) */
523
+ SymbolUpload: 61451, // 0x0000F00B
524
+ /** SymbolUploadInfo (0xF00C, 61452) */
525
+ SymbolUploadInfo: 61452, // 0x0000F00C
526
+ /** SymbolDownload2 */
527
+ SymbolDownload2: 0xF00D, //Added, not from .dll
528
+ /** SymbolDataTypeUpload */
529
+ SymbolDataTypeUpload: 0xF00E, //Added, not from .dll
530
+ /** SymbolUploadInfo2 */
531
+ SymbolUploadInfo2: 0xF00F, //Added, not from .dll - 24 bytes of info, uploadinfo3 would contain 64 bytes
532
+ /** Notification of named handle (0xF010, 61456) */
533
+ SymbolNote: 61456, // 0x0000F010
534
+ /** DataDataTypeInfoByNameEx */
535
+ DataDataTypeInfoByNameEx: 0xF011, //Added, not from .dll
536
+ /** read/write input byte(s) (0xF020, 61472) */
537
+ IOImageRWIB: 61472, // 0x0000F020
538
+ /** read/write input bit (0xF021, 61473) */
539
+ IOImageRWIX: 61473, // 0x0000F021
540
+ /** read/write output byte(s) (0xF030, 61488) */
541
+ IOImageRWOB: 61488, // 0x0000F030
542
+ /** read/write output bit (0xF031, 61489) */
543
+ IOImageRWOX: 61489, // 0x0000F031
544
+ /** write inputs to null (0xF040, 61504) */
545
+ IOImageClearI: 61504, // 0x0000F040
546
+ /** write outputs to null (0xF050, 61520) */
547
+ IOImageClearO: 61520, // 0x0000F050
548
+ /** ADS Sum Read Command (ADSIGRP_SUMUP_READ, 0xF080, 61568) */
549
+ SumCommandRead: 61568, // 0x0000F080
550
+ /** ADS Sum Write Command (ADSIGRP_SUMUP_WRITE, 0xF081, 61569) */
551
+ SumCommandWrite: 61569, // 0x0000F081
552
+ /** ADS sum Read/Write command (ADSIGRP_SUMUP_READWRITE, 0xF082, 61570) */
553
+ SumCommandReadWrite: 61570, // 0x0000F082
554
+ /** ADS sum ReadEx command (ADSIGRP_SUMUP_READEX, 0xF083, 61571) */
555
+ /** AdsRW IOffs list size */
556
+ /** W: {list of IGrp, IOffs, Length} */
557
+ /** R: {list of results, Length} followed by {list of data (expepted lengths)} */
558
+ SumCommandReadEx: 61571, // 0x0000F083
559
+ /** ADS sum ReadEx2 command (ADSIGRP_SUMUP_READEX2, 0xF084, 61572) */
560
+ /** AdsRW IOffs list size */
561
+ /** W: {list of IGrp, IOffs, Length} */
562
+ /** R: {list of results, Length} followed by {list of data (returned lengths)} */
563
+ SumCommandReadEx2: 61572, // 0x0000F084
564
+ /** ADS sum AddDevNote command (ADSIGRP_SUMUP_ADDDEVNOTE, 0xF085, 61573) */
565
+ /** AdsRW IOffs list size */
566
+ /** W: {list of IGrp, IOffs, Attrib} */
567
+ /** R: {list of results, handles} */
568
+ SumCommandAddDevNote: 61573, // 0x0000F085
569
+ /** ADS sum DelDevNot command (ADSIGRP_SUMUP_DELDEVNOTE, 0xF086, 61574) */
570
+ /** AdsRW IOffs list size */
571
+ /** W: {list of handles} */
572
+ /** R: {list of results} */
573
+ SumCommandDelDevNote: 61574, // 0x0000F086
574
+ /** DeviceData (0xF100,61696) */
575
+ DeviceData: 61696, // 0x0000F100
576
+ /** Returns the corresponding key as string by given value (number) */
577
+ toString: function (value) {
578
+ const val = Object.keys(this).find((key) => this[key] == value);
579
+ return (val !== undefined ? val : 'UNKNOWN');
580
+ }
581
+ };
582
+ /**
583
+ * ADS symbol flags
584
+ *
585
+ * Source: TwinCAT.Ads.dll By Beckhoff
586
+ */
587
+ exports.ADS_SYMBOL_FLAGS = {
588
+ //None
589
+ None: 0,
590
+ /** ADSSYMBOLFLAG_PERSISTENT */
591
+ Persistent: 1,
592
+ /** ADSSYMBOLFLAG_BITVALUE */
593
+ BitValue: 2,
594
+ /** ADSSYMBOLFLAG_REFERENCETO */
595
+ ReferenceTo: 4,
596
+ /** ADSSYMBOLFLAG_TYPEGUID */
597
+ TypeGuid: 8,
598
+ /** ADSSYMBOLFLAG_TCCOMIFACEPTR */
599
+ TComInterfacePtr: 16, // 0x0010
600
+ /** ADSSYMBOLFLAG_READONLY */
601
+ ReadOnly: 32, // 0x0020
602
+ /** ADSSYMBOLFLAG_ITFMETHODACCESS */
603
+ ItfMethodAccess: 64, // 0x0040
604
+ /** ADSSYMBOLFLAG_METHODDEREF */
605
+ MethodDeref: 128, // 0x0080
606
+ /** ADSSYMBOLFLAG_CONTEXTMASK (4 Bit) */
607
+ ContextMask: 3840, // 0x0F00
608
+ /** ADSSYMBOLFLAG_ATTRIBUTES */
609
+ Attributes: 4096, // 0x1000
610
+ /** Symbol is static (ADSSYMBOLFLAG_STATIC,0x2000) */
611
+ Static: 8192, // 0x2000
612
+ /** Persistent data will not restored after reset (cold, ADSSYMBOLFLAG_INITONRESET 0x4000) */
613
+ InitOnReset: 16384, // 0x4000
614
+ /** Extended Flags in symbol (ADSSYMBOLFLAG_EXTENDEDFLAGS,0x8000) */
615
+ ExtendedFlags: 32768, // 0x8000
616
+ /** Return given flag value as string array */
617
+ toStringArray: function (flags) {
618
+ const flagsArr = [];
619
+ for (const key of Object.keys(this)) {
620
+ const typedKey = key;
621
+ if (typeof this[typedKey] !== 'number')
622
+ continue;
623
+ //Check if flag is available
624
+ if ((flags & this[typedKey]) === this[typedKey]) {
625
+ if (flags === 0 || this[typedKey] !== 0)
626
+ flagsArr.push(key);
627
+ }
628
+ }
629
+ return flagsArr;
630
+ },
631
+ toString: function (flag) {
632
+ const index = Object.values(this).findIndex(val => val === flag);
633
+ return index >= 0 ? Object.keys(this)[index] : "unknown";
634
+ }
635
+ };
636
+ /**
637
+ * ADS symbol extended flags
638
+ *
639
+ * Source: TwinCAT.Ads.dll By Beckhoff
640
+ */
641
+ exports.ADS_SYMBOL_FLAGS_2 = {
642
+ //None
643
+ None: 0,
644
+ /** PLC pointer type (ADSSYMBOLFLAG2_PLCPOINTERTYPE, 0x00000001) */
645
+ PlcPointerType: 1,
646
+ /** Ignore symbol while equalizing redundancy projects (ADSSYMBOLFLAG2_RDIGNORE, 0x00000002) */
647
+ RedundancyIgnore: 2,
648
+ /** Contains refactoring information (ADSSYMBOLFLAG2_REFACTORINFO, 0x00000004) */
649
+ RefactorInfo: 4,
650
+ /** Online change PTR reference type (ADSSYMBOLFLAG2_OCPTRREFTYPE, 0x00000008) */
651
+ OnlineChangePtrRefType: 8,
652
+ /** Symbol is a Variant Type (ADSSYMBOLFLAG2_VARIANT, 0x00000010) */
653
+ VariantType: 16, // 0x0010
654
+ /** Return given flag value as string array */
655
+ toStringArray: function (flags) {
656
+ const flagsArr = [];
657
+ for (const key of Object.keys(this)) {
658
+ const typedKey = key;
659
+ if (typeof this[typedKey] !== 'number')
660
+ continue;
661
+ //Check if flag is available
662
+ if ((flags & this[typedKey]) === this[typedKey]) {
663
+ if (flags === 0 || this[typedKey] !== 0)
664
+ flagsArr.push(key);
665
+ }
666
+ }
667
+ return flagsArr;
668
+ },
669
+ toString: function (flag) {
670
+ const index = Object.values(this).findIndex(val => val === flag);
671
+ return index >= 0 ? Object.keys(this)[index] : "unknown";
672
+ }
673
+ };
674
+ /**
675
+ * ADS data type flags
676
+ *
677
+ * Source: TwinCAT.Ads.dll By Beckhoff
678
+ */
679
+ exports.ADS_DATA_TYPE_FLAGS = {
680
+ /** ADSDATATYPEFLAG_DATATYPE */
681
+ DataType: 1,
682
+ /** ADSDATATYPEFLAG_DATAITEM */
683
+ DataItem: 2,
684
+ /** ADSDATATYPEFLAG_REFERENCETO */
685
+ ReferenceTo: 4,
686
+ /** ADSDATATYPEFLAG_METHODDEREF */
687
+ MethodDeref: 8,
688
+ /** ADSDATATYPEFLAG_OVERSAMPLE */
689
+ Oversample: 16, // 0x00000010
690
+ /** ADSDATATYPEFLAG_BITVALUES */
691
+ BitValues: 32, // 0x00000020
692
+ /** ADSDATATYPEFLAG_PROPITEM */
693
+ PropItem: 64, // 0x00000040
694
+ /** ADSDATATYPEFLAG_TYPEGUID */
695
+ TypeGuid: 128, // 0x00000080
696
+ /** ADSDATATYPEFLAG_PERSISTENT */
697
+ Persistent: 256, // 0x00000100
698
+ /** ADSDATATYPEFLAG_COPYMASK */
699
+ CopyMask: 512, // 0x00000200
700
+ /** ADSDATATYPEFLAG_TCCOMIFACEPTR */
701
+ TComInterfacePtr: 1024, // 0x00000400
702
+ /** ADSDATATYPEFLAG_METHODINFOS */
703
+ MethodInfos: 2048, // 0x00000800
704
+ /** ADSDATATYPEFLAG_ATTRIBUTES */
705
+ Attributes: 4096, // 0x00001000
706
+ /** ADSDATATYPEFLAG_ENUMINFOS */
707
+ EnumInfos: 8192, // 0x00002000
708
+ /** this flag is set if the datatype is aligned (ADSDATATYPEFLAG_ALIGNED) */
709
+ Aligned: 65536, // 0x00010000
710
+ /** data item is static - do not use offs (ADSDATATYPEFLAG_STATIC) */
711
+ Static: 131072, // 0x00020000
712
+ /** Has Software Protection Levels for DataTypes. */
713
+ SoftwareProtectionLevels: 262144, // 0x00040000
714
+ /** do not restore persistent data (ADSDATATYPEFLAG_IGNOREPERSIST) */
715
+ IgnorePersist: 524288, // 0x00080000
716
+ /** Any size array (ADSDATATYPEFLAG_ANYSIZEARRAY) */
717
+ AnySizeArray: 1048576, // 0x00100000
718
+ /** data type used for persistent variables -&gt; should be saved with persistent data (ADSDATATYPEFLAG_PERSIST_DT,0x00200000) */
719
+ PersistantDatatype: 2097152, // 0x00200000
720
+ /** Persistent data will not restored after reset (cold) (ADSDATATYPEFLAG_INITONRESET,0x00400000) */
721
+ InitOnReset: 4194304, // 0x00400000
722
+ /** Indicates PLC Pointer types (ADSDATATYPEFLAG_PLCPOINTERTYPE, 0x00800000) */
723
+ PlcPointerType: 8388608, // 0x00800000
724
+ /** Refactoring information is added (ADSDATATYPEFLAG_REFACTORINFO, 0x01000000) */
725
+ RefactorInfo: 16777216, // 0x01000000
726
+ /** SubItems are hidden / will not be evaluated (ADSDATATYPEFLAG_HIDESUBITEMS, 0x02000000) */
727
+ HideSubItems: 33554432, // 0x02000000
728
+ /** The Type description is incomplete (ADSDATATYPEFLAG_INCOMPLETE, 0x04000000) */
729
+ Incomplete: 67108864, // 0x04000000
730
+ /** Type contains online change PTR reference (ADSDATATYPEFLAG_OCPTRREFTYPE, 0x08000000) */
731
+ ContainsOnlineChangePtrRef: 134217728, // 0x08000000
732
+ /** Deref Type Item (ADSDATATYPEFLAG_DEREFTYPEITEM, 0x10000000) */
733
+ DeRefTypeItem: 268435456, // 0x10000000
734
+ /** Extended Enum Infos. Comment and attributes for enum values (ADSDATATYPEFLAG_EXTENUMINFOS, 0x20000000) */
735
+ ExtendedEnumInfos: 536870912, // 0x20000000
736
+ /** Extended Flags used/added (ADSDATATYPEFLAG_EXTENDEDFLAGS, 0x80000000) */
737
+ ExtendedFlags: 2147483648, // 0x80000000
738
+ /** None / No Flag set */
739
+ None: 0,
740
+ /** Return given flag value as string array */
741
+ toStringArray: function (flags) {
742
+ const flagsArr = [];
743
+ for (const key of Object.keys(this)) {
744
+ const typedKey = key;
745
+ if (typeof this[typedKey] !== 'number')
746
+ continue;
747
+ //Check if flag is available
748
+ if ((flags & this[typedKey]) === this[typedKey]) {
749
+ if (flags === 0 || this[typedKey] !== 0)
750
+ flagsArr.push(key);
751
+ }
752
+ }
753
+ return flagsArr;
754
+ }
755
+ };
756
+ /**
757
+ * ADS data types
758
+ *
759
+ * Source: TwinCAT.Ads.dll By Beckhoff
760
+ */
761
+ exports.ADS_DATA_TYPES = {
762
+ /** Empty Type*/
763
+ ADST_VOID: 0,
764
+ /**Integer 16 Bit*/
765
+ ADST_INT16: 2,
766
+ /**Integer 32 Bit*/
767
+ ADST_INT32: 3,
768
+ /**Real (32 Bit)*/
769
+ ADST_REAL32: 4,
770
+ /**Real 64 Bit*/
771
+ ADST_REAL64: 5,
772
+ /**Integer 8 Bit*/
773
+ ADST_INT8: 16, // 0x00000010
774
+ /**Unsigned integer 8 Bit*/
775
+ ADST_UINT8: 17, // 0x00000011
776
+ /**Unsigned integer 16 Bit*/
777
+ ADST_UINT16: 18, // 0x00000012
778
+ /**Unsigned Integer 32 Bit*/
779
+ ADST_UINT32: 19, // 0x00000013
780
+ /**LONG Integer 64 Bit*/
781
+ ADST_INT64: 20, // 0x00000014
782
+ /**Unsigned Long integer 64 Bit*/
783
+ ADST_UINT64: 21, // 0x00000015
784
+ /**STRING*/
785
+ ADST_STRING: 30, // 0x0000001E
786
+ /**WSTRING*/
787
+ ADST_WSTRING: 31, // 0x0000001F
788
+ /**ADS REAL80*/
789
+ ADST_REAL80: 32, // 0x00000020
790
+ /**ADS BIT*/
791
+ ADST_BIT: 33, // 0x00000021
792
+ /**Internal Only*/
793
+ ADST_MAXTYPES: 34, // 0x00000022
794
+ /**Blob*/
795
+ ADST_BIGTYPE: 65, // 0x00000041
796
+ /** Returns the corresponding key as string by given value (number) */
797
+ toString: function (value) {
798
+ const val = Object.keys(this).find((key) => this[key] == value);
799
+ return (val !== undefined ? val : 'UNKNOWN');
800
+ }
801
+ };
802
+ /**
803
+ * ADS RCP method flags
804
+ *
805
+ * Source: TwinCAT.Ads.dll By Beckhoff
806
+ */
807
+ exports.ADS_RCP_METHOD_FLAGS = {
808
+ /** (ADSMETHODFLAG_PLC_CALLINGCONVENTION, 0x00000001) */
809
+ PlcCallingConvention: 1,
810
+ /** (ADSMETHODFLAG_CALL_UNLOCKED, 0x00000002) */
811
+ CallUnlocked: 2,
812
+ /** (ADSMETHODFLAG_NOTCALLABLE, 0x00000004) */
813
+ NotCallable: 4,
814
+ /** (ADSMETHODFLAG_ATTRIBUTES, 0x00000008) */
815
+ Attributes: 8,
816
+ /** Return given flag value as string array */
817
+ toStringArray: function (flags) {
818
+ const flagsArr = [];
819
+ for (const key of Object.keys(this)) {
820
+ const typedKey = key;
821
+ if (typeof this[typedKey] !== 'number')
822
+ continue;
823
+ //Check if flag is available
824
+ if ((flags & this[typedKey]) === this[typedKey]) {
825
+ if (flags === 0 || this[typedKey] !== 0)
826
+ flagsArr.push(key);
827
+ }
828
+ }
829
+ return flagsArr;
830
+ }
831
+ };
832
+ /**
833
+ * ADS RCP method parameter flags
834
+ *
835
+ * Source: TwinCAT.Ads.dll By Beckhoff
836
+ */
837
+ exports.ADS_RCP_METHOD_PARAM_FLAGS = {
838
+ /** Input Parameter (ADSMETHODPARAFLAG_IN) */
839
+ In: 1,
840
+ /** Output Parameter (ADSMETHODPARAFLAG_OUT) */
841
+ Out: 2,
842
+ /**
843
+ * By reference Parameter (ADSMETHODPARAFLAG_BYREFERENCE)
844
+ * This flag indicates parameters that are transfered as REFERENCE or POINTER Types.
845
+ */
846
+ ByReference: 4,
847
+ /** (ADSMETHODPARAFLAG_RPC_OUTPTR, 0x00000008) */
848
+ RpcOutPtr: 4,
849
+ /** (ADSMETHODPARAFLAG_RPC_ARRAYDIM_MASK,0x00000030) */
850
+ MaskRpcArrayDim: 48,
851
+ /** Attributes (ADSMETHODPARAFLAG_ATTRIBUTES, 0x00000040)*/
852
+ Attributes: 64,
853
+ /** Return given flag value as string array */
854
+ toStringArray: function (flags) {
855
+ const flagsArr = [];
856
+ for (const key of Object.keys(this)) {
857
+ const typedKey = key;
858
+ if (typeof this[typedKey] !== 'number')
859
+ continue;
860
+ //Check if flag is available
861
+ if ((flags & this[typedKey]) === this[typedKey]) {
862
+ if (flags === 0 || this[typedKey] !== 0)
863
+ flagsArr.push(key);
864
+ }
865
+ }
866
+ return flagsArr;
867
+ }
868
+ };
869
+ /**
870
+ * AMS router state
871
+ */
872
+ exports.AMS_ROUTER_STATE = {
873
+ /** Router is stopped */
874
+ STOP: 0,
875
+ /** Router is started */
876
+ START: 1,
877
+ /** Router is remove (unavailable?) */
878
+ REMOVED: 2,
879
+ /** Returns the corresponding key as string by given value (number) */
880
+ toString: function (value) {
881
+ const val = Object.keys(this).find((key) => this[key] == value);
882
+ return (val !== undefined ? val : 'UNKNOWN');
883
+ }
884
+ };
885
+ /**
886
+ * Base data types
887
+ *
888
+ * Object that handles PLC base data types
889
+ */
890
+ exports.BASE_DATA_TYPES = {
891
+ /**
892
+ * Returns true if given data type is found and known
893
+ *
894
+ * @param name Data type name
895
+ */
896
+ isKnownType: function (name) {
897
+ return this.find(name.trim()) !== null;
898
+ },
899
+ /**
900
+ * Finds the given data type from array
901
+ *
902
+ * @param name Data type name
903
+ */
904
+ find: function (name) {
905
+ let regExpRes = null;
906
+ //First, try to find easy way
907
+ let type = this.types.find(type => type.name.includes(name.toUpperCase()));
908
+ if (!type) {
909
+ //Not found, try to find the correct type with regular expressions
910
+ //We can find also STRING(xx), WSTRING(xx) and DWORD(100..2000) for example
911
+ type = this.types.find(type => {
912
+ const re = new RegExp('^(' + type.name.join('|') + ')([\\[\\(](.*)[\\)\\]])*$', 'i');
913
+ //Using match instead of test so we get capture groups too
914
+ regExpRes = name.trim().match(re);
915
+ return (regExpRes != null);
916
+ });
917
+ }
918
+ if (!type) {
919
+ return null;
920
+ }
921
+ //We are here -> type is found
922
+ //If string/wstring, regExpRes[3] is the string length -> use it
923
+ if (type.adsDataType === exports.ADS_DATA_TYPES.ADST_STRING) {
924
+ if (regExpRes === null) {
925
+ return null;
926
+ }
927
+ type.size = (regExpRes[3] != null ? (parseInt(regExpRes[3]) + 1) : type.size);
928
+ }
929
+ else if (type.adsDataType === exports.ADS_DATA_TYPES.ADST_WSTRING) {
930
+ if (regExpRes === null) {
931
+ return null;
932
+ }
933
+ type.size = (regExpRes[3] != null ? (parseInt(regExpRes[3]) * 2 + 2) : type.size);
934
+ }
935
+ return type;
936
+ },
937
+ /**
938
+ * Writes given value to given or new buffer
939
+ *
940
+ * @param name Data type name
941
+ * @param value Value to write
942
+ * @param buffer Optional - Buffer to write to. If not given, new Buffer is allocated
943
+ */
944
+ toBuffer: function (name, value, buffer) {
945
+ const type = this.find(name);
946
+ if (!type) {
947
+ throw new Error(`Error: Base type ${name} not found from BaseDataTypes - If this should be found, report an issue`);
948
+ }
949
+ //If buffer not given, allocate new
950
+ if (buffer === undefined) {
951
+ buffer = Buffer.allocUnsafe(type.size);
952
+ }
953
+ type.toBuffer(value, buffer);
954
+ return buffer;
955
+ },
956
+ /**
957
+ * Reads given data type from given buffer
958
+ *
959
+ * @param {string} name Data type name
960
+ * @param {Buffer} buffer Buffer to read from
961
+ */
962
+ fromBuffer: function (name, buffer, convertDates) {
963
+ const type = this.find(name);
964
+ if (!type) {
965
+ throw new Error(`Error: Base type ${name} not found from BaseDataTypes - If this should be found, report an issue`);
966
+ }
967
+ return type.fromBuffer(buffer, convertDates);
968
+ },
969
+ /**
970
+ * All base data types and properties
971
+ */
972
+ types: [
973
+ {
974
+ name: ['STRING'],
975
+ adsDataType: exports.ADS_DATA_TYPES.ADST_STRING,
976
+ size: 81, //Just default size
977
+ toBuffer: (value, buffer) => (0, exports.encodeStringToPlcStringBuffer)(value).copy(buffer),
978
+ fromBuffer: (buffer) => (0, exports.decodePlcStringBuffer)(buffer)
979
+ },
980
+ {
981
+ name: ['WSTRING'],
982
+ adsDataType: exports.ADS_DATA_TYPES.ADST_WSTRING,
983
+ size: 162, //Just default size
984
+ toBuffer: (value, buffer) => (0, exports.encodeStringToPlcWstringBuffer)(value).copy(buffer),
985
+ fromBuffer: (buffer) => (0, exports.decodePlcWstringBuffer)(buffer)
986
+ },
987
+ {
988
+ name: ['BOOL', 'BIT', 'BIT8'],
989
+ adsDataType: exports.ADS_DATA_TYPES.ADST_BIT,
990
+ size: 1,
991
+ toBuffer: (value, buffer) => buffer.writeUInt8(value ? 1 : 0),
992
+ fromBuffer: (buffer) => buffer.readUInt8(0) !== 0
993
+ },
994
+ {
995
+ name: ['BYTE', 'USINT', 'BITARR8', 'UINT8'],
996
+ adsDataType: exports.ADS_DATA_TYPES.ADST_UINT8,
997
+ size: 1,
998
+ toBuffer: (value, buffer) => buffer.writeUInt8(value),
999
+ fromBuffer: (buffer) => buffer.readUInt8(0)
1000
+ },
1001
+ {
1002
+ name: ['SINT', 'INT8'],
1003
+ adsDataType: exports.ADS_DATA_TYPES.ADST_INT8,
1004
+ size: 1,
1005
+ toBuffer: (value, buffer) => buffer.writeInt8(value),
1006
+ fromBuffer: (buffer) => buffer.readInt8(0)
1007
+ },
1008
+ {
1009
+ name: ['UINT', 'WORD', 'BITARR16', 'UINT16'],
1010
+ adsDataType: exports.ADS_DATA_TYPES.ADST_UINT16,
1011
+ size: 2,
1012
+ toBuffer: (value, buffer) => buffer.writeUInt16LE(value),
1013
+ fromBuffer: (buffer) => buffer.readUInt16LE(0)
1014
+ },
1015
+ {
1016
+ name: ['INT', 'INT16'],
1017
+ adsDataType: exports.ADS_DATA_TYPES.ADST_INT16,
1018
+ size: 2,
1019
+ toBuffer: (value, buffer) => buffer.writeInt16LE(value),
1020
+ fromBuffer: (buffer) => buffer.readInt16LE(0)
1021
+ },
1022
+ {
1023
+ name: ['DINT', 'INT32'],
1024
+ adsDataType: exports.ADS_DATA_TYPES.ADST_INT32,
1025
+ size: 4,
1026
+ toBuffer: (value, buffer) => buffer.writeInt32LE(value),
1027
+ fromBuffer: (buffer) => buffer.readInt32LE(0)
1028
+ },
1029
+ {
1030
+ name: ['UDINT', 'DWORD', 'TIME', 'TIME_OF_DAY', 'TOD', 'BITARR32', 'UINT32'],
1031
+ adsDataType: exports.ADS_DATA_TYPES.ADST_UINT32,
1032
+ size: 4,
1033
+ toBuffer: (value, buffer) => buffer.writeUInt32LE(value),
1034
+ fromBuffer: (buffer) => buffer.readUInt32LE(0)
1035
+ },
1036
+ {
1037
+ name: ['DATE_AND_TIME', 'DT', 'DATE'],
1038
+ adsDataType: exports.ADS_DATA_TYPES.ADST_UINT32,
1039
+ size: 4,
1040
+ toBuffer: (value, buffer) => {
1041
+ if (typeof value === 'object' && value.getTime !== undefined) {
1042
+ buffer.writeUInt32LE(value.getTime() / 1000);
1043
+ }
1044
+ else {
1045
+ buffer.writeUInt32LE(value);
1046
+ }
1047
+ },
1048
+ fromBuffer: (buffer, convertDates) => {
1049
+ if (convertDates) {
1050
+ return new Date(buffer.readUInt32LE(0) * 1000);
1051
+ }
1052
+ else {
1053
+ return buffer.readUInt32LE(0);
1054
+ }
1055
+ }
1056
+ },
1057
+ {
1058
+ name: ['REAL', 'FLOAT'],
1059
+ adsDataType: exports.ADS_DATA_TYPES.ADST_REAL32,
1060
+ size: 4,
1061
+ toBuffer: (value, buffer) => buffer.writeFloatLE(value),
1062
+ fromBuffer: (buffer) => buffer.readFloatLE(0)
1063
+ },
1064
+ {
1065
+ name: ['LREAL', 'DOUBLE'],
1066
+ adsDataType: exports.ADS_DATA_TYPES.ADST_REAL64,
1067
+ size: 8,
1068
+ toBuffer: (value, buffer) => buffer.writeDoubleLE(value),
1069
+ fromBuffer: (buffer) => buffer.readDoubleLE(0)
1070
+ },
1071
+ {
1072
+ name: ['LWORD', 'ULINT', 'LTIME', 'UINT64'],
1073
+ adsDataType: exports.ADS_DATA_TYPES.ADST_UINT64,
1074
+ size: 8,
1075
+ toBuffer: (value, buffer) => {
1076
+ //64 bit integers are missing from older Node.js Buffer, so use buffer instead if so
1077
+ if (typeof value === 'bigint' && buffer.writeBigUInt64LE) {
1078
+ buffer.writeBigUInt64LE(value);
1079
+ }
1080
+ else if (typeof value === 'number' && buffer.writeBigUInt64LE) {
1081
+ buffer.writeBigUInt64LE(BigInt(value));
1082
+ }
1083
+ else if (typeof value === 'object' && Buffer.isBuffer(value)) {
1084
+ value.copy(buffer);
1085
+ }
1086
+ else {
1087
+ throw new Error(`Could not convert ${value} to LWORD/ULINT/LTIME - BigInt is not supported`);
1088
+ }
1089
+ },
1090
+ fromBuffer: (buffer) => {
1091
+ if (buffer.readBigUInt64LE !== undefined) {
1092
+ return buffer.readBigUInt64LE(0);
1093
+ }
1094
+ else {
1095
+ return buffer;
1096
+ }
1097
+ }
1098
+ },
1099
+ {
1100
+ name: ['LINT', 'INT64'],
1101
+ adsDataType: exports.ADS_DATA_TYPES.ADST_INT64,
1102
+ size: 8,
1103
+ toBuffer: (value, buffer) => {
1104
+ //64 bit integers are missing from older Node.js Buffer, so use buffer instead if so
1105
+ if (typeof value === 'bigint' && buffer.writeBigInt64LE) {
1106
+ buffer.writeBigInt64LE(value);
1107
+ }
1108
+ else if (typeof value === 'number' && buffer.writeBigInt64LE) {
1109
+ buffer.writeBigInt64LE(BigInt(value));
1110
+ }
1111
+ else if (typeof value === 'object' && Buffer.isBuffer(value)) {
1112
+ value.copy(buffer);
1113
+ }
1114
+ else {
1115
+ throw new Error(`Could not convert ${value} to LINT - BigInt is not supported`);
1116
+ }
1117
+ },
1118
+ fromBuffer: (buffer) => {
1119
+ if (buffer.readBigInt64LE !== undefined) {
1120
+ return buffer.readBigInt64LE(0);
1121
+ }
1122
+ else {
1123
+ return buffer;
1124
+ }
1125
+ }
1126
+ },
1127
+ ],
1128
+ /**
1129
+ * Returns true if given data type is found and known
1130
+ *
1131
+ * @param {string} name Data type name
1132
+ */
1133
+ isPseudoType: function (name) {
1134
+ return this.findPseudoType(name) !== undefined;
1135
+ },
1136
+ /**
1137
+ * Finds the given pseudo data type from array using regular expressions
1138
+ *
1139
+ * @param {string} name Pseudo data type name
1140
+ */
1141
+ findPseudoType: function (name) {
1142
+ return this.pseudoTypes.find(type => {
1143
+ const regexName = type.name.map(t => `^${t}`);
1144
+ const re = new RegExp(regexName.join('|'), 'i');
1145
+ return re.test(name.trim());
1146
+ });
1147
+ },
1148
+ /**
1149
+ * Finds the given pseudo data type from array using regular expressions
1150
+ *
1151
+ * @param name Pseudo data type name (like PVOID)
1152
+ */
1153
+ getTypeByPseudoType: function (name, byteSize) {
1154
+ return this.findPseudoType(name)?.actualTypesBySize[byteSize];
1155
+ },
1156
+ /**
1157
+ * All pseudo data types and their actual types depending on size
1158
+ * Example: XINT is 4 bytes @ 32 bit platform -> DINT
1159
+ */
1160
+ pseudoTypes: [
1161
+ {
1162
+ name: ['XINT', '__XINT'],
1163
+ actualTypesBySize: {
1164
+ 4: 'DINT',
1165
+ 8: 'LINT'
1166
+ }
1167
+ },
1168
+ {
1169
+ name: ['UXINT', '__UXINT', 'POINTER TO', 'REFERENCE TO', 'PVOID'],
1170
+ actualTypesBySize: {
1171
+ 4: 'UDINT',
1172
+ 8: 'ULINT'
1173
+ }
1174
+ },
1175
+ {
1176
+ name: ['XWORD', '__XWORD'],
1177
+ actualTypesBySize: {
1178
+ 4: 'DWORD',
1179
+ 8: 'LWORD'
1180
+ }
1181
+ },
1182
+ ]
1183
+ };
1184
+ /**
1185
+ * Converts array of bytes (or Buffer) to AmsNetId string, such as 192.168.1.10.1.1
1186
+ *
1187
+ * @param byteArray Array of bytes or Buffer object
1188
+ */
1189
+ const byteArrayToAmsNetIdStr = (byteArray) => {
1190
+ return byteArray.join('.');
1191
+ };
1192
+ exports.byteArrayToAmsNetIdStr = byteArrayToAmsNetIdStr;
1193
+ /**
1194
+ * Converts AmsNetId string (such as 192.168.10.1.1) to array of bytes
1195
+ *
1196
+ * @param str AmsNetId as string
1197
+ */
1198
+ const amsNetIdStrToByteArray = (str) => {
1199
+ return str.split('.').map(x => parseInt(x));
1200
+ };
1201
+ exports.amsNetIdStrToByteArray = amsNetIdStrToByteArray;
1202
+ /**
1203
+ * Trims the given PLC string until end mark (\0) is found (removes empty bytes from the end)
1204
+ *
1205
+ * @param str String to trim
1206
+ * @returns Trimmed string
1207
+ */
1208
+ const trimPlcString = (str) => str.substring(0, str.indexOf("\0"));
1209
+ exports.trimPlcString = trimPlcString;
1210
+ /**
1211
+ * Decodes provided Buffer object to plc STRING using cp1252 encoding.
1212
+ * Also removes empty bytes from the end.
1213
+ * @param data Buffer data that contains plc STRING
1214
+ * @returns
1215
+ */
1216
+ const decodePlcStringBuffer = (data) => (0, exports.trimPlcString)(iconv_lite_1.default.decode(data, "cp1252"));
1217
+ exports.decodePlcStringBuffer = decodePlcStringBuffer;
1218
+ /**
1219
+ * Encodes provided string to a Buffer object as plc STRING using cp1252 encoding
1220
+ * @param str String to encode
1221
+ * @returns
1222
+ */
1223
+ const encodeStringToPlcStringBuffer = (str) => iconv_lite_1.default.encode(str, "cp1252");
1224
+ exports.encodeStringToPlcStringBuffer = encodeStringToPlcStringBuffer;
1225
+ /**
1226
+ * Decodes provided Buffer object to plc WSTRING using ucs2 encoding.
1227
+ * Also removes empty bytes from the end.
1228
+ * @param data Buffer data that contains plc WSTRING
1229
+ * @returns
1230
+ */
1231
+ const decodePlcWstringBuffer = (data) => (0, exports.trimPlcString)(iconv_lite_1.default.decode(data, "ucs2"));
1232
+ exports.decodePlcWstringBuffer = decodePlcWstringBuffer;
1233
+ /**
1234
+ * Encodes provided string to a Buffer object as plc WSTRING using ucs2 encoding
1235
+ * @param str String to encode
1236
+ * @returns
1237
+ */
1238
+ const encodeStringToPlcWstringBuffer = (str) => iconv_lite_1.default.encode(str, "ucs2");
1239
+ exports.encodeStringToPlcWstringBuffer = encodeStringToPlcWstringBuffer;
1240
+ /**
1241
+ * Converts given AmsAddress as string of type `amsNetId:port`
1242
+ * @param address
1243
+ * @returns
1244
+ */
1245
+ const amsAddressToString = (address) => `${address.amsNetId}:${address.adsPort}`;
1246
+ exports.amsAddressToString = amsAddressToString;
1247
+ //# sourceMappingURL=ads-commons.js.map