Tuesday 21 March 2017

MODBUS RTU

Modbus RTU Unplugged – An introduction to Modbus RTU Addressing, Function Codes and Modbus RTU Networking
OVERVIEW
Modbus RTU is an open, serial (RS-232 or RS-485) protocol derived from the Master/Slave architecture. It is a widely accepted protocol due to its ease of use and reliability. Modbus RTU is widely used within Building Management Systems (BMS) and Industrial Automation Systems (IAS). This wide acceptance is due in large part to MODBUS RTU’s ease of use.
MODBUS RTU messages are a simple 16-bit CRC (Cyclic-Redundant Checksum). The simplicity of these messages is to ensure reliability. Due to this simplicity, the basic 16-bit MODBUS RTU register structure can be used to pack in floating point, tables, ASCII text, queues, and other unrelated data.


modbusrtu_diagram
MODBUS is considered an application layer messaging protocol, providing Master/Slave communication between devices connected together through buses or networks. On the OSI model, MODBUS is positioned at level 7. MODBUS is intended to be a request/reply protocol and delivers services specified by function codes. The function codes of MODBUS are elements of MODBUS’ request/reply PDUs (Protocol Data Unit).
In order to build the MODBUS application data unit, the client must initiate a MODBUS transaction. It is the function which informs the server as to which type of action to perform. The format of a request initiated by a Master is established by the MODBUS application protocol. The function code field is then coded into one byte. Only codes within the range of 1 through 255 are considered valid, with 128-255 being reserved for exception responses. When the Master sends a message to the Slave, it is the function code field which informs the server of what type of action to perform.
To define multiple actions, some functions will have sub-function codes added to them. For instance, the Master is able to read the ON/OFF states of a group of discreet outputs or inputs. It could also read/write the data contents of a group of MODBUS registers. When the Master receives the Slave response, the function code field is used by the Slave to indicate either an error-free response or an exception response. The Slave echoes to the request of the initial function code in the case of a normal response.
Data Object Properties
MODBUS RTU packets are only intended to send data; they do not have the capability to send parameters such as point name, resolution, units, etc. If the ability to send such parameters is needed, one should investigate a BACnet, EtherNet/IP, or other modern protocols.
MODBUS RTU versus Other Protocols
Despite the limitations of MODBUS RTU, there are still many good reasons as to why it is still a contender among other industrial automation protocols. For one, MODBUS RTU is much easier to implement than newer protocols and is a dominant force in the market place. MODBUS RTU also requires significantly less memory. To implement MODBUS RTU, you can fit the necessary size of 2Kb on a small 8-bit CPU or PIC processor, whereas with BACnet and EtherNet/IP, you may require 30-100Kb of memory.
MODBUS RTU Address Requirements
Standard MODBUS RTU node addresses are 1-254, with 0 being reserved for broadcast messages and write only. However the 0 address is rarely used due to the fact that there is no confirmation that the message was properly received at the slave node. This doesn’t have much affect if your physical layer is RS-232 as only one node can be implemented anyway. RS-485 limits the number of nodes to 32, though some drivers will allow you to extend the amount.
The difference between MODBUS RTU and MODBUS TCP
The most basic difference between MODBUS RTU and MODBUS TCP (Also known as MODBUS IP, MODBUS EtherNet, and MODBUS TCP/IP) is that MODBUS TCP runs on an Ethernet physical layer and Modbus RTU is a serial level protocol. Modbus TCP also uses a 6 byte header to allow routing.
Bit Structure in the Byte
The Bit of least importance is sent and received first. All devices within the network must interpret each transmitted byte analogously in this manner. There are no methods for automated recognition of baud rates is not assigned and the same baud rate must be utilized by the Server as well as all clients connected to the bus. No specific baud rate is specified by the MODBUS: typical baud rates are 9600 or 19200.
A transmitted Byte is coded as: 8 Bit binary value, hexadecimal 0 - 9 and A - F. The least significant Bit is sent and received first
A transmitted Byte is coded as: 8 Bit binary value, hexadecimal 0 – 9 and A – F. The least significant Bit is sent and received first
MODBUS RTU Memory Map
Modbus RTU
Data Type
Common
name
Starting address
Modbus Coils Bits, binary values, flags 00001
Digital Inputs Binary inputs 10001
Analog Inputs Binary inputs 30001
Modbus Registers Analog values, variables 40001
The difference between MODBUS RTU and MODBUS/ASCII
There are two basic transmission modes found in serial MODBUS connections, ASCII and RTU. These transmission modes determine the way in which the MODBUS messages are coded. In ASCII format, the messages are readable, whereas in RTU the messages are in binary coding and cannot be read while monitoring. The trade-off is that the RTU messages are a smaller size, which allows for more data exchange in the same time span. One should be aware that all nodes within one MODBUS network must be of the same transmission mode, meaning MODBUS ASCII cannot communicate with MODBUS RTU and vice versa.
In MODBUS/ASCII, messages are encoded with hexadecimal value, represented with comprehensive ASCII characters. The characters used for this encoding are 0…9 and A…F. For every byte of information, two communication-bytes are used because every communication-byte can only define 4 bits in the hexadecimal system. MODBUS RTU, however, exchanges data in binary format where each byte of data is coded in one communication-byte.
The MODBUS messages on a serial connection are not broadcast in plain format. They are constructed in a way that allows receivers an easy way to detect the beginning and end of a message. The characters start and end a frame when in ASCII mode. To flag the start of a message, a colon ‘:’ is used, and each message is ended with a CR/LF combination. MODBUS RTU uses a different method. In RTU, framing is constructed by measuring gaps of silence on the communication line. Before each message, there must be a minimum gap of 3.5 characters. To prepare for new messages, the receiver clears the buffer when a gap of 1.5 characters is detected. One of the main differences between MODBUS/ASCII and MODBUS RTU is that ASCII allows gaps between the bytes of a message with a maximum length of 1 second. With MODBUS RTU, continuous streams of messages must be sent.
Properties of Modbus/ASCII and Modbus/RTU
Modbus/ASCII Modbus/RTU
Characters ASCII 09 and A..F Binary 0255
Error check LRC Longitudinal Redundancy Check CRC Cyclic Redundancy Check
Frame start character ‘: 3.5 chars silence
Frame end characters CR/LF 3.5 chars silence
Gaps in message 1 sec 1.5 times char length
Start bit 1 1
Data bits 7 8
Parity even/odd none even/odd none
Stop bits 1 2 1 2

