
\section{Manifestation of Modbus protocol through Python}
The objective of this experiment is to make the user acquainted with
the demonstration of Modbus protocol through the Python-Arduino toolbox. 
It gives an insight into how to acquire readings from the energy meter and interpret them accordingly. As explained in \secref{sec:energy-meter}, 
an energy meter is a device that gives us different electrical parameters, including voltage, current, and power, consumed by a device. Here, we aim to obtain these values using the Python-Arduino toolbox. For data transmission, we have used an RS485 module.

Python is used for giving the required parameters to \arduino. For
example, the user will tell the required slave address to be accessed
and the number of registers to be read from or written to. Here,
\arduino\ acts as a master and energy meter as a slave. Therefore,
referring to a particular slave address will refer to the registers
that hold the desired electrical parameters (current, voltage, power, etc.), which we want to read from the energy meter.

In this experiment, \arduino\ is connected to the energy meter via an RS485 module which facilitates long-distance communication. 
Python sends the RQ to the \arduino\, which in turn sends it to the
energy meter. The energy meter then accesses the values in the
required addresses in its memory and transfers them back. This again
is in the form of another packet called RP. In this packet, the data is stored in a little-endian hexadecimal format. Thus, we make use of IEEE 754 to obtain the decimal value from this data. 

\paragraph{Note: } The Python code files presented in this section were tested on the older versions. Now, these codes may require minor changes in
the newer versions. We invite the experts to contribute the revised version of the code.


% \subsection{Software for the experiment}
% Apart from the Python-Arduino toolbox, the software for this experiment comprises two parts:
% \begin{enumerate}
% \item  Firmware for \arduino: This firmware is needed to communicate
% with Python (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.

% \begin{figure}
%   \centering
%   \includegraphics[width=\lgfig]{\LocMODfig/arduino_code_flowchart.png}
%   \caption{Flowchart of Arduino firmware}
%   \label{fig:modbus-firmware}
% \end{figure}

% \item Python code: This code requests the parameters in the energy meter
% by sending an RQ to \arduino\ from Python. 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 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 Python code}
%   \label{fig:flow-chart}
% \end{figure}
% \end{enumerate}


\section{Reading the electrical parameters from Python}
\subsection{Reading the electrical parameters}
In this section, we will show how to access the three parameters (voltage, current, and active power) in the energy meter. As discussed above, we will send an RQ from Python to \arduino. Subsequently, \arduino\ will provide us with an RP, which can be decoded to extract the desired parameter. The reader should go through the instructions given in \secref{sec:python-start} before getting started. 

% \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}

\subsection{Python Code}
\label{sec:modbus-python-code}
\addtocontents{pyd}{\protect\addvspace{\codclr}}

\begin{pycode}
  \pcaption{Code for Single Phase Current Output}
  {Code for Single Phase Current Output.
    Available at \LocMODpybrief{read\_current.py}.}
  \label{py:current-modbus}
  \lstinputlisting[firstline=1,lastline=10]
  {\LocMODpycode/read_current.py}
\end{pycode}

\begin{pycode}
  \pcaption{Code for Single Phase Voltage Output}
  {Code for Single Phase Voltage Output.
    Available at \LocMODpybrief{read\_voltage.py}.}
  \label{py:voltage-modbus}
  \lstinputlisting[firstline=1,lastline=10]
  {\LocMODpycode/read_voltage.py}
\end{pycode}

\begin{pycode}
  \pcaption{Code for Single Phase Active Power
    Output}{Code for Single Phase Active Power
    Output.  Available at
    \LocMODpybrief{read\_active\_power.py}.}
  \label{py:modbus-power}
  \lstinputlisting[firstline=1,lastline=10]
  {\LocMODpycode/read_active_power.py}
\end{pycode}
