node中有没有对应C中结构体变量的处理 - CNode技术社区

node中有没有对应C中结构体变量的处理
发布于 9 年前 作者 MaskWu 5198 次浏览 来自 问答

项目中需要对接受到buffer进行处理,示例C代码中有结构体变量的处理(我也把C忘得差不多了), 现在想用node对buffer进行一个响应的处理,在npm上找了一个C-struct和struct的包尝试了一下,发现读出的数据仍然是有错误,想请问下有没有人对这个处理过,处理的方法

C 代码:

//对接受的数据进行处理
struct cliprobe_data {
 unsigned char Apmac[6];
 unsigned short int Vendorid;
 unsigned short int Reserved1;
 unsigned char Bssid[6];
 unsigned char Radiotype;
 unsigned char Channel;
 unsigned char Associated;
 unsigned char Messagetype;
 char Timestamp[14];
 unsigned short int Reserved2;
 unsigned char Mutype;
 unsigned short int Reserved3;
 char Rssi;
 unsigned short int Reserved4;
 unsigned char Noisefloor;
 unsigned short int Reserved5;
 unsigned char Datarate;
 unsigned char MPDUflags;
 unsigned char Mumac[6];
 unsigned short int Framecontrol;
 unsigned short int Sequencecontrol;
 unsigned short int Reserved6;
};
package_data = (struct cliprobe_data *) buffer;//相当于格式化了buffer?

对应的C-Struct包中的声明:

var MacSchema = new _.Schema({
 Apmac:_.type.string(6),
 Vendorid:_.type.uint16,
 Reserved1:_.type.uint16,
 Bssid:_.string(6),
 Radiotype:_.type.uint8,
 Channel:_.type.uint8,
 Associated:_.type.uint8,
 Messagetype:_.type.uint8,
 Timestamp:_.string(14),
 Reserved2:_.type.uint16,
 Mutype:_.type.uint8,
 Reserved3:_.type.uint16,
 Rssi:_.type.uint8,
 Reserved4:_.type.uint16,
 Noisefloor:_.type.uint8,
 Reserved5:_.type.uint16,
 Datarate:_.type.uint8,
 MPDUflags:_.type.uint8,
 Mumac:_.type.string(6),
 Framecontrol:_.type.uint16,
 Sequencecontrol:_.type.uint16,
 Reserved6:_.type.uint16
});
1 回复

node的buffer类型数据里面就是纯粹的二进制数据,如果你已经有C结构体的定义,相当于有了一份数据解析协议,遍历这份数据,按每个字段的大小和类型去解析就可以了,具体API可以参考node Buffer相关文档。

回到顶部

AltStyle によって変換されたページ (->オリジナル) /