ads-client 2.0.0-beta.4 → 2.0.0-beta.6

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.
@@ -1,7 +1,7 @@
1
1
  import EventEmitter from "events";
2
2
  import * as ADS from './ads-commons';
3
- import type { ActiveSubscription, AdsClientConnection, AdsClientSettings, AdsCommandToSend, AdsDataTypeContainer, AdsSymbolContainer, AdsUploadInfo, ConnectionMetaData, SubscriptionSettings, ReadValueResult, WriteValueResult, VariableHandle, RpcMethodCallResult, CreateVariableHandleMultiResult, ReadRawMultiResult, ReadRawMultiCommand, WriteRawMultiResult, DeleteVariableHandleMultiResult, ReadWriteRawMultiResult, ReadWriteRawMultiCommand, WriteRawMultiCommand, SubscriptionCallback, DebugLevel, AdsClientEvents } from "./types/ads-client-types";
4
- import { AdsDataType, AdsDeviceInfo, AdsResponse, AdsState, AdsSymbol, AmsAddress, AmsTcpPacket } from "./types/ads-protocol-types";
3
+ import type { ActiveSubscription, AdsClientConnection, AdsClientSettings, AdsCommandToSend, AdsDataTypeContainer, AdsSymbolContainer, ConnectionMetaData, SubscriptionSettings, ReadValueResult, WriteValueResult, VariableHandle, RpcMethodCallResult, CreateVariableHandleMultiResult, ReadRawMultiResult, ReadRawMultiCommand, WriteRawMultiResult, DeleteVariableHandleMultiResult, ReadWriteRawMultiResult, ReadWriteRawMultiCommand, WriteRawMultiCommand, SubscriptionCallback, DebugLevel, AdsClientEvents, SendAdsCommandWithFallbackResult } from "./types/ads-client-types";
4
+ import { AdsAttributeEntry, AdsDataType, AdsDeviceInfo, AdsResponse, AdsState, AdsSymbol, AmsAddress, AmsTcpPacket, AdsUploadInfo } from "./types/ads-protocol-types";
5
5
  export type * from "./types/ads-client-types";
6
6
  export type * from './types/ads-protocol-types';
7
7
  export type * from './client-error';
@@ -271,6 +271,14 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
271
271
  * @param subscription The subscription object (unused here)
272
272
  */
273
273
  private onPlcRuntimeStateChanged;
274
+ /**
275
+ * Checks if PLC runtime state has changed, and if so, emits an event.
276
+ *
277
+ * Call from `onPlcRuntimeStateChanged()` and `readPlcRuntimeState()`.
278
+ *
279
+ * @param state Active PLC runtime state
280
+ */
281
+ private handlePlcRuntimeStateChange;
274
282
  /**
275
283
  * A subscription callback that is called when the target PLC runtime symbol version has changed.
276
284
  *
@@ -463,8 +471,16 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
463
471
  *
464
472
  * @param buffer The raw data to convert
465
473
  * @param dataType Target data type
474
+ * @param attributes Additional attributes of the symbol or data type to use for conversion
466
475
  */
467
476
  private convertBufferToPrimitiveType;
477
+ /**
478
+ * Extracts TcEncoding attribute from the data datatype attributes or additional attributes
479
+ *
480
+ * @param dataType The data type
481
+ * @param attributes Additional attributes of the symbol or data type
482
+ */
483
+ private getStringDataTypeEncoding;
468
484
  /**
469
485
  * Converts raw data to Javascript object.
470
486
  *
@@ -472,6 +488,7 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
472
488
  *
473
489
  * @param data The raw data to convert
474
490
  * @param dataType Target data type
491
+ * @param attributes Additional attributes of the symbol or data type used for conversion
475
492
  * @param isArrayItem If `true`, this is an array item (default: `false`)
476
493
  */
477
494
  private convertBufferToObject;
@@ -480,6 +497,7 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
480
497
  *
481
498
  * @param value Javascript object to convert
482
499
  * @param dataType Target data type
500
+ * @param attributes Additional attributes of the symbol or data type used for conversion
483
501
  * @param objectPath Object path that is passed forward when calling recursively. This is used for error reporting if a property is missing
484
502
  * @param isArrayItem If `true`, this is an array item (default: `false`)
485
503
  */
@@ -490,6 +508,7 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
490
508
  * @param value Javascript object to convert
