Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write the return promise from the read register in the serial server #2780

Open
Davensi opened this issue Apr 15, 2024 · 1 comment
Open

Comments

@Davensi
Copy link

Davensi commented Apr 15, 2024

SerialPort Version

8.0.16

Node Version

18

Electron Version

No response

Platform

No response

Architecture

No response

Hardware or chipset of serialport

No response

What steps will reproduce the bug?

/* eslint-disable no-console, no-unused-vars, spaced-comment */

// create an empty modbus client
const ModbusRTU = require("modbus-serial");
// 保持寄存器
const holdingRegisters = { 4000: 31, 4001: 32, 4002: 25, 4003: 26, 4004: 27 };
// 线圈
const coils = {};
// 读输入寄存器
const inputRegisters = {};
// 离散输入 开关量输入
const discreteInputs = {};

const vector = {
// 获取单个输入寄存器
getInputRegister: function (addr) {
console.log('[ getInputRegister ] >', addr)
return inputRegisters[addr];
},
// 获取多个寄存器
// 1-That's accurate ?
getMultipleInputRegisters: function (startAddr, length) {
console.count('[ getMultipleInputRegisters ] >', startAddr, length, Date.now())
const values = [];
for (let i = 0; i < length; i++) {
values[i] = inputRegisters[startAddr + i];
}
return new Promise(reslove => reslove(values));
},
// 2-Write /async
// getMultipleInputRegisters: async function (startAddr, length) {

//     const values = [];
//     for (let i = 0; i < length; i++) {
//         values[i] = inputRegisters[startAddr + i];
//     }
//     return values
// },
// I find that neither seems to work
// 获取分离输入
getDiscreteInput: function (addr) {
    console.log('[ getDiscreteInput ] >', addr)
    return discreteInputs[addr];
},
// 获取保持寄存器
getHoldingRegister: function (addr) {
    console.log('[ getHoldingRegister ] >', addr)
    return holdingRegisters[addr];
},
// 设置单个寄存器
setRegister: function (addr, value) {
    console.log('[ 设置寄存器--setRegister ] >', addr)
    holdingRegisters[addr] = value;
    return;
},
// 获取多个保持寄存器
getMultipleHoldingRegisters: async function (startAddr, length, c) {
    const values = [];
    console.log('[ getMultipleHoldingRegisters ] >', startAddr, length, Date.now())
    for (let i = 0; i < length; i++) {
        values[i] = holdingRegisters[startAddr + i];
    }
    console.log('[ alues ] >', values, c)
    return values
},
// 获取一个线圈
getCoil: function (addr) {
    console.log('[ getCoil ] >', addr)
    return coils[addr];
},
// 设置一个线圈
setCoil: function (addr, value) {
    console.log('[ setCoil ] >', addr)
    coils[addr] = value;
    return coils[addr];
},
// 读取设备号
readDeviceIdentification: function () {
    console.log('[ readDeviceIdentification ] >')
    return {
        0x00: "MyVendorName",
        0x01: "MyProductCode",
        0x02: "MyMajorMinorRevision",
        0x05: "MyModelName",
        0x97: "MyExtendedObject1",
        0xab: "MyExtendedObject2"
    };
}

};
const serverTCP = new ModbusRTU.ServerSerial(vector,
{
port: "COM6",
baudRate: 115200,
debug: true,
unitID: 1,
// enron: true,
interval: 10,
// enronTables: {
// booleanRange: [1001, 1999],
// shortRange: [3001, 3999],
// longRange: [5001, 5999],
// floatRange: [7001, 7999]
// }
}
);

What happens?

Returning a promise should be no problem

What should have happened?

It wasn't presented as intended

Additional information

No response

@Davensi
Copy link
Author

Davensi commented Apr 15, 2024

In source code
getMultipleInputRegisters?: ((addr: number, length: number, unitID: number, cb: FCallbackVal<number[]>) => void) | ((addr: number, length: number, unitID: number) => Promise<number[]>) | ((addr: number, length: number, unitID: number) => number[]);

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

No branches or pull requests

1 participant