@enyo-energy/sunspec-sdk 0.0.4 → 0.0.5

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.
@@ -51,29 +51,63 @@ class SunspecModbusClient {
51
51
  this.scaleFactors = {};
52
52
  }
53
53
  }
54
+ /**
55
+ * Detect the base address and addressing mode (0-based or 1-based) for SunSpec
56
+ */
57
+ async detectSunspecBaseAddress() {
58
+ if (!this.modbusClient) {
59
+ throw new Error('Modbus client not initialized');
60
+ }
61
+ // Try 1-based addressing first (most common)
62
+ try {
63
+ const sunspecId = await this.modbusClient.readRegisterStringValue(40001, 2);
64
+ if (sunspecId.includes('SunS')) {
65
+ console.log('Detected 1-based addressing mode (base address: 40001)');
66
+ this.baseAddress = 40001;
67
+ return {
68
+ baseAddress: 40001,
69
+ isZeroBased: false,
70
+ nextAddress: 40003
71
+ };
72
+ }
73
+ }
74
+ catch (error) {
75
+ console.debug('Could not read SunS at 40001:', error);
76
+ }
77
+ // Try 0-based addressing
78
+ try {
79
+ const sunspecId = await this.modbusClient.readRegisterStringValue(40000, 2);
80
+ if (sunspecId.includes('SunS')) {
81
+ console.log('Detected 0-based addressing mode (base address: 40000)');
82
+ this.baseAddress = 40000;
83
+ return {
84
+ baseAddress: 40000,
85
+ isZeroBased: true,
86
+ nextAddress: 40002
87
+ };
88
+ }
89
+ }
90
+ catch (error) {
91
+ console.debug('Could not read SunS at 40000:', error);
92
+ }
93
+ throw new Error('Device is not SunSpec compliant - "SunS" identifier not found at addresses 40000 or 40001');
94
+ }
54
95
  /**
55
96
  * Discover all available Sunspec models
56
- * Scans through the address space starting at 40001
97
+ * Automatically detects base address (40000 or 40001) and scans from there
57
98
  */
