Modbus TCP slave invalid response

Moderator: Benj

Post Reply
TaliG
Posts: 52
Joined: Fri Apr 07, 2017 1:55 am
Location: Greece
Has thanked: 16 times
Been thanked: 14 times
Contact:

Modbus TCP slave invalid response

Post by TaliG »

Hi,

I am trying to convert a Modbus RTU serial slave flow (rpiRTU) into Modbus TCP slave (rpiTCP).

I am using EB091 plus W5500 module for slave.
Master is a raspberry Pi programed with codesys, also modbus Poll is used to poll the slave.

UART ModbusRTU (rpiRTU) works very well on real hardware.
ModbusTCP (rpiTCP) can connect to master but never returns a valid response when polled.
Master can update coils (without proper response from slave though).
Master cannot read anything from slave. (discrete inputs, analog inputs etc)

The problem is quite old as all forum posts related to ModbusTCP boil down to this exact same problem.(wrong response from slave)
viewtopic.php?f=76&t=21030&start=25#p101063
viewtopic.php?f=78&t=20201&p=95553&hili ... tcp#p95728
viewtopic.php?f=78&t=20905&p=93694&hili ... tcp#p95601
viewtopic.php?f=78&t=20201&hilit=modbus ... =25#p96469

The program reads 8 digital inputs, 2 analog inputs and sets 8 coils (outputs).
Attachments
rpiTCP.fcfx
(16.97 KiB) Downloaded 182 times
rpiRTU.fcfx
(16.03 KiB) Downloaded 171 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

Many thanks for letting us know and reminding me of this.

I'll try and replicate the problem today. It's very hectic here at the moment, trying to balance a full time job with looking after a 2 year old is not ideal.

Still i'll see if I can get this sorted for you as it certainly should be compatible with other third party Modbus TCP products.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

I've got a nice test bed up and running for this now.

I think one of the major issues was we were expecing the master to open up the TCP connection when it wanted to send a command but instead it seems to open the connection and then kept alive. This seems a tad foolish for long term communications IMO and also disables the possibility of multi master but if that's how it's done then we will have to go with that.

I've now made progress but I'll keep working on it and see if I can get it so it will work correctly with the third party controller software.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

Right I've now found and fixed the problem. While I'm here I'll make sure it's as robust as I can get it and then I'll push it to the update system.

I was forming the reply as it is done for the serial mode but in the TCP mode you are first suppost to echo back the 6 header bytes and I wasn't doing this. This explains why data read back was incorrect.

TaliG
Posts: 52
Joined: Fri Apr 07, 2017 1:55 am
Location: Greece
Has thanked: 16 times
Been thanked: 14 times
Contact:

Re: Modbus TCP slave invalid response

Post by TaliG »

Hi Benj,

great support as always.

Working from home together with your litle prince or princes is a monumental task.
I definitely wouldn't be able to do that.

I will report back as soon as I test your solution.

Thanks again for your great support.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

Should all be fixed now in the slave and the master.

The only problem that remains as far as I am aware is that if you run the Modbus TCP slave in the simulator you can connect fine the first time but if you disconnect the master then you will need to stop and restart the simulation before the connection will be allowed again.

I've added it to the list to try and investigate this but Leigh is far better at this type of stuff and so I might pass it onto him when he comes back.

I haven't tested with hardware but it should be ok. I you give it a go then please let us know how you get on.

TaliG
Posts: 52
Joined: Fri Apr 07, 2017 1:55 am
Location: Greece
Has thanked: 16 times
Been thanked: 14 times
Contact:

Re: Modbus TCP slave invalid response

Post by TaliG »

Hi Benj,

I tested today and the results are the following.

--Server: modbusPoll--
Simulation responds nicely to modbus Poll software.
So I guess the modbusTCP and netcomsComponents works as intended after the update.
On hardware, the W5500 connects but never reply to any request. modbus Poll can only update coils (without getting any reply).
Before the update, I could see an invalid response from w5500 in modbusPoll, after the update, I cannot see any response from w5500.
So now the problem should be between the netcomsComponent and the w5500.