491
509
  * @param dataType Data type
492
510
  * @param buffer Reference to Buffer object where to write the raw value
511
+ * @param attributes Additional attributes of the symbol or data type used for conversion
493
512
  */
494
513
  private convertPrimitiveTypeToBuffer;
495
514
  /**
@@ -623,12 +642,69 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
623
642
  * }
624
643
  * ```
625
644
  *
645
+ * @param command The ADS command to send
646
+ *
626
647
  * @template T In Typescript, the type of the ADS response. If omitted, generic {@link AdsResponse} type is used.
627
648
  *
628
649
  * @throws Throws an error if sending the command fails or if target responds with an error
629
650
  *
630
651
  */
631
652
  sendAdsCommand<T = AdsResponse>(command: AdsCommandToSend): Promise<AmsTcpPacket<T>>;
653
+ /**
654
+ * Sends a raw ADS command to the target with fallback. A wrapper for {@link Client.sendAdsCommand}().
655
+ *
656
+ * Calls `sendAdsCommand(command)` and if it fails with
657
+ * ADS error 1793 or 1808 then calls the `sendAdsCommand(fallback)`.
658
+ *
659
+ * See {@link Client.readPlcUploadInfo}() for use case.
660
+ *
661
+ * The ideas is copied from TwinCAT.Ads.dll (`TwinCAT.Ads.AdsClientExtensions.ReadWithFallbackAsync()`).
662
+ *
663
+ * @example
664
+ * ```js
665
+ * try {
666
+ * const data = Buffer.alloc(12);
667
+ * //...code omitted...
668
+ *
669
+ * const command = {
670
+ * adsCommand: ADS.ADS_COMMAND.Read,
671
+ * targetAmsNetId: targetOpts.amsNetId,
672
+ * targetAdsPort: targetOpts.adsPort,
673
+ * payload: data
674
+ * };
675
+ *
676
+ * const fbData = Buffer.alloc(12);
677
+ * //...code omitted...
678
+ *
679
+ * const fallback = {
680
+ * adsCommand: ADS.ADS_COMMAND.Read,
681
+ * targetAmsNetId: targetOpts.amsNetId,
682
+ * targetAdsPort: targetOpts.adsPort,
683
+ * payload: fbData
684
+ * };
685
+ *
686
+ * const { response: res, fallbackUsed } = await this.sendAdsCommandWithFallback<AdsReadResponse>(command, fallback);
687
+ *
688
+ * //If we are here, one of those commands was succcesful
689
+ * if(fallbackUsed) {
690
+ * console.log("Fallback was used. Result:", res.ads.payload);
691
+ * } else {
692
+ * console.log("Fallback was not used. Result:", res.ads.payload);
693
+ * }
694
+ *
695
+ * } catch (err) {
696
+ * console.log("Error:", err);
697
+ * }
698
+ * ```
699
+ *
700
+ * @param command The main ADS command to send
701
+ * @param fallback The fallback ADS command to send
702
+ *
703
+ * @template T In Typescript, the type of the ADS response. If omitted, generic {@link AdsResponse} type is used.
704
+ *
705
+ * @throws Throws an error if sending the command fails or if target responds with an error
706
+ */
707
+ sendAdsCommandWithFallback<T = AdsResponse>(command: AdsCommandToSend, fallback: AdsCommandToSend): Promise<SendAdsCommandWithFallbackResult<T>>;
632
708
  /**
633
709
  * Sends an ADS `WriteControl` command to the target.
634
710
  *
@@ -1501,11 +1577,11 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
1501
1577
  *
1502
1578
  * //Writing a POINTER value (Note the dereference operator ^)
1503
1579
  * const ptrValue = ...
1504
- * await client.writeRawByPath('GVL_Read.ComplexTypes.POINTER_^', ptrValue);
1580
+ * await client.writeRawByPath('GVL_Write.ComplexTypes.POINTER_^', ptrValue);
1505
1581
  *
1506
1582
  * //Writing a REFERENCE value
1507
1583
  * const refValue = ...
1508
- * await client.readRawByPath('GVL_Read.ComplexTypes.REFERENCE_');
1584
+ * await client.writeRawByPath('GVL_Write.ComplexTypes.REFERENCE_');
1509
1585
  *
1510
1586
  * } catch (err) {
1511
1587
  * console.log("Error:", err);
@@ -1675,11 +1751,9 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
1675
1751
  * @example
1676
1752
  * ```js
1677
1753
  * try {
1678
- * const value = {
1679
- * example: true
1680
- * };
1754
+ * const res = await client.writeValue('GVL_Write.StandardTypes.INT_', 32767);
1755
+ * console.log('Value written:', res.value);
1681
1756
  *
1682
- * const res = await client.writeValue('GVL_Read.StandardTypes.INT_', value);
1683
1757
  * } catch (err) {
1684
1758
  * console.log("Error:", err);
1685
1759
  * }
@@ -1746,12 +1820,13 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
1746
1820
  * ```
1747
1821
  *
1748
1822
  * @param dataType Data type name in the PLC as string (such as `ST_Struct`) or data type object (acquired using {@link getDataType}())
1823
+ * @param attributes Additional attributes of the symbol or data type used for conversion
1749
1824
  * @param targetOpts Optional target settings that override values in `settings`
1750
1825
  *
1751
1826
  * @template T Typescript data type of the PLC data, for example `getDefaultPlcObject<number>(...)` or `getDefaultPlcObject<ST_TypedStruct>(...)`
1752
1827
  * @throws Throws an error if sending the command fails or if the target responds with an error.
1753
1828
  */
