csmark Programming in Mosaic Jun 12, 2023, 3:36 PM Jun 20, 2023, 1:34 PM

Hello,

I have run into the issue of not being able to implement Modbus TCP connections the way they worked on the older generation devices. I have followed the built-in documentation's steps of setting up the ethernet channel and function block to use the CP unit as master, but I got no response (timeout error). I have tried it in three separate networks, and none seemed to work in the long run.

Some of the networks have Mikrotik routers or switches, which may cause problems, so I've made a test network, but the TCP communication didn't work through a simple router either, nor being connected through a basic switch and nothing else. There was 0 data traffic measured, as if the control unit wouldn't send out data packets. Using a computer as a Modbus master, communcation is successful, showing that the clients and the network are working.

What can cause the problem?

Thank you in advance.

 

Below are the settings and code snippets that I used as test:

//ETH1_uni0: Receive and transmit zone size: 260; Protocol: TCP server

 

//Variables

ModbusTCPLine : fbModbusTCPmas2;

ModbusTCPCommand : ARRAY [0..30] OF TCmdStructTCP;

ModbusTCPGroup : USINT;

UINTArray : ARRAY [1..20] OF UINT;

 

//Modbus initialization with a fbModbusTCPmas2 function block

ModbusTCPLine(EN := 1,

                 GrSel := ModbusTCPGroup, //Periodically changes group number

                 MaxCmd := 30,

                 chanCode := ETH1_uni0,

                 Commands := ModbusTCPCommand[0]);

                 

//Device IP: 192.168.1.2; Unit ID set to 2; Reading 20 registers to an UINT array

ModbusCmdTCP(Gr := 2, IP := '192.168.1.2', UnitID := 2, FNC := 3, StAdr := 102, NoPoint := 20, PtrData := ADR(UINTArray), Cmd := ModbusTCPCommand[1]);

Answers 7

Luboš Urban Jun 12, 2023, 3:36 PM

Hello,

the problem is in uni channel setup. For Modbus TCP master the channel has to be set as TCP client, for Modbus TCP slave it has to be set as a TCP server because the client sends requests and commands and the server fulfils them.

csmark Jun 12, 2023, 4:02 PM

Tried it, didn't help, still no change using TCP Client protocol either.

Do I have to set the local port, or can it stay on the default value of 61000?

Luboš Urban Jun 13, 2023, 6:47 AM

The port is set according to slave device (TCP server). Usually it is used 502 as a default port for Modbus. If the server uses other port, you have to specify it on input IP address when you call ModbusTCPmaster function block (IPaddress = 'xxx.xxx.xxx.xxx:port') But it is used 502, you simply enter only IP address. In this case it doesn't matter what is written in the uni channel configuration window. Modbus function blocks replace these values.

csmark Jun 13, 2023, 9:21 AM

I have checked it, the default port for the slaves weren't changed(usually it's not even an option), the master's function block seems fine too(remoteIP checks out, remotePort = 502, localPort = 0). 

Luboš Urban Jun 19, 2023, 6:30 PM

If Modbus TCP master function block returns timeout error, it means that TCP connection is not established. In this case, the Modbus TCP master does not even send a query. It is possible when type of connection is not set properly (ie. if the type is not TCP client but TCP server, that point you have checked and repaired it) or remote port  doesn't match (you also checked and it matches) or if IP addresess are not in the same subnet. Device IP is 192.168.1.2. What is PLC IP address? Is it in 192.168.1.x subnet too? 

 

csmark Jun 20, 2023, 1:02 PM

Yes, they are both on the same subnet. The device is 192.168.1.2, the PLC is 192.168.1.3, other than that there is only a router on the 192.168.1.1 address. (Subnet mask is 255.255.255.0 for both, and gateway set to router). If I connect a computer to the network and ping the devices, all of them respond to it, frfom this I assume they should be able to contact eachother as well.

As a test I have tried to setup the ETH channel using the OpenUniSocket fucntion, but it made no difference.

Luboš Urban Jun 20, 2023, 1:34 PM

Can you send me this Mosaic project? You can send it to my e-mail urban@tecomat.cz.

Your answer

You have to be signed-in for asking a question. Continue after sign-in.