# Modbus TCP/IP

Hello everyone,

I try to connect arduino to another device using Modbus TCP/IP protocol. Could anyone explain to me this piece of code ?

unitId = 1

functionCode = 4

startRegister = 100

numRegister = 60

BUFFER_SIZE = 0

# Construct request packet

req = struct.pack('>3H 2B 2H', 0, 0, 6, 1, 4, 100, 60)

sock.send(req)

#Calculate receipt packet buffer and structure

BUFFER_SIZE = (32) + (31) + (int(numRegister)*2)

rec = sock.recv(BUFFER_SIZE)

num = int(numRegister / 2)

s = struct.Struct('>3H 3B %s%s' %(num,'l'))

data = s.unpack(rec)

# Concatenate each two registers

G =int(data[9])

L = int(data[13])

## P =int(data[14]) + int(data[15]) + int(data[16])

1-For the construction of the packet: '>3H 2B 2H' is it the transaction ID ?

2-I didn't understand how to Calculate receipt packet buffer and structure

3-if we supposed that "data" is all the packet , the first index of the data message will be 9 ( supposing that sizeof(int)=4 bytes) why is the index of L equal to 13 ? same question for 14,15 and 16 .

I think data is an array variable that we are filling with the content of registers , each register is 2 bytes.
And we know that the size of the data ( I mean the message) is 4 bytes.
I'm getting confused !

LamySae writes:

I'm getting confused !

You are not the only one.
With a post how you have written, I can not even makes sense of it, and I guess others will be equally confused.

First, put your code in code tags and is this part of some library code, if so, provide links and details about it.
Don't make us guess.

If it is library code, you should only need to know how to use the functions, not necessarily how they work.

Paul - VK7KPA

This code connect a database to a power controller ( the same device that I want my arduino to connect with ).

The database reads data in modbus TCP/IP .

I only want to understand this code to get an idea on how I can write an arduino code for my case.

I'm quite sure this isn't code that is expected to run on an Arduino. Please explain how this is related to the Arduino world.

This code is written to connect a database to the same device that I want to connect with my Arduino with a

modbus tcp/ip protocol. It doesn't have a relation with the arduino world.

My mission is to write another arduino code using a modbus tcp/ip protocol to connect it with this device . As I've

already have this code which does the same work, I want to understand it to get an idea on how I can transform

it to an arduino code.

In this case you should not post short excerpts of that code but either post the complete code or provide a link to it.

That's python code and it hand codes complete ModBus messages (including CRC) which is definitely nothing you should take as a good example. There are libraries for the Arduino platform that handles the ModBus TCP protocol, so you should have a register description of your device, the rest is calling the library appropriately.

pylon:
it hand codes complete ModBus messages (including CRC)

Modbus TCP doesn't use checksums

Modbus TCP doesn't use checksums

Correct, please excude my error.