Skip to content

Instantly share code, notes, and snippets.

@jisotalo
Last active July 10, 2021 19:39
Show Gist options
  • Save jisotalo/cfcc9e4935dfc6f391667cad2cdcb6c6 to your computer and use it in GitHub Desktop.
Save jisotalo/cfcc9e4935dfc6f391667cad2cdcb6c6 to your computer and use it in GitHub Desktop.
Reading TwinCAT routes from target system using ads-client Node.js library (https://github.com/jisotalo/ads-client)
/*
Copyright (c) 2021 Jussi Isotalo <[email protected]>
Permission 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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE 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.
*/
//Reads TwinCAT routes from target system using ads-client library
//NOTE: Install ads-client 1.11.0+ first with command "npm i ads-client"
//https://github.com/jisotalo/ads-client
const ads = require('ads-client')
const client = new ads.Client({
targetAmsNetId: 'localhost',
targetAdsPort: 851,
allowHalfOpen: true
})
client.connect()
.then(async res => {
console.log(`Connected to the ${res.targetAmsNetId}`)
//Creates a read request packet
const createPacket = (entryNumber) => {
const data = Buffer.alloc(12)
let pos = 0
//0..3 IndexGroup
data.writeUInt32LE(803, pos)
pos += 4
//4..7 IndexOffset
data.writeUInt32LE(entryNumber, pos)
pos += 4
//8..11 Read data length
data.writeUInt32LE(2092, pos)
pos += 4
return data
}
//Parses the response
const parseEntry = (data) => {
let entry = {}, pos = 0
//0..5 Target AMSNetId
entry.amsNetId = client.byteArrayToAmsNetIdStr(data.slice(pos, pos + ads.ADS.AMS_NET_ID_LENGTH))
pos += ads.ADS.AMS_NET_ID_LENGTH
//Something here (like transport type etc.) 38 bytes
entry._unknown = data.slice(pos, pos + 38)
pos += 38
//Target address (unknown number of bytes)
entry.address = ''
do {
let nextByte = data.slice(pos, pos + 1)
pos++
if (nextByte[0] == 0) {
break
} else {
entry.address += nextByte.toString('ascii')
}
}
while (true)
//Target name (unknown number of bytes)
entry.name = ''
do {
let nextByte = data.slice(pos, pos + 1)
pos++
if (nextByte[0] == 0) {
break
} else {
entry.name += nextByte.toString('ascii')
}
}
while (true)
return entry
}
//Getting all entries one by one
let entries = [], entryNumber = 0
do {
try {
const result = await client.sendAdsCommand(
ads.ADS.ADS_COMMAND.Read,
createPacket(entryNumber),
10000
)
entries.push(parseEntry(result.ads.data))
entryNumber++
} catch (err) {
if (err.adsError && err.adsErrorInfo && err.adsErrorInfo.adsErrorCode == 1814) {
//"No more notification handles" -> all entries received (not really an error)
console.log('Done!')
} else {
console.log('Error:', err)
}
//Time to quit
break
}
} while (true)
console.log(entries)
return await client.disconnect()
})
.then(() => {
console.log('Disconnected')
})
.catch(err => {
console.log('Something failed:', err)
})
@jisotalo
Copy link
Author

Updated to work with version 1.11.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment