Jump to content

Reading MODBUS IP as TCP from multiple IP addressis?


Recommended Posts

Hey Everyone!

I have tried to find example problem as below:

Reading MODBUS IP as TCP from multiple IP addressis. My Samba 43 is MODBUS master so it is ethernet client for MODBUS slaves.

I try to make program to read Slave "A" and after it  have read correct then read slave "B". After slave "B" have read correct, read again Slave "A", newer ending loop ?

Connections for slaves its OK but I cant make the loop to work.

This is my first time to use ethernet to connect other devices with Unitronics so I'am newbie ?

Something behind my home project:
1: Reading power consuption of my home energy analyser (all three phases one by one)
2: Reading power supply of solar inverter
3: Calculate how much I can heat my boiler (Every phases have solid state relys, connected to my Samba)

Maybe someone can help me.

Best Recards:
Toni Ovaskainen
Polvijärvi city, Finland

Link to comment
Share on other sites

  • MVP 2023

Are you trying to use a single socket to talk to both slaves, or different sockets?

Also, you don't need a "loop" to read both slaves. The PLC is always running in a loop. Try creating a self-resetting timer (maybe 100ms or so, or use SB 7)  that toggles a bit. Read one slave on the positive transition of the toggled bit and the other slave on the negative transition. It's best if you use two sockets or else the timing of connecting and closing a single socket can get lengthy (on the order of 1-2 sec, or more).

Link to comment
Share on other sites

  • 1 year later...

Hi there.. I'm also using a single socket0  to read out values of 20 slaves unit with individual having an unit IP address last digit 1 to 20 for example? Can i do it by just changing the last digit of an IP address MI2243 . So do i need to close/open socket every time when i'm reading values out of it upon the complete switching? Or i just need to change the IP without toggling the socket at all? How to go about doing it?  How long it take for the values to be ready to be read upon the socket reopened? How long do i need to close the socket before i can reopen it again to read the next IP values? Any bit to confirm the values is readable before going to the next one? Any advice from you? 

Steps:

1. connect socket

2.Read unit01-192.168.1.1

3. close socket

4. connect socket

5. Read unit02-192.168.1.2

6. close socket

7. connect socket

8. Read unit03-192.168.1.3

''

''

''

9. close socket

10. connect socket

11. Read unit20-192.168.1.20

 

multiple slaves IP switching using indirect tag to change the last digit1.png

Link to comment
Share on other sites

  • MVP 2023
On 6/10/2020 at 4:23 AM, newbie88 said:

So do i need to close/open socket every time when i'm reading values out of it upon the complete switching?

My understanding of MODBUS TCP is that you MUST Connect with each device you communicate with. To connect to a different device using the same socket, you must Disconnect, re-initialize the MODBUS Configuration FB, then Connect to the new device. You have System Bits that will help you determine the status of all this, but I would use timers to be sure sufficient time has occurred for all the operations to complete. I would expect communication with 20 slaves to take perhaps a couple of minutes if you want it to be 100% reliable.

  • Like 1
Link to comment
Share on other sites

I'll tell what worked for me regarding your questions, newbie88.

Steps go as you described. Connect to 1-Read-Disconnect;  Connect to 2-Read-Disconnect... You are using TCP so connection is required. Delay of 0.4 seconds after disconnecting from one client before connecting to second one, and another 0.4 seconds before sending Modbus commands, maybe this can be shorter (probably, you should experiment). For 4 clients this cycle takes 3.9 seconds, if time is critical you may need to use more sockets or go to UniStream. For any slave that drops number of retries and timeout will add extra delay.

11 hours ago, Flex727 said:

To connect to a different device using the same socket, you must Disconnect, re-initialize the MODBUS Configuration FB, then Connect to the new device.

I'm not sure about reinitializing Configuration FB. It does have addresses for only 15 slaves, but if I remember right when you execute read command it will read from whichever slave the PLC is connected to, disregarding the "Slave ID number" that is in the read command. Maybe this was an issue from some older VisiLogic version when I encountered this for the first time. I've wanted to test this for some time now, but don't have the will.

19 hours ago, newbie88 said:

Any bit to confirm the values is readable before going to the next one?

This depends on how you make your logic. If you use that standard form from Examples

ip.PNG.26f51876a46f030b18e63bd03a1c7d9e.PNG

You can go to next step after all read bits are low for how many read commands you have. I found it works without putting delays between commands, just using "Function in Progress" bit to stop multiple read commands from interfering one with another.

Don't forget that system bit that shows if socket is connected should be a condition before blasting Modbus commands.

Hope it helps, tell us how it goes.

  • Like 1
Link to comment
Share on other sites

  • MVP 2023
3 hours ago, Isakovic said:

I'm not sure about reinitializing Configuration FB. It does have addresses for only 15 slaves,

I'm sure you're correct about not needing to reinitialize the FB in normal situations, but I assumed it would be necessary at some point if you have more than 15 slaves. I thought it would be simpler to have a single routine that repeats for each of the 20 slaves. You could do 10 and 10, but I don't think it saves any time as the FB initialization appears to complete in a single PLC scan.

  • Like 1
Link to comment
Share on other sites

39 minutes ago, Flex727 said:

I'm sure you're correct about not needing to reinitialize the FB in normal situations, but I assumed it would be necessary at some point if you have more than 15 slaves. I thought it would be simpler to have a single routine that repeats for each of the 20 slaves. You could do 10 and 10, but I don't think it saves any time as the FB initialization appears to complete in a single PLC scan.

Noted with thanks.

Link to comment
Share on other sites

On 6/11/2020 at 7:51 AM, Isakovic said:
On 6/10/2020 at 7:46 PM, Flex727 said:

To connect to a different device using the same socket, you must Disconnect, re-initialize the MODBUS Configuration FB, then Connect to the new device.

I'm not sure about reinitializing Configuration FB. It does have addresses for only 15 slaves, but if I remember right when you execute read command it will read from whichever slave the PLC is connected to, disregarding the "Slave ID number" that is in the read command. Maybe this was an issue from some older VisiLogic version when I encountered this for the first time. I've wanted to test this for some time now, but don't have the will.

So, I tested this.

One master reading 3 slaves one after another from the same Socket. No slaves were defined, every read command had Slave 0 and it worked.

Turns out you don't need to have defined slave addresses in Modbus Configuration FB. Modbus commands are sent to device which is connected at the moment of execution, no matter what is in the Slave ID field. Should we add slave addresses in Configuration FB or not (I don't see the point since connection has to be made manualy), what is a good practice here? Is it OK to communicate with more than 15 devices without reinitializing Configuration FB, like in newbie88's case?

  • Like 1
Link to comment
Share on other sites

  • MVP 2023
3 hours ago, Isakovic said:

Turns out you don't need to have defined slave addresses in Modbus Configuration FB.

Wow, this is interesting news. It seemed to me that I had problems at one time with the slave address incorrect in the Config FB, but maybe I'm misremembering. This is great information to file away for future reference. Thanks @Isakovic for taking the time to collect the data!

Link to comment
Share on other sites

  • 2 weeks later...

Hi there.. so far my socket0 is working fine when a wireless device is connected to it however I tried to implement socket1 for the same usage so as to concurrently receive data out of two sockets however the 2nd socket1 is not working. Currently i'm testing with the 1 wireless unit ip 10.1.8.11 and switching it IP to the last digit 39 to test for socket1 no matter how many time I triggered, it cannot respond with SB148 on. port number for socket0 502 & socket1 20000. Can any advice on this please??  

 

two socket0&1.png

Link to comment
Share on other sites

  • MVP 2023

To add to Flex's comments, you really don't need to cram so much into one ladder rung.  Sometimes, although it might look ok on the ladder, things won't work the way you expect due to the way the PLC interprets the info.  Splitting areas up into separate rungs often makes a project far easier to work with.

I also note in your socket init rung that you are setting SB168.  Do you know about Power Up values?  In the operand list you have one column with a power plug.  For MBs you can choose none, Reset or Set that occurs on power up.  What you are doing does the same thing, but is an awkward way of achieving it.

cheers, Aus

Link to comment
Share on other sites

3 hours ago, Ausman said:

To add to Flex's comments, you really don't need to cram so much into one ladder rung.  Sometimes, although it might look ok on the ladder, things won't work the way you expect due to the way the PLC interprets the info.  Splitting areas up into separate rungs often makes a project far easier to work with.

I also note in your socket init rung that you are setting SB168.  Do you know about Power Up values?  In the operand list you have one column with a power plug.  For MBs you can choose none, Reset or Set that occurs on power up.  What you are doing does the same thing, but is an awkward way of achieving it.

cheers, Aus

Ans: Thanks for the advice I shall separate that portion into two rungs to try out later. Cheerzz~~!!

 

can two different sockets use the same port number 502 for Modbus communication? Seem  to me 

Link to comment
Share on other sites

  • MVP 2023

No.

It's a little complicated and has to do with who is the Master and who is the Slave.

The port number must be unique to each socket.  Port 502 is the industry standard for Modbus IP but you can change that number on the Unitronics side.  The port number is local - it took me a while to get my head around this when I first started playing with Modbus IP.  Being a certified PLC dinosaur, most of my Modbus experience was with serial RTU where you don't have to worry about such things.  

But each Master socket can have a port number totally different than the port number on the device you are talking to.  Remember - the port lives on the device.  In UnitronicsLand, you can use any port number you want as long as you keep track of them.  They're like slots in a massive mailroom.

It looks like the PLC is always the Master in your application.  So...

PLC Master Socket 0 Port 502 asks for data from device 1 (socket unknown and not important) Port 502.

PLC Master Socket 1 Port 503  asks for data from device 2 (socket unknown and not important) Port 502.

I know.  Clear as mud.  And don't forget to leave port 20256 assigned to a socket so you can still talk to the PLC.

After you've beat your head long enough on a Samba spec a V700 on your next project and you'll get 8 sockets.  Way more headroom.  Or migrate to UniStream, where Modbus is easy.

I always ask people to post their code so we can have a look at things you're maybe not showing us.

Joe T.

Link to comment
Share on other sites

10 minutes ago, Joe Tauser said:

No.

It's a little complicated and has to do with who is the Master and who is the Slave.

The port number must be unique to each socket.  Port 502 is the industry standard for Modbus IP but you can change that number on the Unitronics side.  The port number is local - it took me a while to get my head around this when I first started playing with Modbus IP.  Being a certified PLC dinosaur, most of my Modbus experience was with serial RTU where you don't have to worry about such things.  

But each Master socket can have a port number totally different than the port number on the device you are talking to.  Remember - the port lives on the device.  In UnitronicsLand, you can use any port number you want as long as you keep track of them.  They're like slots in a massive mailroom.

It looks like the PLC is always the Master in your application.  So...

PLC Master Socket 0 Port 502 asks for data from device 1 (socket unknown and not important) Port 502.

PLC Master Socket 1 Port 503  asks for data from device 2 (socket unknown and not important) Port 502.

I know.  Clear as mud.

I always ask people to post their code so we can have a look at things you're maybe not showing us.

Joe T.

Ans: I shall try on port 503 for socket1 and separate them into two rungs and remove SB1 to try out. Thank you so much for your advice.~!! cheerzz~~!!

Link to comment
Share on other sites

  • MVP 2023

The way I troubleshoot this sort of problem is to save the program under a different file name, then delete all the code for the connection that works, then try the other connection to see if it works, and if not, troubleshoot that independently. Compare the part that worked with the part that doesn't work, VERY CAREFULLY. Once you get that working, add back in the code for the other device.

That said, you will get your best help if you post your program file here for us to look at it. Just pictures doesn't help as much.

Link to comment
Share on other sites

  • MVP 2023

Flex is correct.  I have often harped on the forum about the way to learn any PLC things, including Visilogic.  If you have never worked with a particular function/operation, you should play with an actual plc just experimenting with only that process until you get it exactly right, and fully understand how it works.

You then progressively build your final version, saving under progessively descriptive different names and test along the way.  Once an error appears...which it will....!!......you can find what's caused the issue very easily.

cheers, Aus

Link to comment
Share on other sites

13 hours ago, Flex727 said:

The way I troubleshoot this sort of problem is to save the program under a different file name, then delete all the code for the connection that works, then try the other connection to see if it works, and if not, troubleshoot that independently. Compare the part that worked with the part that doesn't work, VERY CAREFULLY. Once you get that working, add back in the code for the other device.

That said, you will get your best help if you post your program file here for us to look at it. Just pictures doesn't help as much.

Ans: Agree. Currently what i'm doing is to remove the faulty one and test and keep the one that is working and rebuilt and re-assign code addressing for the 2nd one again with the working codes. I shall test it individually before merging them as one however today I'm not with my controller. Maybe I will do it tomorrow. Noted with thanks.

Something I realized that only socket0 & socket2 have something to do with Modbus however socket2 seem only allow slave connection and only socket0 can be used as master client. Correct me if i'm wrong. Currently I'm replacing the 2nd one with socket2 so wonder whether visilogic can have two masters or just one? Kindly clear my doubts on the default descriptions from the each sockets. Thanks again. 

 

5.png

Link to comment
Share on other sites

3 hours ago, Ausman said:

Flex is correct.  I have often harped on the forum about the way to learn any PLC things, including Visilogic.  If you have never worked with a particular function/operation, you should play with an actual plc just experimenting with only that process until you get it exactly right, and fully understand how it works.

You then progressively build your final version, saving under progessively descriptive different names and test along the way.  Once an error appears...which it will....!!......you can find what's caused the issue very easily.

cheers, Aus

Ans: Agree. Initially I building one code only and tested successfully before going to the 2nd one however encounter problem. I'm rebuilding the 2nd one with socket2 to test it out before I merge them as one again. See how it goes. Cheers~~!!

Below my testing of the respond times to a wireless device for socket0 only .

https://www.dropbox.com/s/9ymdi5s45o9k4vs/Wireless device simulation running from slave11 to 37.mp4?dl=0

https://www.dropbox.com/s/haq72vhmarqqhow/Wireless devices connection.MOV?dl=0

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...