--Server: rasberryPi and codesys--
Simulation with the rasberryPi and codesys, connects nicely, but I am getting an invalid header response error whenever I try to read from the slave. I can update coils though.
Hardware w5500 connects but does not reply at all.
One thing I noticed after the update is that the connection between hardware (w5500) and the master is a lot more stable now.
Before the update, except from invalid response errors I was getting a lot of tcp socket errors as well. Server was disconnecting and reconnecting at least 5-10 times per minute.
Now it never disconnects. That is some nice progress.

Thanks for the great support,
George

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

Thanks George,

I've got both those bits of hardware in the office so I'll grab them next time I'm in and try and debug what's happening.

TaliG
Posts: 52
Joined: Fri Apr 07, 2017 1:55 am
Location: Greece
Has thanked: 16 times
Been thanked: 14 times
Contact:

Re: Modbus TCP slave invalid response

Post by TaliG »

Hi Benj,

I tested again today modbus TCP slave simulation with modbus Poll and I think I found the problem.

Flowcode simulation always replies with incorrect message length byte.

e.g.

server request to read 2 analog inputs starting from address 0:

TX: 00 01 00 00 00 06 01 04 00 00 00 02

00 01 transaction id
00 00 protocol identifier (always 0)
00 06 message length
01 devise address
04 function code
00 address of the first byte of register Hi
00 address of the first byte of register Lo
00 number of registers Hi Byte
02 number of registers Lo Byte

values inside registers: 0=10 1=100. Flowcode simulation respond:

RX: 00 01 00 00 00 06 01 04 04 00 0A 00 64

00 01 transaction id
00 00 protocol identifier
00 06 message length <======= Incorrect, should be 00 07, 05 for one register , 09 for 3 registers and so on
01 devise address
04 function code
04 Number of bytes more
00 Register value Hi (AI0)
0A Register value Lo (AI0)
00 Register value Hi (AI1)
64 Register value Lo (AI1)

Modbus Poll will never inform about header errors . It will only inform about connection errors (timeouts etc).
This explains why I am getting invalid header response from codesys.

Here is a really nice reference on modbus tcp correct request and reply :
https://ipc2u.com/articles/knowledge-ba ... ples/#0x04

Thanks
George

TaliG
Posts: 52
Joined: Fri Apr 07, 2017 1:55 am
Location: Greece
Has thanked: 16 times
Been thanked: 14 times
Contact:

Re: Modbus TCP slave invalid response

Post by TaliG »

Hi Ben!

Can you acknowledge the "number of bytes that follow" or "message length" bug pls?
At the moment I thing you just echo those bytes from the master request which is wrong.

thanks
George

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

Hi George,

Thanks that's a great find, I'll investigate for you and see if I can spot the problem.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Modbus TCP slave invalid response

Post by Benj »

I've now made that change for you, thanks for letting us know.

Hopefully that will solve the problem but let us know how you get on.

TaliG
Posts: 52
Joined: Fri Apr 07, 2017 1:55 am
Location: Greece
Has thanked: 16 times
Been thanked: 14 times
Contact:

Re: Modbus TCP slave invalid response

Post by TaliG »

Hi Ben!

I think you nailed it (again).
I tested only with ModbusPoll but I think the response frame is correct now.
I will test with Codesys and raspberry pi and report back but I am pretty sure it is going to work fine.

Now about the hardware and the w5500 module not replying back to any request from master, I think I found the solution.
I added the "TCP_IP_W5500::TxEnd(channel 0)" component macro after a successful transaction from ModbusTCPSlave::CheckForIncoming() macro
and boom!! the module started to respond :)

I thought the Check for incoming macro was taking care all of the transaction (receive-send).
Anyway I am so happy right now. (I think I ll try to get my belly exploded from beer tonight) :)

Thanks
George
Attachments
rpiTCP.fcfx
(22.62 KiB) Downloaded 148 times

Post Reply