init: PLC data collection system
This commit is contained in:
commit
ae90bc9368
38 files changed
+5234
No files matched your search
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* PLC 通信诊断工具
|
||||
* 逐步骤测试 node-snap7 库的加载和连接,帮助定位 "TypeError: Wrong arguments" 错误
|
||||
*
|
||||
* 用法:
|
||||
* npx tsx test/plc-comm.ts <IP地址> [机架号] [槽号]
|
||||
* npx tsx test/plc-comm.ts 192.168.11.111 0 1
|
||||
*/
|
||||
|
||||
import { createRequire } from 'module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
const LOG = {
|
||||
ok: (msg: string) => console.log(` ✓ ${msg}`),
|
||||
fail: (msg: string, err?: unknown) => {
|
||||
console.log(` ✗ ${msg}`)
|
||||
if (err) console.error(` ${err}`)
|
||||
},
|
||||
info: (msg: string) => console.log(` · ${msg}`),
|
||||
step: (n: number, msg: string) => console.log(`\n[步骤 ${n}] ${msg}`),
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const ip = process.argv[2]
|
||||
const rack = parseInt(process.argv[3] || '0', 10)
|
||||
const slot = parseInt(process.argv[4] || '1', 10)
|
||||
|
||||
console.log(`\n${'='.repeat(50)}`)
|
||||
console.log(` PLC 通信诊断工具`)
|
||||
console.log(`${'='.repeat(50)}`)
|
||||
console.log(` 目标 IP: ${ip || '(未指定)'}`)
|
||||
console.log(` 机架 Rack: ${rack}`)
|
||||
console.log(` 槽号 Slot: ${slot}`)
|
||||
console.log(` Node 版本: ${process.version}`)
|
||||
console.log(` 平台: ${process.platform} ${process.arch}`)
|
||||
|
||||
if (!ip) {
|
||||
console.log(`\n 用法: npx tsx test/plc-comm.ts <IP> [rack] [slot]`)
|
||||
console.log(` 示例: npx tsx test/plc-comm.ts 192.168.11.111 0 1\n`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
/* ===== 步骤 1: 加载 node-snap7 ===== */
|
||||
LOG.step(1, '加载 node-snap7')
|
||||
let snap7: any
|
||||
try {
|
||||
snap7 = require('node-snap7')
|
||||
LOG.ok(`module loaded, keys: ${Object.keys(snap7).join(', ')}`)
|
||||
} catch (err) {
|
||||
LOG.fail('加载失败', err)
|
||||
LOG.info('尝试: npm install node-snap7@1.0.9')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
/* ===== 步骤 2: 检查 S7Client ===== */
|
||||
LOG.step(2, '检查 S7Client 构造函数')
|
||||
let client: any
|
||||
try {
|
||||
client = new snap7.S7Client()
|
||||
LOG.ok('S7Client 实例化成功')
|
||||
LOG.info(`client type: ${typeof client}`)
|
||||
LOG.info(`methods: ${Object.getOwnPropertyNames(Object.getPrototypeOf(client)).slice(0, 15).join(', ')}`)
|
||||
} catch (err) {
|
||||
LOG.fail('S7Client 实例化失败', err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
/* ===== 步骤 3: 测试 ConnectTo ===== */
|
||||
LOG.step(3, `测试 ConnectTo("${ip}", ${rack}, ${slot})`)
|
||||
try {
|
||||
const result = client.ConnectTo(ip, rack, slot)
|
||||
const errText = client.ErrorText(result)
|
||||
LOG.info(`ConnectTo 返回码: ${result}`)
|
||||
LOG.info(`ErrorText: "${errText}"`)
|
||||
|
||||
if (result === 0) {
|
||||
LOG.ok('连接成功!')
|
||||
try {
|
||||
const orderCode = client.GetOrderCode()
|
||||
LOG.info(`PLC 订货号: ${orderCode}`)
|
||||
} catch { /* ignore */ }
|
||||
client.Disconnect()
|
||||
LOG.ok('已断开连接')
|
||||
} else {
|
||||
LOG.fail(`连接返回非零: ${result} ("${errText}")`)
|
||||
LOG.info('下一步将尝试 SetConnectionParams + Connect 备选方案')
|
||||
}
|
||||
} catch (err: any) {
|
||||
LOG.fail(`ConnectTo 抛异常`, err)
|
||||
LOG.info(`异常类型: ${err.constructor?.name}`)
|
||||
LOG.info(`异常消息: ${err.message}`)
|
||||
if (err.stack) {
|
||||
console.log(` Stack (首行): ${err.stack.split('\n').slice(0, 2).join(' | ')}`)
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== 步骤 4: 备选方案 SetConnectionParams + Connect ===== */
|
||||
LOG.step(4, '测试备选方案: SetConnectionParams + Connect')
|
||||
let altClient: any
|
||||
try {
|
||||
altClient = new snap7.S7Client()
|
||||
} catch (err) {
|
||||
LOG.fail('创建备选客户端失败', err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
try {
|
||||
const localTSAP = 0x0100
|
||||
const remoteTSAP = 0x0300 + rack * 0x0100 + slot
|
||||
|
||||
LOG.info(`SetConnectionParams("${ip}", ${localTSAP}, ${remoteTSAP})`)
|
||||
const setResult = altClient.SetConnectionParams(ip, localTSAP, remoteTSAP)
|
||||
LOG.info(`SetConnectionParams 返回: ${setResult}`)
|
||||
|
||||
if (setResult !== 0) {
|
||||
LOG.fail(`SetConnectionParams 非零: ${setResult} ("${altClient.ErrorText(setResult)}")`)
|
||||
} else {
|
||||
LOG.info('Connect()...')
|
||||
const connResult = altClient.Connect()
|
||||
const connErr = altClient.ErrorText(connResult)
|
||||
LOG.info(`Connect 返回: ${connResult} ("${connErr}")`)
|
||||
|
||||
if (connResult === 0) {
|
||||
LOG.ok('备选方案连接成功!')
|
||||
altClient.Disconnect()
|
||||
} else {
|
||||
LOG.fail(`备选方案连接失败: ${connResult} ("${connErr}")`)
|
||||
}
|
||||
}
|
||||
} catch (err: any) {
|
||||
LOG.fail('SetConnectionParams/Connect 抛异常', err)
|
||||
LOG.info(`异常类型: ${err.constructor?.name}`)
|
||||
LOG.info(`异常消息: ${err.message}`)
|
||||
}
|
||||
|
||||
/* ===== 步骤 5: 库版本和诊断信息 ===== */
|
||||
LOG.step(5, '诊断信息汇总')
|
||||
try {
|
||||
LOG.info(`snap7 版本: ${snap7.Snap7Version || 'N/A'}`)
|
||||
} catch { /* ignore */ }
|
||||
|
||||
const nodeMajor = parseInt(process.version.slice(1).split('.')[0], 10)
|
||||
if (nodeMajor >= 25) {
|
||||
LOG.info(`Node.js v${nodeMajor} 较新,node-snap7 可能缺少预编译绑定`)
|
||||
LOG.info(`解决方案: 参见 test/README-PLC.md`)
|
||||
}
|
||||
|
||||
const bindingPath = require.resolve('node-snap7/build/Release/node_snap7.node')
|
||||
LOG.info(`绑定文件: ${bindingPath}`)
|
||||
|
||||
console.log(`\n${'='.repeat(50)}`)
|
||||
console.log(` 诊断完成`)
|
||||
console.log(`${'='.repeat(50)}\n`)
|
||||
|
||||
// 退出码:成功连接为 0,否则为 1
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error('\n[致命错误]', err)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in new issue
Block a user