Nodejs - 如何充当 modbus rtu 服务器

问题描述 投票:0回答:2

我正在尝试设置一个接受 modbus RTU 轮询的侦听器。

我发现了很多与modbus相关的npm库,但它们都与其他modbus从站的轮询有关,或者充当modbus TCP服务器。我需要模拟 modbus 从站,而不是轮询器(充当 modbus RTU 服务器)

编辑:我找到了这个库https://github.com/Cloud-Automation/node-modbus

源码中有一个modbus-rtu-server.js文件,所以我一直在尝试使用这个。但是,如果没有文档,我很难理解和使用该库。这是我尝试过的:

const modbus = require('jsmodbus')
const SerialPort = require('serialport')
const options = {
    baudRate: 9600
}
const socket = new SerialPort("/dev/ttyUSB0", options)
const server = new modbus.server.RTU(socket)
server.on('connect', function (client) {
    console.log(client);
});

这是我到目前为止所拥有的,但“连接”事件从未发生,我做错了什么?

node.js npm modbus
2个回答
0
投票

服务器只是等待客户端连接。要发生 connect 事件,您必须从客户端启动查询。这意味着您需要将第二个串行端口连接到您的

/dev/ttyUSB0
并从中启动 Modbus 查询。为此,您可以使用相同的库或任何其他工具,例如 modpollQModMaster

你还需要开发一点你的服务器,因为现在它没有定义任何寄存器,所以它不会有太大用处。

查看 TCP 示例,特别是 SimpleServer。正如您所看到的,您需要在服务器上定义 Modbus 寄存器或线圈,例如:

server.on('readCoils', function (request, response, send) {
/* Implement your own */

response.body.coils[0] = true
response.body.coils[1] = false

send(response)
})

0
投票

在撰写本文时,如果

jsmodbus
modsbus.server.RTU()
包含
显式
options 选项,
undefined
库将仅调用事件处理程序:

const rtuServer = new server.RTU(
  socket,
  {
    coils: undefined,
    discrete: undefined,
    holding: undefined,
    input: undefined
  }
)

省略时,库将回退到以下缓冲区:

const DEFAULT_MODBUS_SERVER_OPTIONS: IModbusServerOptions = {
  coils: Buffer.alloc(1024),
  discrete: Buffer.alloc(1024),
  holding: Buffer.alloc(1024),
  input: Buffer.alloc(1024)
}

来源永久链接

© www.soinside.com 2019 - 2024. All rights reserved.