modbus-connect
Version:
Modbus RTU over Web Serial and Node.js SerialPort
61 lines (54 loc) • 1.52 kB
JavaScript
// function-codes/SGM130/set-controller-time.js
const { FUNCTION_CODES } = require('../../constants/constants.js');
const toBytesLE = require('../../utils/utils.js');
const SET_CONTROLLER_TIME = FUNCTION_CODES.SET_CONTROLLER_TIME;
/**
* Формирует PDU-запрос для установки текущего времени на контроллере.
* @param {{
* seconds: number,
* minutes: number,
* hours: number,
* day: number,
* month: number,
* year: number
* }} time
* @returns {Uint8Array}
*/
function buildSetControllerTimeRequest(time) {
const {
seconds,
minutes,
hours,
day,
month,
year
} = time;
const [yearLow, yearHigh] = toBytesLE(year, 2);
return new Uint8Array([
SET_CONTROLLER_TIME,
0x00, // резерв
0x00, // резерв
seconds,
minutes,
hours,
day,
month,
yearLow,
yearHigh
]);
}
/**
* Разбирает PDU-ответ от контроллера после установки времени.
* @param {Uint8Array} pdu
* @returns {boolean}
*/
function parseSetControllerTimeResponse(pdu) {
if (!pdu || pdu.length < 1 || pdu[0] !== SET_CONTROLLER_TIME) {
throw new Error('Invalid response for Set Controller Time');
}
return true;
}
module.exports = {
buildSetControllerTimeRequest,
parseSetControllerTimeResponse
}