简体   繁体   English

节点:如何发送16位二进制信息

[英]Node: How to send 16bit binary information

I have a server program which reads binary commands from a socket. 我有一个服务器程序,该程序从套接字读取二进制命令。 The buffer that is sent to the socket needs to have its data ordered very specifically. 发送到套接字的缓冲区需要非常明确地对其数据进行排序。 This is some C-code from an example client application provided, which shows the ordering and size of the data which needs to be sent. 这是提供的示例客户端应用程序中的一些C代码,其中显示了需要发送的数据的顺序和大小。

typedef   unsigned char     u8;
typedef   unsigned short    u16;
typedef   unsigned int      u32;

typedef struct JobNodeInfo_s {
    u16  jNodeInfoType;                          /* per END device, per router, whole system */
    u8  value[32];        /* mac address / barcode value / Location  */
} JobNodeInfo_t;

typedef struct JobHdrFormat_s {
    u16                 jPreamble;
    JobCommandType_t    jCmd;
    u16                 jBatchID;       /* for logging */
    u16                 jSeq;
    u16                 priority;
    u16                 option;         /* option field could be reserved for future extension */
    JobNodeInfo_t       jNodeInfo;
    u16                 jLen;

} JobHdrFormat_t;

The JobHdrFormat_s struct is passed as a void pointer to the socket: JobHdrFormat_s结构作为空指针传递给套接字:

send(connfd, (void*)jFormat, sizeof(JobHdrFormat_t) + jFormat->jLen , 0);

Now, I need to replicate this buffer in Node.js. 现在,我需要在Node.js中复制此缓冲区。 I've looked into the Node Buffer class but have noticed it only supports Octets. 我研究了Node Buffer类,但是注意到它仅支持八位字节。 As you can see, most of the memory slots are reserved for 16-bit integers. 如您所见,大多数内存插槽都是为16位整数保留的。 Is it possible to arrange the data is Node.js the way I need like this C-code? 是否可以像我的C代码那样以Node.js的方式排列数据? I have taken a first shot at it here: 我在这里对此进行了第一枪:

var net = require( 'net' );
var client = new net.Socket();
client.connect( 13929, '192.168.2.2', function() {
    var mac_addr = "041511000960",
        outgoing_str = "FE9C00020000001100000004"
            + mac_addr
            + "0000",
        outgoing_buf = new Buffer( outgoing_str.length, "hex" );
var new_buff_arr = [];
for ( var i = 0, il = outgoing_str.length; i < il; i += 2 ) {
    var cmd_substr = "0x" + outgoing_str.substring( i, i + 2 ),
        i_cmd_substr = parseInt( cmd_substr );
        console.log( "substr: " + cmd_substr + " int: " +  i_cmd_substr );
        new_buff_arr.push(i_cmd_substr);
}
const buf = new Buffer(new_buff_arr);
    client.write( buf ,
        function() {
            console.log( "data is written" );
        }
    );
});

client.on('data', function( data ) {
    console.log('Received: ' + data.toString( "hex" ));
});

client.on('close', function() {
    console.log( 'Connection closed' );
});

More or less I've attempted to write the buffer as a string, loop through it, write each two 'bytes' as a single hex values, and then convert those back into the numbers and write the buffer out. 我或多或少尝试将缓冲区写为字符串,循环遍历,将每个两个“字节”写为一个十六进制值,然后将其转换回数字并写出缓冲区。 Is any of this possible if Node can only work with Octets? 如果Node仅能与八位位组一起使用,这有可能吗? Let me know if you need any more information. 让我知道您是否需要更多信息。

Look at this : 这个

[The] jBinary [is a] library for working with binary data in JavaScript, allows loading data from any source with automatically detected best supported way on current browser or Node.js [The] jBinary [是一个]库,用于在JavaScript中处理二进制数据,它允许在当前浏览器或Node.js上以自动检测到的最佳支持方式从任何源加载数据

And then also look at jBinary library Typesets ... 然后再看一下jBinary库的T​​ypesets ...

I have no experience with jBinary and Node.js. 我没有jBinary和Node.js的经验。 But I'm interested in such features to, and I have found this solution right now. 但是我对这些功能感兴趣,并且我现在已经找到了该解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM