Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-09

Sending Hex codes over TCP/IP

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of hex codes over TCP/IP, but everything I find seems to want to send and receive strings. Can someone please point me in the right direction on how to do this?

Basically I want to make a connection to an IP address at a specific port and then send some bytes to the server then get some bytes back from the server. The data sent is just hexadecimal and it can't be followed by linefeeds or carriage returns, and I want to just receive the bytes back into a buffer of some sort so I can look at it one byte at a time. I prefer some kind of array of bytes so I can just access the bytes with elements of the array.

I've been going round and round trying to figure this out. Any help is greatly appreciated

James

Michael Van Canneyt
to James Richters
James Richters wrote 5 lines
I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of hex codes over TCP/IP, but everything I find seems to want to send and receive strings. Can someone please point me in the right direction on how to do this? Basically I want to make a connection to an IP address at a specific port and then send some bytes to the server then get some bytes back from the server. The data sent is just hexadecimal and it can't be followed by linefeeds or carriage returns, and I want to just receive the bytes back into a buffer of some sort so I can look at it one byte at a time. I prefer some kind of array of bytes so I can just access the bytes with elements of the array. I've been going round and round trying to figure this out. Any help is greatly appreciated

You'll need to be more specific. What classes or routines are you using (or trying to use) for this ?

Michael.

Tony Whyman
to James Richters: I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of…

A TCP connection is no more than a pair of byte streams - one in each direction. You have to define your own structure for each byte stream and the procedures for use i.e. a protocol. lt will be easier if you can use a standard protocol such as http. An http POST is one way to send an array of bytes to a server and to receive a response. if you want to define your own protocol then you could send your array of bytes as an integer length followed by the bytes encoded one after the other. If you want your protocol to be platform independent then be careful to define the bit order (little endien or big endien) and how multibyte integers are encoded (low order byte first or high order byte first).Your protocol could be as simple as one side iniiates a connection, sends a byte count followed by the byte array. The receiver, once it has received all bytes (as given by the byte count) processes the data and then returns the response preceded by a byte count. Of course your application may be more complex than that, which is why protocol design is such an interesting problem. -------- Original message --------From: James Richters via fpc-pascal Date: 11/09/2020 21:59 (GMT+00:00) To: 'FPC-Pascal users discussions' Cc: James Richters Subject: [fpc-pascal] Sending Hex codes over TCP/IP I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of hex codes over TCP/IP,  but everything I find seems to want to send and receive strings.  Can someone please point me in the right direction on how to do this? Basically I want to make a connection to an IP address at a specific port and then send some bytes to the server then get some bytes back from the server.  The data sent is just hexadecimal and it can't be followed by linefeeds or carriage returns, and I want to just receive the bytes back into a buffer of some sort so I can look at it one byte at a time.  I prefer some kind of array of bytes so I can just access the bytes with elements of the array.  I've been going round and round trying to figure this out.  Any help is greatly appreciatedJames_______________________________________________fpc-pascal maillist  - 

Bo Berglund
to James Richters: I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of…
James Richters wrote 6 lines
I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of hex codes over TCP/IP, but everything I find seems to want to send and receive strings. Can someone please point me in the right direction on how to do this? Basically I want to make a connection to an IP address at a specific port and then send some bytes to the server then get some bytes back from the server. The data sent is just hexadecimal and it can't be followed by linefeeds or carriage returns, and I want to just receive the bytes back into a buffer of some sort so I can look at it one byte at a time. I prefer some kind of array of bytes so I can just access the bytes with elements of the array. I've been going round and round trying to figure this out. Any help is greatly appreciated

If you use Indy10 you will find that TIdTcpClient has methods to read and send the data as TBytes, i.e. array of bytes.

Bo Berglund Developer in Sweden
James Richters
to Michael Van Canneyt: You'll need to be more specific. What classes or routines are you using (or trying to…

What I'm trying to do is control some different ethernet relays. They use MODBUS TCP. I'm trying to keep it as simple as possible. I found a modbus library for Pascal but it was way over complicated for that I need to do, and while it looks like it would do some cool stuff, the one function I need wasn't implemented yet.

I don't need any classes and the only functions I need are:
1. Make the connection to the IP address and port
2. Send 11 bytes
3. Receive 11 bytes
4. close the connection when I'm done sending and receiving bytes

The protocol is already determined by the device, so I know exactly what bytes I need to send to turn a relay on or off, and I know what to expect back if it was successful. I don't need any threads or anything complicated.. or any high speed performance... just send some bytes, and read some bytes.