How to Install and Use the Linux Bash Shell on Windows 10

Windows 10’s Anniversary Update offers a big new feature for developers: A full, Ubuntu-based Bash shell that can run Linux software directly on Windows. This is made possible by the new “Linux Subsystem for Windows” Microsoft is adding to Windows 10.
If you’re using an Insider Preview build of Windows 10, you can now install the Bash shell and play with it yourself right now. This feature was added in Insider Preview build 14316, which was only available on the Fast channel at the time this article was written. It’s also only for 64-bit versions of Windows 10.
What You Need to Know About Windows 10’s Bash Shell
This isn’t a virtual machine, a container, or Linux software compiled for Windows (like Cygwin). Instead, Windows 10 gains a Linux Subsystem for Windows, which is based on Microsoft’s abandoned Project Astoria work for running Android apps on Windows.
Think of it as the opposite of Wine. While Wine allows you to run Windows applications directly on Linux, the Linux Subsystem for Windows allows you to run Linux applications directly on Windows.
Microsoft has worked with Canonical to offer a full Ubuntu-based Bash shell that runs atop this subsystem. Technically, this isn’t Linux at all. Linux is the underlying operating system kernel, and that isn’t available here. Instead, this allows you to run the Bash shell and the exact same binaries you’d normally run on Ubuntu Linux. Free-software purists often argue the average Linux operating system  should be called “GNU/Linux” because it’s really a lot of GNU software running on the Linux kernel. The Bash shell you’ll get is really just all those GNU utilities and other software.
There are some limitations here. This won’t work with server software, and it won’t work with graphical software. It’s intended for developers who want to run Linux command-line utilities on Windows. These applications get access to the Windows file system, but you can’t use Bash commands to automate normal Windows programs, or launch Bash commands from the standard Windows command-line. They get access to the same Windows file system, but that’s it. Not every command-line application will work, either, as this feature is still in beta.

How to Install Bash on Windows 10


Once you’re sure you’re using the correct version of Windows 10, open the Settings app and head to Update & Security > For Developers. Activate the “Developer Mode” switch here to enable Developer Mode.

Next, open the Control Panel, click “Programs,” and click “Turn Windows Features On or Off” under Programs and Features. Enable the “Windows Subsystem for Linux (Beta)” option in the list here and click “OK.”
After you do, you’ll be prompted to reboot your computer. Click “Restart Now” to reboot your computer and Windows 10 will install the new feature.
After your computer restarts, click the Start button (or press the Windows key), type “bash”, and press “Enter.”
The first time you run the bash.exe file, you’ll be prompted to accept the terms of service. The command will then download the “Bash on Ubuntu on Windows” application from the Windows Store.

How to Use Ubuntu’s Bash Shell and Install Linux Software

You’ll now have a full command-line bash shell based on Ubuntu. Because they’re the same binaries, you can use Ubuntu’s apt-get command to install software from Ubuntu’s repositories. You’ll have access to all the Linux command line software out there, although not every application may work perfectly–especially in the initial beta releases.
To open the Bash shell, just open your Start menu and search for “bash” or “Ubuntu.” You’ll see a “Bash on Ubuntu on Windows” application. You can pin this application shortcut to your Start menu, taskbar, or desktop for easier access.
If you’re experienced using a Bash shell on Linux, Mac OS X, or other platforms, you’ll be right at home. You don’t need to use sudo, as you’re given a root shell. The “root” user on UNIX platforms has  full system access, like the “Administrator” user on Windows. Your Windows file system is located at /mnt/c in the Bash shell environment.
Use the same Linux terminal commands you’d use to get around. If you’re used to the standard Windows Command Prompt with its DOS commands, here are a few basic commands on both Bash and Windows:
  • Change Directory: cd in Bash, cd or chdir in DOS
  • List Contents of Directory: ls in Bash, dir in DOS
  • Move or Rename a File: mv in Bash, move and rename in DOS
  • Copy a File: cp in Bash, copy in DOS
  • Delete a File: rm in Bash, del or erase in DOS
  • Create a Directory: mkdir in Bash, mkdir in DOS
  • Use a Text Editor: vi or nano in Bash, edit in DOS
It’s important to remember that, unlike Windows, the Bash shell and its Linux-imitating environment are case-sensitive. In other words, “File.txt” with a capital letter is different from “file.txt” without a capital.
For more instructions, consult our beginner’s guide to the Linux command-line and other similar introductions to the Bash shell, Ubuntu command line, and Linux terminal online.
You’ll need to use the apt-get command to install and update the Ubuntu environment’s software. Here are the apt-get commands you’ll need to know:
  • Download Updated Information About Available Packages: apt-get update
  • Install an Application Package: apt-get install packagename (Replace “packagename” with the package’s name.)
  • Uninstall an Application Package: apt-get remove packagename (Replace “packagename” with the package’s name.)
  • Search for Available Packages: apt-cache search word (Replace “word” with a word you want to search package names and descriptions for.)
  • Download and Install the Latest Versions of Your Installed Packages: apt-get upgrade
Once you’ve downloaded and installed an application, you can type its name at the prompt and press Enter to run it. Check that particular application’s documentation for more details.

Remember, software you install in the Bash shell is restricted to the Bash shell. You can’t access it from the Command Prompt, Power Shell, or elsewhere in Windows. Software in the Bash shell also can’t interact directly with or launch Windows programs, although the Bash environment and Windows have access to the same files on your computer.
However, you can create Bash shell scripts (.sh scripts) and run them with the Bash shell.

Making A LoRa Gateway with RPI

Making A LoRa Gateway

A nice thing about LoRa is that the receiver needs very very little processor power - even a lowly PIC or AVR has plenty enough power to do the job - because the hard work is done inside the LoRa chip.  However, having received the data it would be good to upload it to the internet so that others can follow the flight on a map, and/or view the pretty pictures being received, and for this a Raspberry Pi is ideal.
So to build a LoRa gateway you need:
  • Pi In The Sky LoRa board
  • Raspberry Pi A+ or B+ or V2 B
  • Internet connection to the Pi (wired or wireless)
  • 70cm (434MHz) antenna
The LoRa board is exactly the same as used by the tracker.
Note: You can also use LoRa 868MHz modules in which case the antenna needs to be different
LoRa Board

The above board is populated with 2 LoRa modules, so it can be used to listen on 2 separate frequencies at the same time.  Our software supports the use of 1 or 2 modules, and either can be 434 or 868 MHz versions.
Physically install is very simple – just push on to the Pi using a standard pin header extender (supplied).  Or a pin header with extended pins can be used if you want to stack another board on top.
Software Installation
Next, burn an operating system onto a suitable SD card.  For this purpose anything from 4GB should be fine.  The following instructions are for Raspbian, and no other operating systems have been tested.
First, run raspi-config:
sudo raspi-config
Then expand the filesystem, as you would normally do, then choose Advanced Options –> SPI and enable SPI.

It’s also worthwhile to change the hostname (Advanced Options –> Hostname) to something like "LoRaGateway".  Finally, close the program and agree to reboot the Pi.
Once rebooted, login again.  We now have some software to install.  First, install wiringPi, which is used for the SPI library and to read the status of the LoRa module via 2 of its INT pins:
cd ~
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
The gateway software uses the curl library for internet access (uploading telemetry data and/or image data), so install that:
sudo apt-get install libcurl4-openssl-dev
and the ncurses library used for the screen display:
sudo apt-get install libncurses5-dev
Finally, install the gateway software itself:
cd ~
git clone https://github.com/PiInTheSky/lora-gateway.git
cd lora-gateway
make
That completes the installation, so now for the configuration.  The main settings are in a file gateway.txt in the above folder (/home/pi/lora-gateway).  Here’s a simple example:
tracker=MYCALLSIGN
frequency_0=434.450
mode_0=5
AFC=Y
This firstly sets your callsign, which if you are a radio amateur would normally be your radio callsign, but it can be something else.
The next part sets the frequency and mode for the first LoRa device (the one in position “CE0”).  Frequency is in MHz and should match the frequency of the tracker that you intend to receive.
For full details on all settings see the README.md file in the /github repository.
Usage
To run, just type
sudo ./gateway
and you will see a screen like this:

Now start up your tracker and, all being well, you should soon start to see the packets landing at the gateway:


For more knowledge click here.