 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul Dolen Guest
|
Posted: Thu Mar 24, 2005 2:22 am Post subject: Newbie on TIdTcpClient - writing packets? |
|
|
I'm trying to use Indy 10, Delphi 7 Pro, for a protocol.
Specifically, I'm doing ModbusTCP. Which uses registered well-known
port 502. A number of years ago, I did a similar project under UNIX,
using UNIX sockets, and remember quite well it being rather
straightforward. Open the connection, send the bytes, receive the
bytes, close the connection. Worked without any fuss.
So, I'm trying to now use Indy, and I'm not having luck. I even
bought the e-Book Indy in Depth. Based on what I read, I believe I
don't want or need Nagle. So I set UseNagle to false on the
TIdIoHandler. Therefore, I don't think I need to do the write
buffering. I think I just want to write, and then read.
There are a ton of write methods, but I figured to keep it simple, put
the packet I want to send in a string (even though it is binary,
including nulls) and just do a write(myString). Then do a read with a
timeout. At least that is what I would think. I do a connect, then
write, and then I try a read and I get a timeout exception.
Am I doing this basically right? Any clues what my problem might be?
Thanks.
|
|
| Back to top |
|
 |
Ben Hochstrasser Guest
|
Posted: Thu Mar 24, 2005 2:44 am Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
Paul Dolen wrote:
| Quote: | Any clues what my problem might be?
|
You're coming from Unix. You expect straightforward functions. :)
Try synapse, it /is/ straightforward: http://www.ararat.cz/synapse
--
Ben
|
|
| Back to top |
|
 |
Chad Z. Hower aka Kudzu Guest
|
Posted: Thu Mar 24, 2005 2:56 am Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
Paul Dolen <nospam (AT) nowhere (DOT) com> wrote in
news:bc8441t9b4t4aoq3jrum6q8kmasmbsbpsf (AT) 4ax (DOT) com:
| Quote: | So, I'm trying to now use Indy, and I'm not having luck. I even
bought the e-Book Indy in Depth. Based on what I read, I believe I
don't want or need Nagle. So I set UseNagle to false on the
|
Generally you should NOT mess with Nagle.
| Quote: | TIdIoHandler. Therefore, I don't think I need to do the write
buffering. I think I just want to write, and then read.
|
WriteBuffering is something different.
| Quote: | There are a ton of write methods, but I figured to keep it simple, put
|
They are generaly just for type safety but most of them simply write.
| Quote: | the packet I want to send in a string (even though it is binary,
including nulls) and just do a write(myString). Then do a read with a
|
If its binary, use one of the write binary methods. Stream, or Bytes
variants.
| Quote: | timeout. At least that is what I would think. I do a connect, then
write, and then I try a read and I get a timeout exception.
|
What timeout exception and what read method are you using?
| Quote: | Am I doing this basically right? Any clues what my problem might be?
|
Mostly, but some code might help.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Need extra help with an Indy problem?
http://www.atozed.com/indy/experts/
|
|
| Back to top |
|
 |
DonS Guest
|
Posted: Thu Mar 24, 2005 3:35 am Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
| Quote: | don't want or need Nagle. So I set UseNagle to false on the
TIdIoHandler. Therefore, I don't think I need to do the write
buffering. I think I just want to write, and then read.
There are a ton of write methods, but I figured to keep it simple, put
the packet I want to send in a string (even though it is binary,
including nulls) and just do a write(myString). Then do a read with a
timeout. At least that is what I would think. I do a connect, then
write, and then I try a read and I get a timeout exception.
Am I doing this basically right? Any clues what my problem might be?
|
Not without seeing your existing code. I don't know anything about ModBus, but you could try...
// var AData: TIdBytes; AClient: TIdTCPClient;
SetLength(AData, 12);
AData[0] := $01;
AData[1] := Ord('M');
AData[1] := Ord('M');
AData[1] := Ord('M');
AData[1] := Ord('M');
AData[1] := Ord('M');
// etc...
AClient := TIdTCPClient.Create(Nil);
AClient.Host := '192.168.0.3';
AClient.Port := 502;
AClient.ConnectTimeout := 5000; // 5 secs
AClient.ReadTimeout := 5000; // 5 secs
try
try
AClient.Connect;
AClient.IOHandler.Write(AData);
AClient.IOHandler.ReadBytes(AData, Length(AData), False);
AClient.Disconnect;
except
// exception handling here
end;
finally
AClient.Free;
// AData just goes out of scope
end;
|
|
| Back to top |
|
 |
