Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
456 stars 169 forks source link

tcp server endian #301

Closed 94ljy closed 2 years ago

94ljy commented 2 years ago

sample code

import { client, server } from "jsmodbus"
import { Socket, Server } from "net"

const input = new Uint16Array(10)
const tcpServer = new Server()
const modbusServer = new server.TCP(tcpServer, {
  input: Buffer.from(input.buffer),
})

// start modbus server
tcpServer.listen(5503, "0.0.0.0", () => {
  // data init
  input[0] = 1
  input[1] = 2
  input[2] = 3

  // modbus client init
  const socket = new Socket()
  const modbusClient = new client.TCP(socket)

  socket.connect({ host: "127.0.0.1", port: 5503 }, () => {
    // read input register
    modbusClient.readInputRegisters(0, 3).then((data) => {
      console.log(data.response.body.values)
      // console.log >> [256, 512, 768]
      // input [1, 2, 3] >> [257, 513, 769] mismatch
    })
  })
})

If the server has data [1,2,3] Shouldn't it be read as [1, 2, 3] on the client as well?

Why are the endians of reading and writing different?

stefanpoeter commented 2 years ago

@94ljy Do you expect the content of data.response.body.values to be [1, 2, 3]? Remember that the input buffer on the server is a simple node.js buffer like a byte array.