MBLogic for an open world in automation
MBAsyncServer is a simple Modbus/TCP server with the following features:
MBAsyncServer will accept multiple incoming client Modbus/TCP connections. There is no defined limit to the number of connections that it will accept. However, if more connections result in a greater volume of communications, the increased CPU load may cause reduced server performance.
The following Modbus functions are supported:
All Modbus functions read and write to a single unified data table. By "unified", it is meant that all of the data types are overlaid on top of each other and occupy the same address space. This means that a single data table element may be accessed by several different functions.
The data table has the following address ranges:
All data types are overlaid on top of each other in the following manner:
There is also an option to pack the Coils and Discrete Inputs into the first 4096 registers (16 coils per register). When this option is enabled, writing to a Coil or Discrete Input affects the corresponding register, and visa versa. When the Coils and Discretes Inputs are *not* packed into registers, this called a separate data table. When they are packed into registers, this is called an overlaid data table. The default is 'separate'. For example:
python mbasyncserver.py -d s
MBAsyncServer will come as an archive file in "tar-gz" (most platforms) or "zip" (MS-Windows) formats. The MS-Windows version has the special new-line character combination (CR-LF) required by that platform, but is otherwise identical to the standard version.
Extract the archive file into the desired location. MBAsyncServer doesn't care where you place it.
This is the language support for Python which is required for MBAsyncServer to run. MBAsyncServer has beent tesed with version 2.5. Other versions may work but has not been tested. For Linux, Python is often already installed. If not, it can normally be installed from the distro's package manager. For MS-Windows, Python can be downloaded from http://Python.org
MBAsyncServer may be started from the command line. For Linux, the syntax is:
You should see something like the following appear:
MBAsyncServer version 2.00 Starting server on port 8600. Fri Jan 02 18:53:36 2009 Server running...
For Microsoft Windows, the syntax is:
The path used for Python (e.g. "C:\Python25\") may need to be changed if you have installed a different version, or have installed it in a different location.
To shut down MBAsyncServer, press "control-C" (press the "control" and "C" keys simultaneously). This will send a signal to MBAsyncServer asking it to shut down. You should see something like the following appear.
Operator terminated server at Fri Jan 01 18:56:31 2009
The Modbus/TCP protocol defines IP port 502 as the "standard" port to use for Modbus/TCP communications. However, MBAsyncServer allows you to select a different IP port by means of a command line parameter "-p".
./mbasyncserver.py -p 8502
As well as the normal Modbus commands, the server can also be made to respond to a special shut down signal which causes the program to exit. This is sometimes useful when testing clients, especially when running multiple servers. The shut down signal is defined by the "-q" parameter.
python mbasyncserver.py -q quit.
To shut down the server remotely, simply send the shut down command to the server on the same port which it is listening to Modbus commands on, and it will exit immediately. If no shutdown command is specified, the feature is disabled by default.
The available command line options are:
Standard Ethernet protocols use what are called "ports". These are numbers which are part of the Ethernet packets which are used to route them to the correct application program. Both ends of the connection (client and server) have to use an agreed upon port number in order to communicate with each other. The server must "bind to the port number" (request the number from the operating system) before it can listen on that port for requests. Clients on the other hand do not have to bind to a port and are free to send requests to any port.
The standard port number for Modbus/TCP is 502. It is possible to use a different port, provided both client and server can be set to use a different port number. In some cases, this isn't possible, and in most cases other cases it is most convenient to just use the standard port number.
With most operating systems, ports numbers less than 1024 are reserved for standard system services such as e-mail servers, web servers, etc. and are not available to ordinary applications. This poses a problem with Modbus/TCP. Either the Modbus/TCP server needs to gain (temporarily at least) elevated priviledges, or else the incoming messages on port 502 need to be re-routed to a different port. The second choice is often the simplest, and poses the fewest security risks.
However, it is important to note that if you don't need to use the Modbus server, or if you do need it but can run it on an alternate port number, then you don't need to redirect the port. If the remote client is capable of using an alternate port, that is usually the best solution.
Linux has a built-in facility called "iptables" which can be used to block, re-route and redirect communications. Redirecting traffic arriving on port 502 to a different can easily be done by using iptables. For example, to redirect incoming traffic on port 502 to port 8502:
iptables -t nat -I PREROUTING -p tcp --dport 502 -j REDIRECT --to-port 8502
Depending upon how security is set up on your distro, you may need to either log in as "root", or (preferably) use "sudo". For example:
sudo iptables -t nat -I PREROUTING -p tcp --dport 502 -j REDIRECT --to-port 8502
To save this change permanently so that it is automatically loaded when the computer boots up (assuming you use sudo and have nano installed):
iptables -t nat -I PREROUTING -p tcp --dport 502 -j REDIRECT --to-port 8502 sudo sh -c "iptables-save > /etc/iptables.rules" sudo nano /etc/network/interfaces
The above example ends with starting the nano editor (you can use a different editor if you wish) to edit the "interfaces" file. This file stores the Ethernet configuration. Add the following line to the end of the section for the Ethernet port which will be used for Modbus/TCP (typically "eth0"):
pre-up iptables-restore < /etc/iptables.rules
Save the configuation file and exit nano. Now Ethernet packets coming from outside the computer to port 502 will be redirected to port 8502. However, packets originating inside the same computer (e.g. 'localhost') will not be redirected. Anything originating on the same computer will need to be sent to port 8502.
Microsoft Windows does not offer any built-in security for system ports, so port 502 can be used directly without redirection. However, if you are using any server (on any port), you may need to adjust the firewall (if one is installed) to allow incoming connections.