smithtek-mako-rf 3.0.3 → 3.1.0

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 (2) hide show
  1. package/package.json +1 -1
  2. package/smithtek-mako-rf.js +41 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smithtek-mako-rf",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
4
4
  "description": "Smithtek dedicated node for communicating with the Mako PLC over RS485 or RF",
5
5
  "keywords": [
6
6
  "node-red",
@@ -426,25 +426,41 @@ state._rssiCleanup = cleanup;
426
426
  // tiny settle time helps on Pi serial
427
427
  await sleep(50);
428
428
  }
429
-
430
429
  async function connectIfNeeded(cfg, state) {
431
430
  if (!cfg) throw new Error("Bus config missing");
432
431
 
433
432
  const portKey = makePortKey(cfg);
434
433
 
435
- // If settings changed, force reconnect
436
- if (state.connected && state.lastPortKey !== portKey) {
434
+ // If serial settings have actually changed, reconnect.
435
+ if (state.lastPortKey && state.lastPortKey !== portKey) {
437
436
  state.connected = false;
438
- await closeClientSafe(state);
437
+
438
+ if (state.client.isOpen) {
439
+ await closeClientSafe(state);
440
+ }
441
+ }
442
+
443
+ // Trust the actual modbus-serial port state.
444
+ // If the serial port is already open, reuse it.
445
+ if (state.client.isOpen) {
446
+ state.connected = true;
447
+ state.lastPortKey = portKey;
448
+ return;
439
449
  }
440
450
 
441
- // Already connected
442
- if (state.connected) return;
451
+ if (state.connected) {
452
+ state.connected = false;
453
+ }
443
454
 
444
- // If already connecting, wait for that attempt to finish
455
+ // If another request is already opening the port, wait.
445
456
  if (state.connecting) {
446
457
  for (let i = 0; i < 200; i++) {
447
- if (state.connected) return;
458
+ if (state.client.isOpen) {
459
+ state.connected = true;
460
+ state.lastPortKey = portKey;
461
+ return;
462
+ }
463
+
448
464
  if (!state.connecting) break;
449
465
  await sleep(50);
450
466
  }
@@ -463,23 +479,35 @@ state._rssiCleanup = cleanup;
463
479
  parity: cfg.parity || "none",
464
480
  };
465
481
 
466
- // Always close cleanly before opening (and WAIT for it)
467
- await closeClientSafe(state);
482
+ // Only close if the library says the port is actually open.
483
+ if (state.client.isOpen) {
484
+ await closeClientSafe(state);
485
+ }
486
+
487
+ // Allow Linux/serialport time to release any previous lock.
488
+ await sleep(200);
468
489
 
469
- // Open
470
490
  await state.client.connectRTUBuffered(port, options);
471
491
 
472
- // UI uses seconds; library uses ms
492
+ // UI uses seconds; library uses milliseconds.
473
493
  const timeout_s = Math.max(1, toNum(cfg.timeout_s, 8));
474
- state.client.setTimeout(Math.max(1000, Math.floor(timeout_s * 1000)));
494
+ state.client.setTimeout(
495
+ Math.max(1000, Math.floor(timeout_s * 1000))
496
+ );
475
497
 
476
498
  state.connected = true;
477
499
  state.lastPortKey = portKey;
500
+
501
+ } catch (err) {
502
+ state.connected = false;
503
+ throw err;
504
+
478
505
  } finally {
479
506
  state.connecting = false;
480
507
  }
481
508
  }
482
509
 
510
+
483
511
  async function doRequest(cfg, state, req) {
484
512
  await connectIfNeeded(cfg, state);
485
513