modbus-connect
Version:
Modbus RTU over Web Serial and Node.js SerialPort
35 lines (27 loc) • 1.08 kB
JavaScript
// function-codes/SGM130/closeFile.js
const FUNCTION_CODE = 0x57;
const EXPECTED_RESPONSE_SIZE = 1;
function buildCloseFileRequest() {
// Используем статический Uint8Array для избежания лишних аллокаций
const request = new Uint8Array(1);
request[0] = FUNCTION_CODE;
return request;
}
function parseCloseFileResponse(response) {
if (!(response instanceof Uint8Array)) {
throw new TypeError('Response must be Uint8Array');
}
const responseLength = response.length;
if (responseLength === 0) {
console.warn('⚠️ Empty response for Close File command (0x57)');
return false;
}
if (responseLength !== EXPECTED_RESPONSE_SIZE || response[0] !== FUNCTION_CODE) {
throw new Error(`Invalid response: expected [0x${FUNCTION_CODE.toString(16)}], got [${Array.from(response).map(b => '0x' + b.toString(16)).join(', ')}]`);
}
return true;
}
module.exports = {
buildCloseFileRequest,
parseCloseFileResponse
};