Web端通过node-snap7读取西门子LOGO!模拟输入时plc.ReadArea参数错误
解决西门子LOGO!模拟输入读取错误(Wrong arguments)
错误原因分析
- ReadArea参数格式错误:你调用
plc.ReadArea时额外传入了手动创建的buffer参数,但node-snap7的异步ReadArea方法不需要提前传入buffer——回调函数的result参数会直接返回读取到的Buffer对象。多余的参数触发了Wrong arguments类型错误。 - 读取区域不匹配:西门子LOGO!的模拟输入(AI)并不存储在数据块(DB)中,而是位于输入过程映像区(PE),对应地址格式为
PIWx(例如AI2对应PIW2),因此必须使用snap7.S7AreaPE而非S7AreaDB。
修正后的代码
var snap7 = require('node-snap7'); var plc = new snap7.S7Client(); const ipAddress = '192.168.0.3'; const rack = 0; const slot = 1; plc.ConnectTo(ipAddress, rack, slot, function(err) { if (err) { return console.log('Erro:', err); } console.log('Connect ok') const start = 2; // AI2对应的PIW2起始地址 const amount = 1; // 要读取的Word数量(单个模拟输入占1个16位Word) const wordLen = snap7.S7Word; plc.ReadArea(snap7.S7AreaPE, 0, start, amount, wordLen, function(err, buffer) { if (err) { console.error('Erro', err); return; } // 读取16位无符号整数(LOGO!模拟输入的原始值) const value = buffer.readUInt16BE(); console.log('Value analog input:', value); }) })
补充说明
- LOGO!模拟输入的原始值范围通常为0-27648(对应0-10V电压或4-20mA电流信号),如需转换为实际物理量,可根据量程计算。例如4-20mA信号的转换公式:
const mA = ((value - 5529) * 16) / (27648 - 5529) + 4; console.log('Current (mA):', mA.toFixed(2)); - 确认LOGO!的网络参数(IP、rack、slot)正确,默认情况下LOGO!的slot为1,rack为0。
内容的提问来源于stack exchange,提问作者Jorge Vieira
相关产品推荐
相关产品推荐