So I need to establish a connection to 10.10.02.02 at TCP Port 502
Then I want to send this:
    Bytesout[0]:=  $00;   // High Byte of transaction ID Word
    Bytesout[1]:=  $01;   // Low Byte of transaction ID Word
    Bytesout[2]:=  $00;  //High Byte of Protocol ID (Always $00)
    Bytesout[3]:=  $00;  //Low Byte of Protocol ID (Always $00)
    Bytesout[4]:=  $00;   // High Byte of Message Length Word
    Bytesout[5]:=  $06;   // Low Byte of Message Length Word  (6 Bytes to
follow)
    Bytesout[6]:=  $FE;   // Unit ID (hard coded to $FE)
    Bytesout[7]:=  $05;  // Function Code  $05=Write to Coil
    Bytesout[8]:=  $00;  // Hight Byte of Coil Number ($00)
    Bytesout[9]:=  $02;   // Low Byte of Coil Number ($01 to $08 for this
device)
    Bytesout[10]:=  $FF;  // High Byte of Status ($FF to turn on,  $00 to
turn off)
    Bytesout[11]:=  $00; // Low Byte of Status (always $00 for this
operation)

If the operation is successful, I will receive the exact same thing back, After the relay is activated. My program doesn't need to do anything else until the bytes are received, or a timeout occurs. This is going to be a FPC console application that will only need to run on Window 10. I'm wanting to keep it simple and lightweight. I don't need to do anything else but send and receive theses 11 bytes.... nothing else is supported by the device anyway.

Any ideas?

James

Earlier message 32 lines
-----Original Message----- From: Michael Van Canneyt Sent: Friday, September 11, 2020 5:13 PM To: James Richters ; FPC-Pascal users discussions Subject: Re: [fpc-pascal] Sending Hex codes over TCP/IP On Fri, 11 Sep 2020, James Richters via fpc-pascal wrote: > I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of hex codes over TCP/IP, but everything I find seems to want to send and receive strings. Can someone please point me in the right direction on how to do this? > > Basically I want to make a connection to an IP address at a specific port and then send some bytes to the server then get some bytes back from the server. The data sent is just hexadecimal and it can't be followed by linefeeds or carriage returns, and I want to just receive the bytes back into a buffer of some sort so I can look at it one byte at a time. I prefer some kind of array of bytes so I can just access the bytes with elements of the array. > > I've been going round and round trying to figure this out. Any help > is greatly appreciated You'll need to be more specific. What classes or routines are you using (or trying to use) for this ? Michael.
James Richters
to LacaK

Thanks! That is exactly what I’ve been trying to figure out. Just one simple unit, not a whole huge package that is so complicated I can’t follow it. Your MODBUS TCP implementation is very easy for me to follow and to see what’s going on. I wrote my own MODBUS RTU unit, so I’m familiar with the process. I don’t need a whole lot of functionality or any of the really advanced things that could be done with MODBUS, I just want to turn some relays on and off and check their status. This will work perfectly for that.

Thank you very much, this will help greatly!

James

Earlier message 28 lines
From: fpc-pascal On Behalf Of LacaK via fpc-pascal Sent: Sunday, September 13, 2020 5:56 AM To: ; FPC-Pascal users discussions Cc: LacaK Subject: Re: [fpc-pascal] Sending Hex codes over TCP/IP Dňa Fri, 11 Sep 2020 19:13:38 -0400 James Richters via fpc-pascal napísal: > What I'm trying to do is control some different ethernet relays. > They use > MODBUS TCP. I'm trying to keep it as simple as possible. I found >a modbus > library for Pascal but it was way over complicated for that I need >to do, > and while it looks like it would do some cool stuff, the one >function I > need wasn't implemented yet. > You can look at my simple MODBUS TCP implementation ... http://lacak.users.sourceforge.net/freepascal.html I think that it is simple/straightforward to understand. If there is something not implemented yet I can add it ... (I just implemented what I needed for my work project) -Laco.
Brian
to James Richters: I'm trying to figure out how to send and receive Arrays of Bytes or perhaps a buffer of…

Use Ararat Synapse .

http://synapse.ararat.cz/doc/help/

Unit blcksock Class TTCPBlockSocket

Then send and receive byte arrays using :

Public 	function SendBuffer(Buffer: TMemory; Length: Integer): Integer;
override;
Public 	function RecvBuffer(Buffer: TMemory; Len: Integer): Integer;
override;
var
 Tx_buffer : array[0..n] of byte;
SentN := SendBuffer(@Tx_Buffer,NumBytes);