const net = require('net'); // ====== 配置区(设备客户端模拟)====== const HOST = '127.0.0.1'; // 服务端地址(联调用本机) const PORT = 8234; // 服务端端口 // ====================================== // 指令(来自服务端) const ACTIVATE_CMD_HEX = '16 2f 40 31 04 03'; const GET_DATA_CMD_HEX = '16 31 3f 43 44 04 03'; // 设备回传固定数据帧(收到获取数据指令后返回) const RESPONSE_HEX = '16 31 5F 43 44 02 6B 42 56 2B 30 30 36 41 2D 30 30 32 55 2B 30 38 37 42 30 30 30 30 53 30 33 36 35 54 30 33 32 35 43 30 31 30 35 52 30 30 30 30 47 30 30 30 30 50 30 30 30 30 48 30 30 30 30 46 35 58 30 30 30 30 4D 30 30 30 30 30 30 49 30 30 30 4C 34 39 35 33 52 34 37 31 36 32 30 5A 2B 30 30 30 4E 31 34 30 30 42 2B 30 51 30 30 30 59 30 30 30 49 37 56 43 41 30 59 38 32 53 30 30 30 50 30 30 52 30 30 30 42 30 30 30 30 56 30 30 30 30 42 32 33 37 33 37 38 30 53 32 45 31 46 42 38 37 33 37 31 38 31 46 31 46 30 30 30 59 30 30 30 30 35 35 06 03'; // ===== 工具函数 ===== function hexToBuffer(hexStr) { const hex = hexStr.replace(/\s+/g, ''); const bytes = []; for (let i = 0; i < hex.length; i += 2) { bytes.push(parseInt(hex.substr(i, 2), 16)); } return Buffer.from(bytes); } function bufferToHex(buf) { return buf.toString('hex').replace(/(.{2})/g, '$1 ').trim().toUpperCase(); } const ACTIVATE_CMD = hexToBuffer(ACTIVATE_CMD_HEX); const GET_DATA_CMD = hexToBuffer(GET_DATA_CMD_HEX); const RESPONSE_BUF = hexToBuffer(RESPONSE_HEX); let socket; let rxBuffer = Buffer.alloc(0); function initClient() { socket = new net.Socket(); socket.on('connect', () => { console.log(`✅ 设备客户端已连接到服务端: ${HOST}:${PORT}`); socket.setKeepAlive(true, 10000); }); socket.on('data', (chunk) => { rxBuffer = Buffer.concat([rxBuffer, chunk]); console.log(`📥 收到服务端数据 (${chunk.length} bytes): ${bufferToHex(chunk)}`); let progressed = true; while (progressed) { progressed = false; const idxActivate = rxBuffer.indexOf(ACTIVATE_CMD); if (idxActivate !== -1) { // 收到激活指令:不返回,仅移除 console.log(' 🔸 收到 激活指令(设备端不回应)'); rxBuffer = Buffer.concat([ rxBuffer.slice(0, idxActivate), rxBuffer.slice(idxActivate + ACTIVATE_CMD.length) ]); progressed = true; continue; } const idxGetData = rxBuffer.indexOf(GET_DATA_CMD); if (idxGetData !== -1) { console.log(' 🔹 收到 获取治疗数据指令(设备端回传数据帧)'); rxBuffer = Buffer.concat([ rxBuffer.slice(0, idxGetData), rxBuffer.slice(idxGetData + GET_DATA_CMD.length) ]); socket.write(RESPONSE_BUF); console.log(`📤 已发送数据帧 (${RESPONSE_BUF.length} bytes)`); progressed = true; continue; } } if (rxBuffer.length > 1024 * 1024) { console.warn('⚠️ 接收缓冲区异常增长,清空'); rxBuffer = Buffer.alloc(0); } }); socket.on('error', (err) => { console.error('❌ 设备客户端错误:', err.message); }); socket.on('close', () => { console.log('🔌 与服务端连接断开,3秒后重连...'); setTimeout(connectServer, 3000); }); connectServer(); } function connectServer() { try { console.log(`🔗 正在连接服务端 ${HOST}:${PORT} ...`); socket.connect(PORT, HOST); } catch (err) { console.error('❌ 连接失败:', err.message); setTimeout(connectServer, 3000); } } function shutdown() { console.log('\n🛑 设备客户端正在关闭...'); if (socket) socket.destroy(); process.exit(0); } console.log('🩺 Fresenius 4008S 设备客户端模拟器启动'); console.log(`🌐 目标服务端: ${HOST}:${PORT}`); initClient(); process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown);