1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
\chapter {Implementation of Modbus Protocol}
\thispagestyle{empty}
\label{modbus}
\newcommand{\LocMODfig}{\Origin/user-code/modbus/figures}
\newcommand{\LocMODscicode}{\Origin/user-code/modbus/scilab}
\newcommand{\LocMODscibrief}[1]{{\tt \seqsplit{%
Origin/user-code/modbus/scilab}}, see \fnrefp{fn:file-loc}}
\newcommand{\LocMODardcode}{\Origin/user-code/modbus/arduino}
\newcommand{\LocMODardbrief}[1]{{\tt \seqsplit{%
Origin/user-code/modbus/arduino}}, see \fnrefp{fn:file-loc}}
%%%%%%%%%%%%python starts
\newcommand{\LocMODpycode}{\Origin/user-code/modbus/python}
\newcommand{\LocMODpybrief}[1]{{\tt \seqsplit{%
Origin/user-code/modbus/python}}, see \fnrefp{fn:file-loc}}
%%%%%%%%%%%%python ends
%%%%julia starts
\newcommand{\LocMODjuliacode}{\Origin/user-code/modbus/julia}
\newcommand{\LocMODjuliabrief}[1]{{\tt \seqsplit{%
Origin/user-code/modbus/julia}}, see \fnrefp{fn:file-loc}}
%%%%julia ends
%%%%OpenModelica starts
\newcommand{\LocMODOpenModelicacode}{\Origin/user-code/modbus/OpenModelica}
\newcommand{\LocMODOpenModelicabrief}[1]{{\tt \seqsplit{%
Origin/user-code/modbus/OpenModelica}}, see \fnrefp{fn:file-loc}}
%%%%OpenModelica ends
In the previous chapters, we have discussed the programs to experiment with the sensors and actuators that come with the Shield, a DC motor, and a servomotor. One may categorize these programs as either basic or intermediate. In this chapter, we will learn one of the advanced applications that can be built using the toolbox. Recall the FLOSS discussed in the book, by default, does not have the capability to connect to Arduino. All such add-on functionalities are added to the FLOSS using toolboxes. Beginners might want to skip this chapter in the first reading. This experiment enables interfacing Modbus-based devices with FLOSS-Arduino toolbox. This functionality has a wide number of applications in the industrial sector.
\section{Preliminaries}
Modbus is an open serial communication protocol developed and
published by Modicon in 1979 \cite{modbus} \cite{modbus-paper}. Because of ease of deployment and maintenance, it finds wide applications in industries. The Modbus protocol provides a means to transmit information over serial lines
between several electronic devices to control and monitor
them. The controlling device requests for reading or writing
information and is known as the Modbus master/client. On the other
hand, the device supplying the information is called
Modbus slave/server. All the slaves/servers have a unique id and
address. Typically, there is one master and a maximum of 247 slaves \cite{simplymodbus}. \figref{mod-block} shows a representation of Modbus protocol.
During the communication on a Modbus network, the protocol determines
how the controller gets to know its device address, recognizes the
the message provided and decides the action to be taken, and accordingly
extracts data and information contained in the message. A typical structure of the communication protocol is shown in \figref{mod-master-slave}. The data is
sent as a series of zeros and ones, \ie\ bits wherein zeros are sent
as positive voltages and ones as negative.
Different versions of Modbus protocol exist on serial lines, namely
Modbus RTU, ASCII, and TCP \cite{simplymodbus}.
The energy meter used in this chapter
supports the Modbus RTU protocol. In Modbus RTU, the data is coded in
binary and requires only one communication byte. This is ideal for use
over RS232 or RS485 networks at baud rates between 1200 and 115K.
\begin{figure}
\centering
%\subfloat[Block diagram representation of the Protocol]{
\includegraphics[width=\hgfig]{\LocMODfig/fig1.png}
\caption{Block diagram representation of the Protocol}
\label{mod-block}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocMODfig/fig2.png}
\caption{Cycle of query-response between master and slave}
\label{mod-master-slave}
\end{figure}
RS485 is one of the most widely used bus standards for industrial
applications. It uses differential communication lines to communicate
over long distances and requires a dedicated pair of signal lines, say
A and B, to exchange information. Here, the voltage on one line equals
to the inverse of the voltage on the other line. In other words, the
output is 1, if A - B \textgreater $\;$ 200mV, and 0, if B - A \textgreater $\;$
200mV. \figref{rs-485} shows the pins available on a typical RS485 module. As shown in \figref{rs-485}, there are four pins on each side of the module. \tabref{tab:rs-485-pins} summarizes the usage of these pins.
\begin{table}
\centering
\caption{Pins available on RS485 and their usage}
\label{tab:rs-485-pins}
\begin{tabular}{lc}\hline
Pin name & Usage \\ \hline
Vcc & 5V \\
B & Inverting receiver input \\
A & Non-inverting receiver input \\
GND & Ground (0V) \\
RO & Receiver output \\
RE & Receiver enable \\
DE & Data enable \\
DI & Data input \\
\hline
\end{tabular}
\end{table}
%\begin{center}
%\framebox(175,30){%
% \parbox{170\unitlength}{R0 outputs 1, if A-B\textgreater200mV\\ R0 outputs 0, if B-A\textgreater200mV}%
%}
%\end{center}
%\begin{align*}
%R0 outputs 1, if A-B\textgreater200mV\\
%R0 outputs 0, if B-A\textgreater200mV
%\end{align*}
\begin{figure}
\centering
\includegraphics[width=\hgfig]{\LocMODfig/fig3.png}
\caption{Pins in RS485 module}
\label{rs-485}
\end{figure}
\subsection{Energy meter}
\label{sec:energy-meter}
An energy meter is a device that measures the amount of electricity consumed
by the load. This book makes use of the EM6400 series energy meter. It is a
multifunction digital power meter by Schneider Electric. It
reads various parameters such as phase voltage, current, active power,
reactive power, power factor, etc. Before using the meter, one has to
program system configuration, PT, CT ratios, communication parameters
through front panel keys. The reason behind using this energy meter is
the fact that it supports the Modbus RTU protocol for communication.
Multiple operations can be performed with devices supporting Modbus.
Every operation has its own fixed function code (coil status - 01,
input status - 02, holding registers - 03, input registers - 04, etc.),
which is independent of devices. The function code tells the slave which
table to access and whether to read from or write to the table.
All the parameter values are stored in the output holding registers.
Different holding registers hold the values of different parameters.
\tabref{tab:modbus-fun-codes} summarizes the various operations which
Modbus RTU supports. One can locate the addresses of individual parameters
in the user manual for EM6400. \tabref{tab:params-addr} provides the addresses
for three individual parameters, which will be accessed in this chapter.
\begin{table}
\centering
\caption{Operations supported by Modbus RTU}
\label{tab:modbus-fun-codes}
\begin{tabular}{llp{3cm}}\hline
Function Code & Action & Table Name \\ \hline
01 (01 hex) & Read & Discrete Output Coils \\
05 (05 hex) & Write single & Discrete Output Coil \\
15 (0F hex) & Write multiple & Discrete Output Coils \\
02 (02 hex) & Read & Discrete Input Contacts \\
04 (04 hex) & Read & Analog Input Registers \\
03 (03 hex) & Read & Analog Output Holding Registers \\
06 (06 hex) & Write single & Analog Output Holding Register \\
16 (10 hex) & Write multiple & Analog Output Holding Registers \\
\hline
\end{tabular}
\end{table}
\begin{table}
\centering
\caption{Individual parameter address in EM6400}
\label{tab:params-addr}
\begin{tabular}{llc}\hline
Parameter & Description & Address \\ \hline
V1 & Voltage phase 1 to neutral & 3927 \\
A1 & Current, phase 1 & 3929 \\
W1 & Active power, phase 1 & 3919 \\
\hline
\end{tabular}
\end{table}
% \begin{center}
% \begin{tabular}{ll}
% Current (phase 1): & 3929 \\
% Voltage (phase 1): & 3927 \\
% Active power (phase 1): & 3919
% \end{tabular}
% \end{center}
% Values in every register are in little endian format (1st
% register contains LSB and next register contains MSB). In our case,
% energy meter is a slave.
% As discussed before, slave addresses can be set between 1 and 247.
In Modbus protocol, the master needs to send a request packet (referred as RQ hereafter)
to the slave to read any of the slave's parameters. When the
slave receives an RQ, it needs to come up with a response packet
(referred as RP hereafter), which contains the value requested
by the master. In other words, an RQ is a message from the
master to a slave and an RP is a message from the slave back to the
master. We will first explain the structure of an RQ, followed by an example.
An RQ consists of the following fields:
\begin{enumerate}
\item Slave id: The first byte of every Modbus message is a slave id.
The master specifies the id of the slave to which the request message
is addressed. Slaves must specify their own id in every
response message (RP).
\item Function code: The second byte of every Modbus message is a
function code. This code determines the type of operation to be
performed by the slave. \tabref{tab:modbus-fun-codes} enlists the various
function codes.
\item Address of the register: After the above two bytes, RQ specifies the
data address of the first register requested.
\item Number of registers: This field denotes the total number of
registers requested.
\item CRC bytes: The last two bytes of every Modbus message are CRC
bytes. CRC stands for Cyclic Redundancy check. It is added to the
end of every Modbus message for error detection.
Every byte in the message is used to calculate the CRC.
The receiving device also calculates the CRC and compares it to the
CRC from the sending device. If even one bit in the message is
received incorrectly, the CRCs will be different, resulting in an error.
\paragraph{Note:} There are some online tools \cite{online-crc} by which one can calculate
the CRC bytes. However, one should note that the calculated CRC bytes
should be mentioned in little-endian format, which means that
the first register contains the least significant bit (LSB) and the
next register contains the most significant bit (MSB).
\end{enumerate}
Let us say, we want to access V1 (Voltage phase 1 to neutral) in the
energy meter. From \tabref{tab:params-addr}, it may be noted that the address of V1 is 3927. The size of each Modbus register is 16 bits and all EM6400 readings
are of 32 bits. So, each reading occupies two consecutive Modbus
registers. Thus, we need to access two consective holding registers
(starting from 3926) to get V1. \tabref{tab:params-rq} summarizes the
values for the various fields in the RQ required to read/access V1.
\begin{table}
\centering
\caption{A request packet to access V1 in EM6400}
\label{tab:params-rq}
\begin{tabular}{lc}\hline
Field of the RQ & Value for reading V1 \\ \hline
Slave id & 01 \\
Function code & 03 \\
Address of the register & 3926 (hex value = 0F56) \\
Number of registers & 02 \\
CRC bytes & 270F \\
\hline
\end{tabular}
\end{table}
Now, we explain the structure of an RP, followed by an example.
An RP consists of following fields:
\begin{enumerate}
\item Slave id: In an RP, the slaves must specify their own id.
\item Function code: Like the RQ, the second byte of RP is the function code.
This code determines the type of operation to be
performed by the slave. \tabref{tab:modbus-fun-codes} enlists the various
function codes.
\item Number of data bytes to follow: It refers to the total number of bytes
read. As our RQ has 2 registers each of two bytes, we expect a total of 4 bytes.
\item Data in the first requested register: It refers to the data stored
in the first register.
\item Data in the second requested register: It refers to the data stored
in the second register.
\item CRC bytes: As stated earlier, the last two bytes of every Modbus message are CRC
bytes. Like RQ, the receiving device also calculates the CRC and compares it to the
CRC from the sending device.
\end{enumerate}
% An example of a request packet is as follows. Suppose that
% the request is 01 03 0F56 0002 270F. Its meaning is explained in
% \tabref{tab:request-packet}.
% \begin{table}
% \centering
% \caption{Interpretation of a request packet}
% \label{tab:request-packet}
% \begin{tabular}{lp{10cm}}
% 01 & Slave address \\
% 03 & Function code to read holding registers \\
% 0F56 & Data Address of the first requested register (address for
% voltage phase1 to neutral) and
% (0F56 hex = 3927, +40001 offset = 43928) \\
% 0002 & Total number of registers requested for read \\
% 270F & CRC (Cyclic Redundancy Check) for error checking (LSB first) \\
% \end{tabular}
% \end{table}
Let us consider the RP, which we have received as a response to the RQ mentioned
in \tabref{tab:params-rq}. \tabref{tab:params-rp} summarizes the values for the
various fields in this RP.
\begin{table}
\centering
\caption{A response packet to access V1 in EM6400}
\label{tab:params-rp}
\begin{tabular}{lc}\hline
Field of the RP & Value for reading V1 \\ \hline
Slave id & 01 \\
Function code & 03 \\
Number of data bytes to follow & 04 \\
Data in the first requested register & 2921 \\
Data in the second requested register & 4373 \\
CRC bytes & D2B0 \\
\hline
\end{tabular}
\end{table}
% The response packet corresponding the above request packet
% is given as 01 03 04 2921 4373 D2B0. Its meaning is explained in
% \tabref{tab:response-packet}.
% \begin{table}
% \centering
% \caption{Interpretation of a response packet}
% \label{tab:response-packet}.
% \begin{tabular}{ll}
% 01 & Slave address \\
% 03 & Function code to read holding registers \\
% 04 & Total number of bytes read \\
% 2921 & Data in 1st requested register \\
% 4373 & Data in 2st requested register \\
% D2B0 & CRC for error checking (LSB first)
% \end{tabular}
% \end{table}
In this RP, we consider the data in the two requested registers to be 43732921
in hexadecimal. The reason behind keeping the data in the second requested register
as the MSB is that the obtained values are being read in little-endian format.
After converting this value to floating point using the
IEEE Standard for Floating-Point Arithmetic (IEEE 754), we obtain the
value as 243.16. Thus, the value of V1 (Voltage phase 1 to neutral) in the
energy meter is found to be 243.16 Volts.
\subsection{Endianness}
Most of the numeric values to be stored in the computer are more than
one byte long. Thus, there arises a question of how to store the
multibyte values on the computer machines where each byte has its own
address \ie\ which byte gets stored at the ``first'' (lower) memory
location and which bytes follow in higher memory locations. For
example, let us picture this. A two-byte integer 0x5E5F is stored on the disk by one machine, with the 0x5E (MSB) stored at the lower memory address and the 0x5F (LSB) stored at a higher memory address.
But there is a different machine that reads this integer by picking 0x5F for the MSB and 0x5E for the LSB, giving 0x5F5E.
Hence, it results in a disagreement on the value of the integer between the two machines. However, there is no so-called ``right''
ordering to store the bytes in the case of multibyte quantities.
Hardware is built to store the bytes in a particular fashion, and as long as compatible hardware reads the bytes in the same fashion, things are
fine. Following are the two major types of storing the bytes:
\begin{enumerate}
\item Little-endian:
If the hardware is designed so that the LSB of a multibyte integer is stored ``first''at the lowest memory address, then the hardware is said to be little-endian. In this format, the ''little'' end of the integer gets stored
first and the next bytes are stored in higher (increasing) memory
locations.
\item Big-endian:
Here, the hardware is designed so that the MSB of a multibyte integer is stored ``first''at the lowest memory address. Thus, the ``big'' end of the integer gets
stored first and accordingly the next bytes get stored in higher
(increasing) memory locations.
\end{enumerate}
For example, let us take a four-byte integer 0x436B84A3. Considering
that the read holding registers in Modbus protocol are 16-bits each, the
LSB (or the little end) of this integer is 0x84A3, and the MSB (or the big end)
of this integer is 0x436B. Then, the memory storage patterns
for the integer would be like that shown in \tabref{tab:memory-storage}.
\begin{table}
\centering
\caption{Memory storage of a four-byte integer in little-endian and big-endian}
\label{tab:memory-storage}
\begin{tabular}{lllc}\hline
Memory Address & Byte & Little-endian & Big-endian \\ \hline
3900 & 8A43 & MSB & LSB \\
3901 & 436B & LSB & MSB \\
\hline
\end{tabular}
\end{table}
% \begin{table}
% \centering
% \caption{Hexadecimal to Decimal}
% \label{tab:ieee-decimal}
% \begin{tabular}{ |p{3cm}|p{3cm}|p{3cm}|p{3cm}|}
% \hline
% \multicolumn{4}{|c|}{Four Bytes Integer Reading from Meter} \\
% \hline
% Memory Address & Memory Address & Little-endian & Big-endian \\ \hline
% 3900 & 8A43 & MSB & LSB \\
% 3901 & 436B & LSB & MSB \\ \hline
% \end{tabular}
% \end{table}
% \begin{table}
% \centering
% \caption{Single and Double Precision Representation}
% \label{tab:single-precision}
% \begin{tabular}{|l|l|l|l|}
% \hline
% Single & Sign (1 bit) & Exponent (8 bit) & Mantissa (23 bit) \\ \hline
% Double & Sign (1 bit) & Exponent (11 bit) & Mantissa (52 bit) \\ \hline
% \end{tabular}
% \end{table}
To represent the hexadecimal values of the read holding
registers into user friendly decimal (floating point) values, we
follow IEEE 754 standard. Most common standards for representing
floating point numbers are:
\begin{enumerate}
\item Single precision: In this standard, 32 bits are used to represent a floating-point number. Out of these 32 bits, one bit is for the sign bit, 8 bits for the exponent, and the remaining 23 bits for mantissa.
\item Double precision: Here, 64 bits are used to represent a floating-point number. Out of these 64 bits, one bit represents the sign bit, 11 bits for the exponent, and the remaining 52 bits for mantissa. As the name indicates, this standard is used where precision matters more.
\end{enumerate}
% Decimal Value = $( - 1) * \text{sign} * 2^{exponent}* \text{Mantissa}$.
% Hence, for 32 bit values, the sign is stored in bit 32. The exponent
% can be calculated from bits 24-31 by subtracting 127. The mantissa is
% stored in bits 1-23. An invisible leading bit (i.e. it is not actually
% stored) with value 1.0 is placed in front, then bit 23 has a value of
% 1/2, bit 22 has value 1/4 etc. As a result, the mantissa has a value
% between 1.0 and 2. At last, the decimal value is calculated using the
% above mentioned equation.
There are several online converters \cite{ieee-754-conv} which peform the
IEEE 754 floating point conversion. In this chapter, a function has been formulated for this conversion, wherever needed.
\section{Setup for the experiment}
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocMODfig/block-diagram.PNG}
\caption{Block diagram for reading the parameters in energy meter}
\label{fig:block-diagram}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocMODfig/Full-Set-Up.png}
\caption{Experimental set up for reading energy meter}
\label{fig:full-set-up}
\end{figure}
This section discusses the setup for configuring
\arduino\ as Modbus master and energy meter as the slave.
The block diagram is shown in \figref{fig:block-diagram} ,
whereas \figref{fig:full-set-up} presents the
actual setup. The following steps discuss the various connections of this setup:
\begin{enumerate}
\item \arduino\ has only one serial port. It communicates on
the digital pins 0 and 1 as well as on the computer via USB. Since
we want serial communication which shouldn't be disturbed by the USB
port and the Serial Monitor, we use the Software Serial
library. Using this library, we can assign any digital pins as RX and
TX and use it for serial communication.
In this experiment, pin 10 (used as RX) and pin 11 (used as TX) are connected to RO (Receive Out) and DI (Data In) pins of the RS485 module respectively.
\item DE (Data Enable) and RE (Receive Enable) pins of RS 485 are
shorted and connected to digital pin 3 of the \arduino\ board.
This serves as a control pin that will control when to receive and transmit serially.
\item Vcc and GND of the RS485 module are connected to Vcc and GND of
the \arduino\ board.
\item A and B pins of RS485 module are connected to A (Pin 7) and B (Pin 14)
pins of the energy meter. These two pins of the energy meter are meant for RS485 communication.
\item A $120k\Omega$ termination resistor is connected between
pins A and B to avoid reflection losses in the transmission line.
\end{enumerate}
\section{Software required for this experiment}
Apart from FLOSS-Arduino toolbox, the software for this experiment comprises two parts:
\begin{enumerate}
\item Firmware for \arduino: This firmware is needed to communicate
with the FLOSS (using serial interface), and with RS485 module (using
Software Serial interface). Control logic to enable receive and
transmit modes of MAX485 chip is also present in this firmware. \figref{fig:modbus-firmware} demonstrates the overall implementation of this firmware. The firmware is provided in \secref{sec:firmware-modbus}.
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocMODfig/arduino_code_flowchart.png}
\caption{Flowchart of Arduino firmware}
\label{fig:modbus-firmware}
\end{figure}
\item FLOSS code: This code requests the parameters in the energy meter
by sending an RQ to \arduino\ from the FLOSS. Then it waits till
an RP is available from the \arduino. After receiving the RP, it extracts
the data from this packet and converts it into IEEE
754 floating-point format. The overall implementation is being
described below:
\begin {enumerate}
\item Frame an RQ to be sent to the energy meter (slave) in ASCII coded decimal
format.
\item Send the RQ serially to \arduino.
\item Let \arduino\ send the RQ to the energy meter via RS485 module.
\item Let the energy meter send the RP to \arduino\ via RS485 module.
\item Read the RP available on \arduino.
\item Extract the data stored in holding registers from the RP.
\item Assuming this data to be stored in little-endian format,
convert this data in floating-point values using IEEE 754 standard.
\item Display the value in the Console, output window, Command Prompt (on Windows) or Terminal (on Linux), as the case maybe.
\end{enumerate}
\figref{fig:flow-chart} presents the sequence in which the steps mentioned above are executed.
\begin{figure}
\centering
\includegraphics[width=\hgfig]{\LocMODfig/flowchart.png}
\caption{Flowchart of the steps happening in the FLOSS code}
\label{fig:flow-chart}
\end{figure}
\end{enumerate}
\subsection{Arduino Firmware}
\label{sec:firmware-modbus}
\addtocontents{ard}{\protect\addvspace{\codclr}}
\begin{ardcode}
\acaption{First 10 lines of the firmware for Modbus Energy Meter
experiment}
{First 10 lines of the firmware for Modbus. Available at
\LocMODardbrief{send\_packet.ino}.}
\label{ard:firmware-modbus}
\lstinputlisting[firstline=1,lastline=10]
{\LocMODardcode/send_packet.ino}
\end{ardcode}
|