
\section{Implementing Modbus protocol using OpenModelica}
The objective of this experiment is to make the user acquainted with
the demonstration of Modbus protocol through OpenModelica-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 OpenModelica-Arduino toolbox. For data transmission, we have used an RS485 module.

OpenModelica 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.
OpenModelica 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 OpenModelica models 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.


\section{Reading electrical parameters from OpenModelica}
\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 OpenModelica 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:OpenModelica-start} before getting started.

\subsection{OpenModelica Code}
Unlike other code files, the code/model for running experiments using OpenModelica are available inside OpenModelica-Arduino toolbox, as explained in \secref{sec:load-om-toolbox}. Please refer to \figref{om-examples-toolbox} to know how to locate the experiments.

\label{sec:modbus-OpenModelica-code}
\addtocontents{OpenModelicad}{\protect\addvspace{\codclr}}

\begin{OpenModelicacode}
    \mcaption{Code for Single Phase Current Output}
    {Code for Single Phase Current Output.
        Available at \LocMODOpenModelicabrief{readCurrent.mo}.}
    \label{OpenModelica:current-modbus}
    \lstinputlisting[firstline=1,lastline=10]
    {\LocMODOpenModelicacode/readCurrent.mo}
\end{OpenModelicacode}

\begin{OpenModelicacode}
    \mcaption{Code for Single Phase Voltage Output}
    {Code for Single Phase Voltage Output.
        Available at \LocMODOpenModelicabrief{readVoltage.mo}.}
    \label{OpenModelica:voltage-modbus}
    \lstinputlisting[firstline=1,lastline=10]
    {\LocMODOpenModelicacode/readVoltage.mo}
\end{OpenModelicacode}

\begin{OpenModelicacode}
    \mcaption{Code for Single Phase Active Power
        Output}{Code for Single Phase Active Power
        Output.  Available at
        \LocMODOpenModelicabrief{readPower.mo}.}
    \label{OpenModelica:modbus-power}
    \lstinputlisting[firstline=1,lastline=10]
    {\LocMODOpenModelicacode/readPower.mo}
\end{OpenModelicacode}
