I need to understand the physical Modbus network design - modbus

I am new to Modbus and I want to understand how the physical network can be set up.
Currently I register Modbus TCP devices as slaves one by one in this C1 device.
This works on ethernet, but I am not sure if I can communicate/discover RTU devices in device C1.
Is it possible via any gateway or router ?
If yes how will be the design of that network ?
Currently what I understand is like this,
Modbus TCP client (C1) <-> Modbus TCP gateway <-> Modbus TCP slaves/clients

Related

Is it possible to connect to Modbus TCP via Ethernet?

Is it possible to connect the Ethernet port (of a Raspberry Pi) directly to a Modbus TCP RJ45 port (such that the devices can talk to each other)? Or is this not possible without a converter?
I am unsure if this is the correct forum, but I believe this should not be specific to the Raspberry pi.
Short answer - Yes... But....
As per the comments this is possible but there are a few things you will need to do (i.e. some configuration will be needed).
I think it's worth nothing that "Modbus TCP RJ45 port" is not really a meaningful term. Modbus is an application layer protocol; this depends upon a number of underlying layers:
Transport layer - TCP
Network layer - IP
Datalink Layer - Ethernet
Physical Layer - Ethernet cable with RJ45 connectors
You don't need to understand this in detail; the point is that before ModbusTCP will work you need to have a working TCP network (which all Modbus-TCP devices will support; generally via an RJ45 Ethernet connection). As such a better question probably is "If I run a CAT-5 cable between a Raspberry Pi and another device (Modbus TCP unit) will I be able to connect via TCP?" (a lot more people know about TCP/IP networking than Modbus!).
The first thing to consider is Ethernet. Running a cable directly between two older devices will often not work because they needed a crossover cable. Almost all modern equipment (including the Pi) supports Auto MDI-X which means the cable will just work. You can also connect the units via a switch (and doing this removes the need for Auto MDI-X).
Next you need to consider the IP layer. When you connect your Pi to your home network it will (usually!) be given an IP address by a DHCP service (usually running on your router). If you are connecting the Pi directly to the device then there will be no DHCP service so you will need to manually assign IP addresses to the devices (and ensure the subnet is correctly configured). A common way to check if an IP connection is working is to use the ping command.
With the lower layers working ModbusTCP will generally 'just work'. Many ModbusTCP devices also offer a browser based configuration and checking that you can access that is a good way to confirm that the network link is working.
One further question is probably "should I do this"; it's OK to hook things up this way to make some quick changes. However generally you will want the Pi to access other network resources so connecting everything to a router (home router will work; for remote devices a cell router is often used). You can either give the Modbus unit a static IP manually or use the routers configuration pages to assign it a static DHCP lease (otherwise it's IP might change from time to time).

UDP broadcast for IoT discovery using Raspberry PI and public wifi

A couple questions.
TLDR is sending UDP broadcast packets on a Wifi network to allow for discovery of a IoT device (Raspberry PI) a conventional practice?
Long version: I'm working on an IoT project for a class. Based on other IoT devices I've used (a Christmas carol lighting system and music player), we decided to utilize an already working application (https://github.com/balena-os/wifi-connect). The way it works is it runs on the Raspberry PI and hosts a wifi endpoint through the RPI's network card that the user can connect to. Once a user connects to this Wifi endpoint on his/her Iphone, a Wifi selection and password entry page appears on his/her Iphone. The user enters the Wifi/password that the IoT device (Raspberry PI) should connect to. The entered Wifi will then be connected to by the Raspberry PI. The user can then connect to the entered Wifi as well and now both the user's Iphone and Raspberry PI will be connected to the same Wifi network.
We decided to send UDP packets to the broadcast address of the Wifi network from the IoT device, so that users connected to the Wifi (via their Iphones) can "discover" the IoT devices IP address by listening for UDP packets. Once the user discovers the IoT device's IP address, they can send HTTP RESTful API calls to the IoT device. I was wondering if the described process is conventionally used.
# script we are using to send UDP broadcast packets
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server.settimeout(0.2)
message = b"message"
while True:
server.sendto(message, ('<broadcast>', 16123))
time.sleep(1)
We tried this setup at a public library, using their free public wifi. However, the UDP broadcast packet discovery process failed. UDP broadcast packets were sent out by the IoT device, but were not received by the Iphone. We're wondering if this has something to do with a firewall. We could successfully send UDP broadcast packets from a Macbook (via a python script) to an IOS emulator residing on the same Macbook, such that the source IP address of the packet was the same as the recipient. Is there a firewall rule such that a broadcast packet sent from IP address [A] can be delivered back to IP address [A] but not to IP address [B].
Also, I didn't mess around with the port number, which might have helped if certain ports were disabled by a firewall. I'm not sure. Help is appreciated. We are both relatively new to this.
DNS-SD (RFC6763) is the a fairly common and standardized way to do network discovery. It actually supports both TCP & UDP services. It was originally invented by Apple under the brand name "Bonjour" for printer discovery.
So yes, it is common to use UDP broadcast packets (via DNS-SD) for IoT device discovery on a Wifi network.

Is it possible to simulate a rs485 slave using pymodbus?

I've been using node-red to simulate TCP and RTU slaves on a raspberry pi and a dell gateway. So far, I've managed to get the TCP slave working, but it doesn't seem possible to do it via rs485 on node-red.
Is it possible to use pymodbus running on a raspberry pi connected to a dell gateway via rs485 to simulate a slave device, because the examples provided only specify a serial connection rather than an rs485 connection specifically? If it is possible, please could you recommend an example that I should use from the pymodbus readthedocs examples?

How to reserve tcp source port on linux?

I'm working on simple traffic tunneling solution (Linux).
Client side creates tun interface, routes all traffic on it, packages all arrived packets and sends to the server side via udp or tcp connection.
Server side expected to work like NAT. Change source ip address, source port (for tcp/udp) put packet on external network interface via sock_raw, listen for response via sock_raw, keep map of original-source-port <-> replaced-source-port and send responses back to the client.
The question is: how should I choose replaced-source-port ? OS chooses them from ephemeral ports. I can't choose it by myself, it would cause conflicts. OS kernel chooses port after I send packet via sock_raw and I have no chance to build original-source-port <-> replaced-source-port map. Even if I choose port by myself – OS kernel will reply with tcp rst to all incoming tcp packets with dst port not associated with particular app.
P.S. I'm not sure on the overall solution for tunneling too. Your suggestions would be highly appreciated.

Is it possible to TCP connect to an iPhone listening on a port on a 3G network?

I'm working on an app that relies heavily on P2P, but I currently don't have any SIM cards with which to experiment so I'm testing on a wifi network.
I'd like to know if iPhones on 3G networks are connectable and whether or not I will need to traverse NAT devices.
You're behind the provider's router and your IP address will be something like 10.x.x.x - so you'd have the tell the router, that a TCP connect from the internet to port 9420 should be forwarded to your device. I doubt that they will do this for you.