[fpc-pascal] Sending Hex codes over TCP/IP

James Richters james at productionautomation.net
Sat Sep 12 01:13:38 CEST 2020


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

-----Original Message-----
From: Michael Van Canneyt <michael at freepascal.org> 
Sent: Friday, September 11, 2020 5:13 PM
To: James Richters <james at productionautomation.net>; FPC-Pascal users
discussions <fpc-pascal at lists.freepascal.org>
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.



More information about the fpc-pascal mailing list