Yes Saragani, you were right! I was building correct packets but couldn't deliver them correctly to the PLC. And I figured out this just after "sniffing" the packets..
Let me explain why I can't send packets from Delphi to PLC:
Indy components are using blocking mechanism. Which means you write and read from a socket "synchronously". This simply just like a console application I/O.
I was using;
IOHandler.WriteLN()
method for sending my packet which accepts STRING as parameter. Therefore I was building my PCOM packet on a local Pascal string. And if you set a character value as zero it means your string is terminated. Although I was building correct packets, remote device had only seen first three characters because fourth character was always zero on the ethernet header..
To solve this problem, I did following:
MsgBufferDump: array[0..19] of byte =
(
$AA, $08, // Transaction id = 2218
101, 0, // Protocol ID : 101 = ASCII
14, 0, // Payload size = 14
47, // STX: '/'
48, 48, // unit id = 00
82, 87, // Command: 'RW' (Read MIs)
0, 0, 0, 0, // Vector base: will be filled by calling method
53, 53, // Vector length is fixed: 85 ($55)
0, 0, // checksum : will be calculated just before sending
13 // ETX: <CR>
);
Before sending, I copy this array to a stream:
sendstream:= TMemoryStream.Create;
sendstream.Write(MsgBufferDump,20);
sendstream.Position:= 0; // if you dont do this you will get an exception
IOHandler.Write(sendstream,20);
sendstream.Free;
And now it is running properly. Thank you very much for your interest.