UNPKG

modbus-connect

Version:

Modbus RTU over Web Serial and Node.js SerialPort

70 lines (57 loc) 2.42 kB
// function-codes/SGM130/read-device-comment.js const { FUNCTION_CODES } = require('../../constants/constants.js'); const { allocUint8Array, isUint8Array } = require('../../utils/utils.js'); /** Таблица символов по спецификации */ const SYMBOL_MAP = { 0: ' ', 1: '0', 2: '1', 3: '2', 4: '3', 5: '4', 6: '5', 7: '6', 8: '7', 9: '8', 10: '9', 11: 'A', 12: 'B', 13: 'C', 14: 'D', 15: 'E', 16: 'F', 17: 'G', 18: 'H', 19: 'I', 20: 'J', 21: 'K', 22: 'L', 23: 'M', 24: 'N', 25: 'O', 26: 'P', 27: 'Q', 28: 'R', 29: 'S', 30: 'T', 31: 'U', 32: 'V', 33: 'X', 34: 'Y', 35: 'Z', 37: 'А', 38: 'Б', 39: 'В', 40: 'Г', 41: 'Д', 42: 'Е', 43: 'Ж', 44: 'З', 45: 'И', 46: 'Й', 47: 'К', 48: 'Л', 49: 'М', 50: 'Н', 51: 'О', 52: 'П', 53: 'Р', 54: 'С', 55: 'Т', 56: 'У', 57: 'Ф', 58: 'Х', 59: 'Ц', 60: 'Ч', 61: 'Ш', 62: 'Щ', 63: 'Ъ', 64: 'Ы', 65: 'Ь', 66: 'Э', 67: 'Ю', 68: 'Я' }; /** * Создаёт PDU-запрос для чтения комментария устройства * @param {number} channel - номер канала (0–255) * @returns {Uint8Array} */ function buildReadDeviceCommentRequest(channel) { if (channel < 0 || channel > 255) { throw new RangeError('Invalid channel number'); } const pdu = allocUint8Array(2); pdu[0] = FUNCTION_CODES.READ_DEVICE_COMMENT; pdu[1] = channel; return pdu; } /** * Парсит ответ от устройства, возвращает строку комментария и байты * @param {Uint8Array} pdu * @returns {{ channel: number, raw: number[], comment: string }} */ function parseReadDeviceCommentResponse(pdu) { if (!isUint8Array(pdu)) { throw new TypeError('PDU must be Uint8Array'); } const fc = FUNCTION_CODES.READ_DEVICE_COMMENT; if (pdu[0] !== fc) { throw new Error(`Invalid function code: expected 0x${fc.toString(16)}, got 0x${pdu[0].toString(16)}`); } const channel = pdu[1]; const length = pdu[2]; if (length > 16) { throw new Error(`Invalid string length: ${length}`); } const data = []; for (let i = 0; i < length; i++) { data.push(pdu[3 + i]); } const comment = data.map(b => SYMBOL_MAP[b] || '').join(''); return { channel, raw: data, comment }; } module.exports = { buildReadDeviceCommentRequest, parseReadDeviceCommentResponse }