1754
- getDefaultPlcObject<T = any>(dataType: string | AdsDataType, targetOpts?: Partial<AmsAddress>): Promise<T>;
1829
+ getDefaultPlcObject<T = any>(dataType: string | AdsDataType, attributes?: AdsAttributeEntry[], targetOpts?: Partial<AmsAddress>): Promise<T>;
1755
1830
  /**
1756
1831
  * Converts raw data to a Javascript object by using the provided data type.
1757
1832
  *
@@ -1771,12 +1846,13 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
1771
1846
  *
1772
1847
  * @param data Raw data (acquired for example using {@link readRaw}())
1773
1848
  * @param dataType Data type name in the PLC as string (such as `ST_Struct`) or data type object (acquired using {@link getDataType}())
1849
+ * @param attributes Additional attributes of the symbol or data type used for conversion
1774
1850
  * @param targetOpts Optional target settings that override values in `settings`
1775
1851
  *
1776
1852
  * @template T Typescript data type of the PLC data, for example `convertFromRaw<number>(...)` or `convertFromRaw<ST_TypedStruct>(...)`
1777
1853
  * @throws Throws an error if sending the command fails or if the target responds with an error.
1778
1854
  */
1779
- convertFromRaw<T = any>(data: Buffer, dataType: string | AdsDataType, targetOpts?: Partial<AmsAddress>): Promise<T>;
1855
+ convertFromRaw<T = any>(data: Buffer, dataType: string | AdsDataType, attributes?: AdsAttributeEntry[], targetOpts?: Partial<AmsAddress>): Promise<T>;
1780
1856
  /**
1781
1857
  * Converts a Javascript object to raw data by using the provided data type.
1782
1858
  *
@@ -1796,11 +1872,12 @@ export declare class Client extends EventEmitter<AdsClientEvents> {
1796
1872
  * @param value Value to convert
1797
1873
  * @param dataType Data type name in the PLC as string (such as `ST_Struct`) or data type object (acquired using {@link getDataType}())
1798
1874
  * @param autoFill autoFill If set and the data type is a container (`STRUCT`, `FUNCTION_BLOCK` etc.), missing properties are automatically set to default values (`0` or empty string).
1875
+ * @param attributes Additional attributes of the symbol or data type used for conversion
1799
1876
  * @param targetOpts Optional target settings that override values in `settings`
1800
1877
  *
1801
1878
  * @throws Throws an error if sending the command fails or if the target responds with an error.
1802
1879
  */
1803
- convertToRaw(value: any, dataType: string | AdsDataType, autoFill?: boolean, targetOpts?: Partial<AmsAddress>): Promise<Buffer>;
1880
+ convertToRaw(value: any, dataType: string | AdsDataType, autoFill?: boolean, attributes?: AdsAttributeEntry[], targetOpts?: Partial<AmsAddress>): Promise<Buffer>;
1804
1881
  /**
1805
1882
  * Creates a handle to a variable at the target system by variable path (such as `GVL_Test.ExampleStruct`).
1806
1883
  *