node.js - TCP 数据包结构
问题描述
有人可以帮我在 nodejs 中用这个数据包结构创建一个 tcp 数据包吗? 在此处输入图像描述
const net = require('net');
var createPacket = require("./MSPPacketHandler/createPacket");
var host = '192.168.4.1';
var port = 23;
let client = new net.Socket();
client.connect(port, host, () => {
var str = "";
console.log("Connected");
client.write(createPacket("Roll",0x6c,">")); //This will send the byte buffer over TCP
client.on('data',function(chunk){
str += chunk;
})
client.on('end',function(){
console.log(str);
});
})
// Packet format
// Header bytes: 0x24, 0x4d
// Direction byte: 0x3c or 0x3e
// Msg Length: 0x00 to 0xff
// Command: 0x01 to 0xff
// Payload Bytes: .....
// CRC xx
function createPacket(payload_data,command,direction){
const packetMainLength = 6; // fixed overhead of packet without payload
const payload = payload_data; // string
const packetLength = packetMainLength + payload.length;
const buffer = Buffer.alloc(packetLength, 0);
// write common MSP message header
buffer[0] = 0x24;
buffer[1] = 0x4d;
// write direction
buffer[2] = (direction == "<" ? 0x3c : 0x3e); // to the flight controller
// write payload length
buffer[3] = packetLength;
// write command
//buffer[4] = 0x6c; // pick the appropriate command to the controller
buffer[4] = command;
// put our payload string into the buffer
buffer.write(payload, 5, payload.length, 'utf8');
const afterIndex = 5 + payload.length;
// calculate CRC of direction, length and payload and put it into the packet
// after the payload
const crcStartIndex = 3;
let crc = buffer[crcStartIndex];
for (let index = 1; index < payload.length + 2; index++) {
crc = crc ^ buffer[index + crcStartIndex];
}
buffer[afterIndex] = crc;
console.log(buffer);
return buffer;
}
module.exports = createPacket;
这是创建数据包功能。它接受三个参数并返回缓冲区。我按原样发送缓冲区,是否需要将其转换为字符串?添加一些随机文本作为 stackoverflow 要求添加更多描述。
解决方案
创建一个Buffer 对象,然后您可以使用该.writeInt8()
方法将单字节值放入缓冲区的正确位置(看起来您的格式都是单字节值)或单字节值,您可以直接使用数组索引到缓冲区语法也是。
您必须手动了解哪些值进入缓冲区的哪些字节,包括有效负载长度、有效负载和有效负载的 CRC(如您的图像中所述计算)。
例如,如果您的有效负载是字符串“Hello”中的字符,您可以像这样构造该数据包:
// Packet format
// Header bytes: 0x24, 0x4d
// Direction byte: 0x3c or 0x3e
// Msg Length: 0x00 to 0xff
// Command: 0x01 to 0xff
// Payload Bytes: .....
// CRC xx
const packetMainLength = 6; // fixed overhead of packet without payload
const payload = "Hello"
const packetLength = packetMainLength + payload.length;
const buffer = Buffer.alloc(packetLength, 0);
// write common MSP message header
buffer[0] = 0x24;
buffer[1] = 0x4d;
// write direction
buffer[2] = 0x3c; // to the flight controller
// write payload length
buffer[3] = packetLength;
// write command
buffer[4] = 0x1; // pick the appropriate command to the controller
// put our payload string into the buffer
buffer.write(payload, 5, payload.length, 'utf8');
const afterIndex = 5 + payload.length;
// calculate CRC of direction, length and payload and put it into the packet
// after the payload
const crcStartIndex = 3;
let crc = buffer[crcStartIndex];
for (let index = 1; index < payload.length + 2; index++) {
crc = crc ^ buffer[index + crcStartIndex];
}
buffer[afterIndex] = crc;
console.log(buffer);
由于其中大部分对所有数据包都是通用的,因此您可以创建一个通用函数,将有效负载、命令和方向作为函数参数,然后它将为您创建包含该数据的数据包。
推荐阅读
- sql - 奇怪的交错要求
- python - 从周数获取周开始日期
- c++ - 使用 std::optional 作为常规指针与使用 has_value() 和 value
- kubernetes - 将共享卷挂载到 Kubernetes 集群,以便所有用户可以访问相同的存储和共享文件
- android - DataBindingUtil.setContentView(this, layoutId) 为空
- forms - 如何在长表单上获取 Access 2010 中的垂直滚动条?好像是个毛病
- crate - Crate.io Copy From 0 行受影响
- html - 如何向图像添加不透明叠加层,图像顶部也有文本?
- .net - .net core - 如何访问独立的网络应用程序?
- angular - 以角度显示转义的 HTML