DonS Guest
|
Posted: Thu Mar 24, 2005 3:42 am Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
| Quote: | don't want or need Nagle. So I set UseNagle to false on the
TIdIoHandler. Therefore, I don't think I need to do the write
buffering. I think I just want to write, and then read.
There are a ton of write methods, but I figured to keep it simple, put
the packet I want to send in a string (even though it is binary,
including nulls) and just do a write(myString). Then do a read with a
timeout. At least that is what I would think. I do a connect, then
write, and then I try a read and I get a timeout exception.
Am I doing this basically right? Any clues what my problem might be?
|
Not without seeing your existing code. I don't know anything about ModBus, but I wouldn't use String vars for binary data. You could try...
// var AData: TIdBytes; AClient: TIdTCPClient;
// build a data packet
SetLength(AData, ;
AData[0] := #1;
AData[1] := Ord('M');
AData[2] := Ord('o');
AData[3] := Ord('d');
AData[4] := Ord('B');
AData[5] := Ord('u');
AData[6] := Ord('s');
AData[7] := #0;
// etc...
AClient := TIdTCPClient.Create(Nil);
AClient.Host := '192.168.0.3';
AClient.Port := 502;
AClient.ConnectTimeout := 5000; // 5 secs
AClient.ReadTimeout := 5000; // 5 secs
try
try
AClient.Connect;
AClient.IOHandler.Write(AData);
AClient.IOHandler.ReadBytes(AData, Length(AData), False);
AClient.Disconnect;
except
// exception handling here
end;
finally
AClient.Free;
end;
hth...
|
|
| Back to top |
|
 |
Paul Dolen Guest
|
Posted: Thu Mar 24, 2005 3:36 pm Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
I'll give this code a try, of course using the correct data. As far
as UseNagle, should that be true or false? Someone else said to
usually not mess with Nagle, which defaults to true.
| Quote: |
// var AData: TIdBytes; AClient: TIdTCPClient;
// build a data packet
SetLength(AData, ;
AData[0] := #1;
AData[1] := Ord('M');
AData[2] := Ord('o');
AData[3] := Ord('d');
AData[4] := Ord('B');
AData[5] := Ord('u');
AData[6] := Ord('s');
AData[7] := #0;
// etc...
AClient := TIdTCPClient.Create(Nil);
AClient.Host := '192.168.0.3';
AClient.Port := 502;
AClient.ConnectTimeout := 5000; // 5 secs
AClient.ReadTimeout := 5000; // 5 secs
try
try
AClient.Connect;
AClient.IOHandler.Write(AData);
AClient.IOHandler.ReadBytes(AData, Length(AData), False);
AClient.Disconnect;
except
// exception handling here
end;
finally
AClient.Free;
end;
hth...
|
|
|
| Back to top |
|
 |
DonS Guest
|
Posted: Thu Mar 24, 2005 5:37 pm Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
| Quote: | I'll give this code a try, of course using the correct data. As far
as UseNagle, should that be true or false? Someone else said to
usually not mess with Nagle, which defaults to true.
|
I"ve used Indy a long time, and I've never had to change the default setting for UseNagle.
ModBus may be a different animal, though. Take a look at the Modbus TCP/IP Implementaiton guide from www.modbus.org. This covers how to work with the Nagle algorithm etc when using modbus TCP/IP. It also covers a lot of other questiosn and options to select for TCP when using Modbus TCP/IP. There is some useful stuff at www.modbus-ida.org too; mainly the ModBus Messaging Implementation Guide in PDF form. It covers TCP-NODELAY (AKA Nagle) in section 4.3.2.
|
|
| Back to top |
|
 |
Chad Z. Hower aka Kudzu Guest
|
Posted: Thu Mar 24, 2005 5:39 pm Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
Paul Dolen <nospam (AT) nowhere (DOT) com> wrote in
news:4hn54156562ikkpfc47v077m7u512od8pb (AT) 4ax (DOT) com:
| Quote: | I'll give this code a try, of course using the correct data. As far
as UseNagle, should that be true or false? Someone else said to
usually not mess with Nagle, which defaults to true.
|
Unless you completley understand what Nagle is, you should never change it
from the default.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Want to keep up to date with Indy?
Join Indy News - it free!
http://www.atozed.com/indy/news/
|
|
| Back to top |
|
 |
Paul Dolen Guest
|
Posted: Tue Mar 29, 2005 3:54 pm Post subject: Re: Newbie on TIdTcpClient - writing packets? |
|
|
Well, I found out at least one of my problems, the device I'm talking
to actually talks "TPC encapsulated Modbus" as opposed to "ModbusTCP".
Similar idea, but slightly different packets. So, the thing would
never respond since I was sending it wrong (for it) packets. So, I
will revise my application and try it again...
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|