58
99
  async discoverModels() {
59
100
  if (!this.connected) {
60
101
  throw new Error('Not connected to Modbus device');
61
102
  }
62
103
  this.discoveredModels.clear();
63
- let currentAddress = this.baseAddress;
64
104
  const maxAddress = 50000; // Safety limit
105
+ let currentAddress = 0;
65
106
  console.log('Starting Sunspec model discovery...');
66
107
  try {
67
- // First, check for Sunspec identifier "SunS" at 40001
68
- if (!this.modbusClient) {
69
- throw new Error('Modbus client not initialized');
70
- }
71
- const sunspecId = await this.modbusClient.readRegisterStringValue(40001, 2);
72
- if (!sunspecId.includes('SunS')) {
73
- console.warn('Device may not be Sunspec compliant - missing SunS identifier');
74
- }
75
- // Start scanning after the SunS identifier
76
- currentAddress = 40003;
108
+ // Detect the base address and addressing mode
109
+ const addressInfo = await this.detectSunspecBaseAddress();
110
+ currentAddress = addressInfo.nextAddress;
77
111
  while (currentAddress < maxAddress) {
78
112
  // Read model ID and length
79
113
  if (!this.modbusClient) {
@@ -19,9 +19,17 @@ export declare class SunspecModbusClient {
19
19
  * Disconnect from Modbus device
20
20
  */
21
21
  disconnect(): Promise<void>;
22
+ /**
23
+ * Detect the base address and addressing mode (0-based or 1-based) for SunSpec
24
+ */
25
+ detectSunspecBaseAddress(): Promise<{
26
+ baseAddress: number;
27
+ isZeroBased: boolean;
28
+ nextAddress: number;
29
+ }>;
22
30
  /**
23
31
  * Discover all available Sunspec models
24
- * Scans through the address space starting at 40001
32
+ * Automatically detects base address (40000 or 40001) and scans from there
25
33
  */
26
34
  discoverModels(): Promise<Map<number, SunspecModel>>;
27
35
  /**
@@ -9,7 +9,7 @@ exports.getSdkVersion = getSdkVersion;
9
9
  /**
10
10
  * Current version of the enyo Energy App SDK.
11
11
  */
12
- exports.SDK_VERSION = '0.0.4';
12
+ exports.SDK_VERSION = '0.0.5';
13
13
  /**
14
14
  * Gets the current SDK version.
15
15
  * @returns The semantic version string of the SDK
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "0.0.4";
8
+ export declare const SDK_VERSION = "0.0.5";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
@@ -19,9 +19,17 @@ export declare class SunspecModbusClient {
19
19
  * Disconnect from Modbus device
20
20
  */
21
21
  disconnect(): Promise<void>;
22
+ /**
23
+ * Detect the base address and addressing mode (0-based or 1-based) for SunSpec
24
+ */
25
+ detectSunspecBaseAddress(): Promise<{
26
+ baseAddress: number;
27
+ isZeroBased: boolean;
28
+ nextAddress: number;
29
+ }>;
22
30
  /**
23
31
  * Discover all available Sunspec models
24
- * Scans through the address space starting at 40001
32
+ * Automatically detects base address (40000 or 40001) and scans from there
25
33
  */
26
34
  discoverModels(): Promise<Map<number, SunspecModel>>;
27
35
  /**
@@ -48,29 +48,63 @@ export class SunspecModbusClient {
48
48
  this.scaleFactors = {};
49
49
  }
50
50
  }
51
+ /**
52
+ * Detect the base address and addressing mode (0-based or 1-based) for SunSpec
53
+ */
54
+ async detectSunspecBaseAddress() {
55
+ if (!this.modbusClient) {
56
+ throw new Error('Modbus client not initialized');
57
+ }
58
+ // Try 1-based addressing first (most common)
59
+ try {
60
+ const sunspecId = await this.modbusClient.readRegisterStringValue(40001, 2);
61
+ if (sunspecId.includes('SunS')) {
62
+ console.log('Detected 1-based addressing mode (base address: 40001)');
63
+ this.baseAddress = 40001;
64
+ return {
65
+ baseAddress: 40001,
66
+ isZeroBased: false,
67
+ nextAddress: 40003
68
+ };
69
+ }
70
+ }
71
+ catch (error) {
72
+ console.debug('Could not read SunS at 40001:', error);
73
+ }
74
+ // Try 0-based addressing
75
+ try {
76
+ const sunspecId = await this.modbusClient.readRegisterStringValue(40000, 2);
77
+ if (sunspecId.includes('SunS')) {
78
+ console.log('Detected 0-based addressing mode (base address: 40000)');
79
+ this.baseAddress = 40000;
80
+ return {
81
+ baseAddress: 40000,
82
+ isZeroBased: true,
83
+ nextAddress: 40002
84
+ };
85
+ }
86
+ }
87
+ catch (error) {
88
+ console.debug('Could not read SunS at 40000:', error);
89
+ }
90
+ throw new Error('Device is not SunSpec compliant - "SunS" identifier not found at addresses 40000 or 40001');
91
+ }
51
92
  /**
52
93
  * Discover all available Sunspec models
53
- * Scans through the address space starting at 40001
94
+ * Automatically detects base address (40000 or 40001) and scans from there
54
95
  */
55
96
  async discoverModels() {
56
97
  if (!this.connected) {
57
98
  throw new Error('Not connected to Modbus device');
58
99
  }
59
100
  this.discoveredModels.clear();
60
- let currentAddress = this.baseAddress;
61
101
  const maxAddress = 50000; // Safety limit
102
+ let currentAddress = 0;
62
103
  console.log('Starting Sunspec model discovery...');
63
104
  try {
64
- // First, check for Sunspec identifier "SunS" at 40001
65
- if (!this.modbusClient) {
66
- throw new Error('Modbus client not initialized');
67
- }
68
- const sunspecId = await this.modbusClient.readRegisterStringValue(40001, 2);
69
- if (!sunspecId.includes('SunS')) {
70
- console.warn('Device may not be Sunspec compliant - missing SunS identifier');
71
- }
72
- // Start scanning after the SunS identifier
73
- currentAddress = 40003;
105
+ // Detect the base address and addressing mode
106
+ const addressInfo = await this.detectSunspecBaseAddress();
107
+ currentAddress = addressInfo.nextAddress;
74
108
  while (currentAddress < maxAddress) {
75
109
  // Read model ID and length
76
110
  if (!this.modbusClient) {
package/dist/version.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "0.0.4";
8
+ export declare const SDK_VERSION = "0.0.5";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/dist/version.js CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export const SDK_VERSION = '0.0.4';
8
+ export const SDK_VERSION = '0.0.5';
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enyo-energy/sunspec-sdk",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "enyo Energy Sunspec SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",