{"id":16760390,"url":"https://github.com/roccomuso/node-ads","last_synced_at":"2025-09-14T05:31:19.413Z","repository":{"id":40589306,"uuid":"109722410","full_name":"roccomuso/node-ads","owner":"roccomuso","description":"NodeJS Twincat ADS protocol implementation","archived":false,"fork":false,"pushed_at":"2021-09-14T05:53:37.000Z","size":104,"stargazers_count":58,"open_issues_count":9,"forks_count":21,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-12-28T12:41:31.298Z","etag":null,"topics":["ads","beckhoff","node","nodejs","protocol","twincat"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roccomuso.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-06T16:44:55.000Z","updated_at":"2024-02-18T14:56:58.000Z","dependencies_parsed_at":"2022-07-13T15:59:29.242Z","dependency_job_id":null,"html_url":"https://github.com/roccomuso/node-ads","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fnode-ads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fnode-ads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fnode-ads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roccomuso%2Fnode-ads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roccomuso","download_url":"https://codeload.github.com/roccomuso/node-ads/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232954591,"owners_count":18602151,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ads","beckhoff","node","nodejs","protocol","twincat"],"created_at":"2024-10-13T04:23:21.433Z","updated_at":"2025-01-07T23:54:11.291Z","avatar_url":"https://github.com/roccomuso.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"node-ads [![NPM Version](https://img.shields.io/npm/v/node-ads.svg)](https://www.npmjs.com/package/node-ads) ![node](https://img.shields.io/node/v/node-ads.svg) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n======\n\n\n\u003e A NodeJS implementation for the Twincat ADS protocol.\n\u003e (Twincat and ADS is from Beckhoff \u0026copy;. I'm not affiliated.)\n\n### Changelog\n\n- `debug` module.\n- Added aliases: `TOD -\u003e TIME_OF_DAY` and `DT -\u003e DATE_AND_TIME`.\n- Code standardized.\n- Using `safe-buffer`.\n- When we use notification, the notification will blocked to fire if too many notifications are defined\n- `multiRead` method and read bug fix (timeout).\n- `multiRead` and `getHandels` method improved, `multiWrite` added.\n- working string length\n- array added\n\n### Requirements\n* Beckhoff PLC that has an ethernet connection and is connected to your LAN\n    * Make your you give the PLC a fixed IP address\n    * Make sure you can ping the PLC from another computer\n\n### Configuration\n\n1. Enable ADS on your PLC project. To do this click on your task and then enable the checkbox before `Create symbols` (if he is not disabled).\nIn addition, you can still, under I/O Devices click on Image and go to the ADS tab. Check the `Enable ADS Server` and also `Create symbols`.\nDownload the new configuration and make sure you reboot your PLC. The reboot is only needed when you are using TwinCat 2.\n\n2. Now add a static route to our Beckhoff PLC. The route should point to your server that will run the proxy application.\nIt's also a good idea to add an extra static route that points to your local development device. This way you can test out the proxy from your development device too.\n\n### Attention\n\n1. TwinCAT AMS Router doesn't allow multiple TCP connections from the same host. So when you use two AdsLib instances on the same host to connect to the same TwinCAT router, you will see that TwinCAT will close the first TCP connection and only respond to the newest. If you start the TwinCat System Manager and Node-Red ADS on the same PC at the same time, Node-Red will not run anymore. You can set up a second IPv4 on the PC and assign to this a ADS NET ID under Twincat\n\n2. As ADS is transmitted over a TCP connection, there is no real time guarantee.\n\n\nExamples\n--------\n\n### Hello machine\n\n\n```javascript\nvar ads = require('node-ads')\n\nvar options = {\n    //The IP or hostname of the target machine\n    host: \"10.0.0.2\",\n    //The NetId of the target machine\n    amsNetIdTarget: \"5.1.204.160.1.1\",\n    //The NetId of the source machine.\n    //You can choose anything in the form of x.x.x.x.x.x,\n    //but on the target machine this must be added as a route.\n    amsNetIdSource: \"192.168.137.50.1.1\",\n\n    //OPTIONAL: (These are set by default)\n    //The tcp destination port\n    //port: 48898\n    //The ams source port\n    //amsPortSource: 32905\n    //The ams target port for TwinCat 2 Runtime 1 \n    //amsPortTarget: 801 \n    //The ams target port for TwinCat 3 Runtime 1\n    //amsPortTarget: 851 \n    //The timeout for PLC requests\n    //timeout: 500\n    //The Local address the socket should connect from\n    //localAddress: \"192.168.137.50\"\n    //The Local port the socket should connect from\n    //localPort: 50000\n    //Version of IP stack. Must be 4, 6, or 0. The value 0 indicates that both IPv4 and IPv6 addresses are allowed. Default: 0\n    //family: 4\n}\n\nvar client = ads.connect(options, function() {\n    this.readDeviceInfo(function(err, result) {\n        if (err) console.log(err)\n        console.log(result)\n        this.end()\n    })\n})\n\nclient.on('error', function(error) {\n    console.log(error)\n})\n```\n\n### Read something\n\n```javascript\nvar myHandle = {\n    //Handle name in twincat\n    symname: '.TESTINT',  \n    //An ads type object or an array of type objects.\n    //You can also specify a number or an array of numbers,\n    //the result will then be a buffer object.\n    //If not defined, the default will be BOOL.\n    bytelength: ads.INT,  \n    //The propery name where the value should be written.\n    //This can be an array with the same length as the array length of byteLength.\n    //If not defined, the default will be 'value'.     \n    propname: 'value'      \n}\n\nvar client = ads.connect(options, function() {\n    this.read(myHandle, function(err, handle) {\n        if (err) console.log(err)\n        //result is the myHandle object with the new properties filled in\n        console.log(handle.value)\n        //All handles will be released automaticly here\n        this.end()\n    })\n})\n```\n\n### Write something\n\n```javascript\nvar client = ads.connect(options, function() {\n    myHandle.value = 5\n    this.write(myHandle, function(err) {\n        if (err) console.log(err)\n        this.read(myHandle, function(err, handle) {\n            if (err) console.log(err)\n            console.log(handle.value)\n            this.end()\n        })\n    })\n})\n```\n\n### Get notifications\n\n```javascript\nvar myHandle = {\n    symname: '.CounterTest',       \n    bytelength: ads.WORD,  \n\n    //OPTIONAL: (These are set by default)       \n    //transmissionMode: ads.NOTIFY.ONCHANGE, (other option is ads.NOTIFY.CYCLIC)\n    //maxDelay: 0,  -\u003e Latest time (in ms) after which the event has finished\n    //cycleTime: 10 -\u003e Time (in ms) after which the PLC server checks whether the variable has changed\n}\n\nvar client = ads.connect(options, function() {\n    this.notify(myHandle)\n})\n\nclient.on('notification', function(handle){\n    console.log(handle.value)\n})\n\nprocess.on('exit', function () {\n    console.log(\"exit\")\n})\n\nprocess.on('SIGINT', function() {\n    client.end(function() {\n        process.exit()\n    })\n})\n```\n\n### MultiRead something\n\n```javascript\nvar client = ads.connect(options, function() {\n    this.multiRead(\n        [{\n            symname: '.TESTBOOL',\n            bytelength: ads.BOOL,\n        }, {\n            symname: '.TESTINT',\n            bytelength: ads.UINT,\n        }],\n        function (error, handles) {\n            if (error) {\n                console.log(error)\n            } else {\n                handles.forEach(function(handle){\n                    if (handle.err) {\n                        console.error(handle.err)\n                    } else {\n                        console.log(handle.value)\n                    }\n                }\n            }\n            this.end()\n        })\n    )\n})\n```\n\n### MultiWrite something\n\n```javascript\nvar client = ads.connect(options, function() {\n    this.multiRead(\n        [{\n            symname: '.TESTBOOL',\n            bytelength: ads.BOOL,\n            value: false\n       }, {\n            symname: '.TESTINT',\n            bytelength: ads.UINT,\n            value: 5\n        }],\n        function (error, handles) {\n            if (error) {\n                console.log(error)\n            } else {\n                handles.forEach(function(handle){\n                    if (handle.err) {\n                        console.error(handle.err)\n                    } else {\n                        console.log(handle.value)\n                    }\n                }\n            }\n            this.end()\n        })\n    )\n})\n```\n\n### Get handles\n\n```javascript\nvar client = ads.connect(options, function() {\n    this.getHandles(\n        [{\n            symname: '.TESTBOOL',\n        }, {\n            symname: '.TESTINT',\n        }],\n        function (error, handles) {\n            if (error) {\n                console.log(error)\n            } else if (handles.err) {\n                console.error(handles.err)\n            } else {\n                console.log(handles)\n            }\n            this.end()\n        })\n})\n```\n\n### Get symbol list\n\n```javascript\nvar client = ads.connect(options, function() {\n    this.getSymbols(function(err, symbols, false) {\n        if (err) console.log(err)\n        console.log(JSON.stringify(symbols, null, 2))\n        this.end()\n    })\n})\n```\n\n### Get datatyp list\n\n```javascript\nvar client = ads.connect(options, function() {\n    this.getDatatyps(function(err, datatyps) {\n        if (err) console.log(err)\n        else console.log(JSON.stringify(datatyps, null, 2))\n        this.end()\n    })\n})\n```\n\n### Read device state\n\n```javascript\nvar client = ads.connect(options, function() {\n    this.readState(function(error,result) {\n      if (error) {\n        consiole.log(error)\n      } else {\n        if (result.adsState == ads.ADSSTATE.RUN) {\n          console.log('The PLC is lucky!')\n        }\n        console.log('The state is '+ads.ADSSTATE.fromId(result.adsState))\n      }\n      this.end()\n    });\n})\n```\n\nThe following states are possible:\n```javascript\n  ads.ADSSTATE.INVALID\n  ads.ADSSTATE.IDLE\n  ads.ADSSTATE.RESET\n  ads.ADSSTATE.INIT\n  ads.ADSSTATE.START\n  ads.ADSSTATE.RUN\n  ads.ADSSTATE.STOP\n  ads.ADSSTATE.SAVECFG\n  ads.ADSSTATE.LOADCFG\n  ads.ADSSTATE.POWERFAILURE\n  ads.ADSSTATE.POWERGOOD\n  ads.ADSSTATE.ERROR\n  ads.ADSSTATE.SHUTDOWN\n  ads.ADSSTATE.SUSPEND\n  ads.ADSSTATE.RESUME\n  ads.ADSSTATE.CONFIG\n  ads.ADSSTATE.RECONFIG\n  ads.ADSSTATE.STOPPING\n```\n\n### Event-Driven Detection of Changes to the Symbol Table\n\nIf the symbol table changes because, for instance, a new PLC program is written into the controller, the handles must be ascertained once again. The example below illustrates how changes to the symbol table can be detected.\n\n```javascript\n\nvar start = true;\n\nvar myHandle = {\n    indexGroup: ads.ADSIGRP.SYM_VERSION,\n    indexOffset: 0,\n    bytelength: ads.BYTE,  \n}\n\nvar client = ads.connect(options, function() {\n    start = true;\n    this.notify(myHandle);\n})\n\nclient.on('notification', function(handle){\n    if (start) {\n      console.log('symbol table version '+handle.value)\n    } else {\n      console.log('symbol table changed '+handle.value)\n    }\n    \n    start = false;\n})\n\nprocess.on('SIGINT', function() {\n    client.end(function() {\n        process.exit()\n    })\n})\n\nclient.on('error', function(error) {\n    console.log(error)\n})\n```\n\n\n### Something about the handle\n\nIf the handle remains persistent (eg as a global object), when the end() function is called the symhandle must be deleted from the handle. Otherwise, the old non-existing syshandle is used in a new connection and triggers an error.\n\n#### symname\n\nGlobal variables must start with a dot: ```.engine```\nProgram variables must start with the programname: ```MAIN.UpTyp.timerUp.PT```\n\n### Something about the bytelength\nAll possibilities of bytelength described here work with read, write, notificaton, multiread and multiwrite.\n\n\nIf you simply enter a number, reading a value returns a buffer object of length or expecting to write. This can be further processed with the standard functions of buffer.\n\nThis mehtode is called RAW read and write.\n```javascript\nvar myHandle = {\n    symname: '.TestDoubleIntStruct',  \n    bytelength: 2,  \n    propname: 'value'      \n}\nvar client = ads.connect(options, function() {\n    myHandle.value = new Buffer(4)\n    myHandle.value.writeInt16LE(5, 0)\n    myHandle.value.writeInt16LE(5, 2)\n    this.write(myHandle, function(err) {\n        if (err) console.log(err)\n        this.read(myHandle, function(err, handle) {\n            if (err) console.log(err)\n            console.log(handle.value)\n            this.end()\n        })\n    })\n})\n```\n\nThere are also ready-made objects for reading and writing numeric variables:\n```javascript\n  ads.BOOL\n  ads.BYTE\n  ads.WORD\n  ads.DWORD\n  ads.SINT\n  ads.USINT\n  ads.INT\n  ads.UINT\n  ads.DINT\n  ads.UDINT\n  ads.LINT\n  ads.ULINT\n  ads.REAL\n  ads.LREAL\n```\n\nThere are also ready-made objects for reading and writing date and time variables:\n\nWith this type it is possible to convert the time zone. There are two ways to control this feature. You can add the variable useLocalTimezone to the handle. This is true for defauld. Or you use the function ads.useLocalTimezone().\n```javascript\n  ads.TIME\n  ads.TIME_OF_DAY\n  ads.TOD // TIME_OF_DAY alias\n  ads.DATE\n  ads.DATE_AND_TIME\n  ads.DT // DATE_AND_TIME alias\n```\n\n```javascript\nvar myHandle = {\n    symname: '.TESTTIME',  \n    bytelength: ads.useLocalTimezone(ads.TIME,false),  \n    propname: 'value'      \n}\n```\n\n```javascript\nvar myHandle = {\n    symname: '.TESTTIME',\n    useLocalTimezone: false,\n    bytelength: ads.TIME,  \n    propname: 'value'      \n}\n```\n\nThe type ads.STRING is fix for 80 characters. Therefore you can use the ads.string(length) function.\n```javascript\nvar myHandle = {\n    symname: '.SOMETEXT80',  \n    bytelength: ads.STRING,  \n    propname: 'value'      \n}\n```\n\n```javascript\nvar myHandle = {\n    symname: '.SOMETEXT10',  \n    bytelength: ads.string(10),  \n    propname: 'value'      \n}\n```\n\nThere is also a possibility to read and write arrays.\nThis is done via the function ads.array(type, lowIndex, hiIndex). The low index and the hi index must be the same as in the twincat definition\nOn reading the Value is an array, on writing the Value must be an array.\nThe array under node always starts with 0, independently as loIndex and hiIndex are given.\n```javascript\nvar myHandle = {\n    symname: '.SOMEINTARREY',  \n    bytelength: ads.array(ads.INT,0,9),  \n    propname: 'value'      \n}\n```\n\nYou can also read and write structures. An array is passed for bytelength. Then an array with the same number of parameters is expected in propname.\nThe structur is mapped binary, so it must be described exactly how it exists in the PLC.\n```javascript\nvar myHandle = {\n    symname: '.SOMESTRUCTURE',  \n    bytelength: [ ads.BOOL, \n                  ads.array(ads.INT,0,9),\n                  ads.array(ads.string(10),0,9),\n                  ads.array(ads.useLocalTimezone(ads.TIME,false),0,9)\n                 ],  \n    propname: ['value_a.bool',\n               'value_a.arrayofint',\n               'value_a.arrayofstring',\n               'value_a.arrayoftime']\n}\n```\n\n\nLicense (MIT)\n-------------\nCopyright (c) 2012 Inando\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froccomuso%2Fnode-ads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froccomuso%2Fnode-ads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froccomuso%2Fnode-ads/lists"}