【问题标题】:TCP Packet Structure [closed]TCP 数据包结构 [关闭]
【发布时间】:2021-03-10 19:20:11
【问题描述】:

有人可以帮我在 nodejs 中使用这种数据包结构创建一个 tcp 数据包吗? enter image description here

   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 要求添加更多描述。

【问题讨论】:

    标签: node.js tcp tcpclient packet


    【解决方案1】:

    创建一个Buffer object,然后您可以使用.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);
    

    由于其中大部分对所有数据包都是通用的,因此您可以创建一个通用函数,将负载、命令和方向作为函数参数,然后它会为您创建包含该数据的数据包。

    【讨论】:

    • @VinayVinu - 这对你有用吗?
    • 我发送了数据包。它说 connect ,但没有收到任何响应。尝试发送数据包命令 - MSP_ATTITUDE (0x6c) Data - Roll DataType - INT16 Range - (-1800 - 1800) Units - (Deci degree) 文档链接 - create.dronaaviation.com/software/remote-programming/…
    • @VinayVinu - 我不认为我们可以在数据包生成方面提供任何进一步帮助,除非您将生成数据包的实际代码添加到问题的末尾(将其添加到末尾,离开原始问题完好无损)。
    • 我已经编辑了这个问题,我实际上试图与使用 multiwii 串行协议并在端口 23 上运行 tcp 服务器的无人机 (create.dronaaviation.com/software/remote-programming/…) 交互。请帮助我,我卡在这里跨度>
    • @VinayVinu - 请显示createPacket() 函数的代码。这就是我们需要看到的。
    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 2011-08-20
    • 2020-01-25
    • 2012-11-22
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多