I'm building an C# library for applications who needs communication over TCP/IP using the CIP-protocol. (This is an industrial protocol, used by PLC's). My library is based on an open source VB.net project.
I found a lot of information at the Microsoft developer network to setup the socket. Next step is to register the session, so I need to receive information using the Socket.Receive method. Again, lots of useful information on the developer network.
I'm building the header like this:
private string Build_Header(byte[] Command, int Length) //Build the encapsulate message header. The header is 24 bytes fixed length and includes the command and the length of the optional data portion
{
string Header;
byte[] HeaderStatus = { 0x0, 0x0, 0x0, 0x0 };
byte[] HeaderOption = { 0x0, 0x0, 0x0, 0x0 };
try
{
Header = Encoding.Unicode.GetString(Command);
Header += Encoding.Unicode.GetString(BitConverter.GetBytes(Length));
Header += Encoding.Unicode.GetString(BitConverter.GetBytes(SessionID));
Header += Encoding.Unicode.GetString(HeaderStatus);
Header += Encoding.Unicode.GetString(BitConverter.GetBytes(Context));
Header += Encoding.Unicode.GetString(HeaderOption);
return Header;
}
catch (Exception ex)
{
Debug.WriteLine("Failed to create header: {0}", ex);
return "FAILED";
}
}
Witch is a copy of the VB function:
Private Function _build_header(Command As Byte(), length As Short)
'Build the encapsulate message header
'The header is 24 bytes fixed length, and includes the command and the length of the optional data portion.
Dim header As String
Try
header = Encoding.Unicode.GetString(Command) '# Command UINT
header += Encoding.Unicode.GetString(BitConverter.GetBytes(length)) '# Length UINT
header += Encoding.Unicode.GetString(BitConverter.GetBytes(_session)) '# Session Handle UDINT
header += Encoding.Unicode.GetString({&H0, &H0, &H0, &H0}) '# Status UDINT
header += Encoding.Unicode.GetString(BitConverter.GetBytes(_Context)) '# Sender Context 8 bytes
header += Encoding.Unicode.GetString({&H0, &H0, &H0, &H0}) '# Option UDINT
'If Not DisableLogFile Then Console.WriteLine("Header created: " + Bytes_To_String(Encoding.Unicode.GetBytes(header)) + " , length: " _
' + header.Length.ToString + " (must be equal to 24 bytes)")
Return header
Catch ex As Exception
Console.WriteLine("Failed to create header: " + ex.Message)
Return ""
End Try
End Function
But I'm receiving an error message as status "Unsupported encapsulation protocol revision." So I started debugging, line after line and found the following difference:
Original (VB):
Copy (C#):
I'm not sure if this is the main problem, but I at least it should be the same format I guess?



Header, it will display the actual (unescaped) valuestring. This data is not text.