Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182#!/usr/bin/env node
import process from "node:process";
function usage() { console.error(`Usage: node tools/protocol-frame.mjs read <address> <length> node tools/protocol-frame.mjs write <address> <length> <signed-value> node tools/protocol-frame.mjs write-hex <address> <declared-length> <data-hex> node tools/protocol-frame.mjs decode <frame-hex>`); process.exit(2);}
function parseInteger(value, label) { const parsed = Number(value); if (!Number.isInteger(parsed)) throw new Error(`${label} must be an integer`); return parsed;}
function commandPrefix(command, address, length) { if (address < 0 || address > 0xffff) throw new Error("address must fit in 16 bits"); if (length < 0 || length > 0xff) throw new Error("length must fit in 8 bits"); const prefix = Buffer.alloc(4); prefix.writeUInt8(command.charCodeAt(0), 0); prefix.writeUInt16BE(address, 1); prefix.writeUInt8(length, 3); return prefix;}
function signedValue(value, length) { if (![1, 2, 4].includes(length)) throw new Error("signed values support lengths 1, 2, or 4"); const buffer = Buffer.alloc(length); buffer.writeIntBE(value, 0, length); return buffer;}
function cleanHex(value) { const cleaned = value.replace(/[^0-9a-f]/gi, ""); if (cleaned.length % 2 !== 0) throw new Error("hex input must contain complete bytes"); return cleaned.toUpperCase();}
const [operation, ...args] = process.argv.slice(2);if (!operation) usage();
if (operation === "read") { const [address, length] = args.map((value, index) => parseInteger(value, index ? "length" : "address")); console.log(commandPrefix("R", address, length).toString("hex").toUpperCase());} else if (operation === "write") { const address = parseInteger(args[0], "address"); const length = parseInteger(args[1], "length"); const value = parseInteger(args[2], "value"); console.log(Buffer.concat([commandPrefix("W", address, length), signedValue(value, length)]).toString("hex").toUpperCase());} else if (operation === "write-hex") { const address = parseInteger(args[0], "address"); const length = parseInteger(args[1], "declared length"); const data = Buffer.from(cleanHex(args[2] ?? ""), "hex"); console.log(Buffer.concat([commandPrefix("W", address, length), data]).toString("hex").toUpperCase());} else if (operation === "decode") { const frame = Buffer.from(cleanHex(args[0] ?? ""), "hex"); if (frame.length < 4) throw new Error("frame must contain at least four bytes");
const length = frame.readUInt8(3); const data = frame.subarray(4); const decoded = { command: String.fromCharCode(frame.readUInt8(0)), address: frame.readUInt16BE(1), declaredLength: length, actualDataLength: data.length, dataHex: data.toString("hex").toUpperCase(), dataAscii: data.toString("latin1"), };
if ([1, 2, 4].includes(length) && data.length >= length && !["OK", "ERR"].includes(decoded.dataAscii)) { decoded.signedValue = data.readIntBE(0, length); }
console.log(JSON.stringify(decoded, null, 2));} else { usage();}