发送 TCP 数据包

Sending TCP Packet

我正在尝试向 Tattile 交通摄像头发送数据包。
Tattile 摄像头使用自己的 TCP 数据包协议,称为 TOP(Tattile 对象协议)

到目前为止,从阅读文档中我可以看到,我需要
IP > TCP > TOP HEADER > VCR PAYLOAD


创建TOP Header
这里是要求。


24 bytes are required I believe.


Here is the command header, from the image above, the Header Dimension part, is this asking for the TOP Header that required 24 bytes?


Here is the Header Constructor which I dont understand why it is there is there is already a Command Header with the same information from what I can see.


Here is an example on building a message, so for the command code at this stage until I get a better understanding, all I want to do is send data, not receive, so with that being said


这里是Start Engine命令代码。

这是我的代码明智的,到目前为止它连接并且 "sends the message" 但是引擎没有启动,至于 enum 将来当我更好地理解时,我应该添加更多带有命令代码的命令。

class Command
{
    public enum Codes
    {
        START_ENGINE
    }

    private static readonly byte[] HeaderDimension = new byte[24];
    private static byte[] CommandCode;
    private static readonly byte[] Sender = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
    private static readonly byte[] Receiver = Sender;
    private static readonly byte[] Error = new byte[] { 0 };
    private static readonly byte[] DataDimension = new byte[] {0};

    public static void Execute(Codes code)
    {
        if (code == Codes.START_ENGINE)
        {
            CommandCode = new byte[4]{ 0x35, 0x0, 0x0, 0x4};
        }

        using (TcpClient tcpClient = new TcpClient("192.168.1.21", 31000))
        {
            NetworkStream networkStream = tcpClient.GetStream();

            byte[] bytesTosend = HeaderDimension.Concat(CommandCode)
                                                .Concat(Sender)
                                                .Concat(Receiver)
                                                .Concat(Error)
                                                .Concat(DataDimension).ToArray();

            networkStream.Write(bytesTosend, 0, bytesTosend.Length);
        }
    }
}

我是这样称呼它的

static void Main()
{
    Command.Execute(Command.Codes.START_ENGINE);
    Console.ReadKey();
}

您的 HeaderDimension 应该是 包含 值 24 的 4 字节数组,而不是 24 字节数组。

此外,ErrorDataDimension 的长度应始终为 4 个字节。

header总共有24个字节,包含6 x 4字节的值。前 4 个字节包含长度,为 24 (0x18)。在 C# 中,这些是 Int32 数据类型,但请记住字节顺序是什么。网络协议通常具有网络字节顺序(大印度),这可能与您的 C# 不同。使用 System.BitConverter class 进行测试并根据需要进行更改。