traveler3468
traveler3468

Reputation: 1656

Sending TCP Packet

I'm trying to send a packet to a Tattile traffic camera.
The Tattile camera uses its own TCP packet protocol called TOP (Tattile Object Protocol)

So far from what I can see from reading the documentation I need a
IP > TCP > TOP HEADER > VCR PAYLOAD
TCP Commmunication

To create the TOP Header
Here are the requirements.

TOP HEADER
24 bytes are required I believe.

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

Header Constructor
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.

Building A Message
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
Here is the Start Engine command code.

Here is what I have code wise, so far it connects and "sends the message" however the engine doesn't start, as for the enum in the future when I get a better understanding, I should be about to add more of the commands with the command codes.

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);
        }
    }
}

Here is how I'm calling it

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

Upvotes: 2

Views: 1816

Answers (2)

Ton Plooij
Ton Plooij

Reputation: 2641

The header has a total of 24 bytes, containing 6 x 4 byte values. The first 4 bytes contain the length, being 24 (0x18). In C# these are Int32 data types, however keep in mind what the byte order is. Network protocols usually have network byte order (big indian) which is likely different from your C#. Use the System.BitConverter class to test and change if needed.

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26436

Your HeaderDimension should be a 4-byte array that contains the value 24, not a 24-byte array.

Also the Error and DataDimension should always be 4 bytes in length.

Upvotes: 1

Related Questions