# Inject dynamic variables in s7 nodes

**URL:** <https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536>\
**Category:** Industrial\
**Created:** [20 February 2025 13:51 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536 "2025-02-20T13:51:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)\
**Post date:** [20 February 2025 20:00 UTC](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536/2 "2025-02-20T20:00:58Z")

</div>

Long story short - not possible with current s7 nodes.  
Only option is to use npm package nodes7.  
Check this first:

> <https://github.com/plcpeople/nodeS7/issues/129>
>
> Hey! I'm working with your node on node-red for reading from a plc S7-1200, rack… 0 and slot 1 with external functions. But every time I send a readAllItems I get the response I want but the connection resets every time and never closes, even with dropConnection and connectionCleanup... I've been trying to solve it for a few days now, if anyone has any ideas...
> 
> \`\`\`javascript
> var devices = msg.payload; // devices example: {"delta":0,"name":"10.255.3.214","rack":0,"slot":1,"data":"Q0.0","ip":"10.255.3.214","port":102}
> 
> msg = \[\];
> 
> let nodes7 = global.get('nodes7');
> var conn = new nodes7({ globalTimeout: 2000 });
> var doneReading = false;
> 
> devices.forEach((/\*\* @type {{ modbus\_id: any; modbus\_ip: any; modbus\_port: any; address: any; quantity: any; }} \*/ dev) =\> {
> var variables = {
> DATA: dev.data
> };
>    
> conn.initiateConnection({ port: dev.port, host: dev.ip, rack: dev.rack, slot: dev.slot, debug: false }, connected);
> function connected(err) {
> if (typeof (err) !== "undefined") {
> // PLC FORA DO ALCANCE
> //console.log(err);
> node.error({
> "ip": dev.ip,
> "port": dev.port,
> "message": "ERRO! Falha na leitura do host " + dev.ip + "!",
> "error": err.code
> });
> flow.set("aguarda", false);
> conn.dropConnection();
> conn.connectionCleanup();
> }
> conn.setTranslationCB(function (tag) { return variables\[tag\]; });
> conn.addItems('DATA');
> conn.readAllItems(valuesReady);
> }
> 
> function valuesReady(anythingBad, values) {
> if (anythingBad) { console.log("ERRO! Falha na leitura do host."); flow.set("aguarda", false); conn.dropConnection(); conn.connectionCleanup(); }
> //console.log(values);
> if (values.DATA != "BAD 255") {
> node.send({
> "topic": "TCP Response",
> "host": dev.ip,
> "port": dev.port,
> "rack": dev.rack,
> "slot": dev.slot,
> "data": dev.data,
> "payload": values.DATA
> });
> }
> doneReading = true;
> conn.dropConnection();
> conn.connectionCleanup();
> }
>    
>    
> });
> 
> return msg;
> 
> \`\`\`

My own examples:

> [@Siemens reading from PLC with nodeS7 npm](https://discourse.nodered.org/t/siemens-reading-from-plc-with-nodes7-npm/68926):
>
> Hello, I needed dynamic addressing of Siemens PLC, so standard NR nodes are not a case. I've imported [GitHub - plcpeople/nodeS7: Node.JS library for communication to Siemens S7 PLCs](https://github.com/plcpeople/nodeS7) as external module. Here is the simple flow which closes connection after data is read. It is necessary because S7 connections accumulate until ECONNRESET is thrown and NR needs to be restarted. My end goal was to read identical structures of data with offset for each module. Simple flow is here, PLC IP…

---

_[View the full topic](https://discourse.nodered.org/t/inject-dynamic-variables-in-s7-nodes/95536)._
