You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node.js中发送十六进制指令控制优派投影仪开关机

优派投影仪RS232指令控制脚本正确性疑问

我有一台优派(ViewSonic)投影仪,尝试发送RS232指令来控制其开关机。
开机指令如下:

0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D

关机指令如下:

0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x01 0x00 0x5E

以下是我编写的Node.js脚本:

const net = require('net');
const ping = require('ping');

const optomaCommands = {
    onCommand: "~0000 1\r\n",
    offCommand: "~0000 2\r\n"
};

const viewSonicCommands = {
    onCommand: Buffer.concat([
        Buffer.from([0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D]),
        Buffer.from('\r\n')
    ]),
    offCommand: Buffer.concat([
        Buffer.from([0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x01, 0x00, 0x5E]),
        Buffer.from('\r\n')
    ])
};

const projectors = [
    {
        name: 'Optoma_1',
        ip: 'xxx.xxx.x.xxx',
        port: 23,
        ...optomaCommands,
    },
    {
        name: 'viewSonic_pj',
        ip: 'xxx.xxx.x.xxx',
        port: 23,
        ...viewSonicCommands,
    }
];

async function pingProjectors() {
    for (const projector of projectors) {
        const res = await ping.promise.probe(projector.ip);
        if (res.alive) {
            console.log(`Projector ${projector.name} at ${projector.ip} is reachable.`);
        } else {
            console.log(`Projector ${projector.name} at ${projector.ip} is not reachable.`);
        }
    }
}

function controlProjector(projector, command) {
    const client = new net.Socket();

    client.connect(projector.port, projector.ip, () => {
        console.log(`Connected to ${projector.name}`);
        client.write(command);
    });

    client.on('data', data => {
        console.log(`Received from ${projector.name}: ${data.toString('hex')}`);
        client.destroy();
    });

    client.on('close', () => {
        console.log(`Connection to ${projector.name} closed`);
    });

    client.on('error', function(err) {
        console.log('An error occurred:', err);
    });
}

// Turn off projectors
function turnOffAllProjectors() {
    for (const projector of projectors) {
        controlProjector(projector, projector.offCommand);
    }
}

// Turn on projectors
function turnOnAllProjectors() {
    for (const projector of projectors) {
        controlProjector(projector, projector.onCommand);
    }
}

// turnOffAllProjectors();

turnOnAllProjectors();

// pingProjectors();

奥图码投影仪的指令可正常工作,但不确定优派的指令使用方式是否正确,恳请帮忙排查。


问题分析与解决方案

核心问题点

你当前的优派指令实现存在一个关键错误:额外拼接了\r\n换行符。

优派提供的开关机指令是完整的二进制帧(包含校验位0x5D/0x5E),这类RS232 over TCP的二进制控制协议通常不需要额外添加换行符作为结束标志——奥图码的指令是ASCII格式,依赖\r\n标识指令结束,但优派的二进制指令帧长度固定、结构完整,额外添加的\r\n会破坏指令完整性,导致投影仪无法正确解析。

修正后的指令定义

移除多余的Buffer.concat和\r\n,直接使用原始二进制数组:

const viewSonicCommands = {
    onCommand: Buffer.from([0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D]),
    offCommand: Buffer.from([0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x01, 0x00, 0x5E])
};

其他优化建议

  1. 验证指令有效性:先用串口调试工具(如Putty、SSCOM)直接发送原始二进制指令到投影仪TCP端口23,确认指令本身是否有效,排除硬件或网络连接问题。
  2. 添加超时处理:当前controlProjector函数无超时机制,若投影仪无响应,连接会一直挂起,可补充超时逻辑:
    function controlProjector(projector, command) {
        const client = new net.Socket();
        const timeoutTimer = setTimeout(() => {
            console.log(`Connection to ${projector.name} timed out`);
            client.destroy();
        }, 5000); // 5秒超时
    
        client.connect(projector.port, projector.ip, () => {
            console.log(`Connected to ${projector.name}`);
            client.write(command);
        });
    
        client.on('data', data => {
            clearTimeout(timeoutTimer);
            console.log(`Received from ${projector.name}: ${data.toString('hex')}`);
            client.destroy();
        });
    
        client.on('close', () => {
            clearTimeout(timeoutTimer);
            console.log(`Connection to ${projector.name} closed`);
        });
    
        client.on('error', function(err) {
            clearTimeout(timeoutTimer);
            console.log(`Error with ${projector.name}:`, err);
        });
    }
    
  3. 解析响应数据:优派投影仪可能返回二进制响应,可对照官方文档解析响应内容,判断指令执行是否成功。

内容的提问来源于stack exchange,提问作者ksatione

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 03:24:51