Engee documentation
Notebook

Reading data via COM: Arduino

In this example, we consider modeling in Engee with external equipment connected. The COM port of the computer where Engee is running is in the mode of receiving messages via the COM port from the Arduino MEGA microcontroller. The program for generating sent messages on a microcontroller is a cyclic generation of increasing numbers with a reset. The received messages, encoded in ASCII, are decoded in the model.

Introduction

Engee has the ability to connect external equipment - such as, for example, a COM port. Not only office and multimedia devices can be connected via this interface, but also instrumentation devices - sensors, microcontrollers, programmable logic controllers.

In previous примере we have considered sending messages from Engee to Arduino MEGA, now we will consider sending messages from Arduino MEGA to Engee.

The example model

The example model - com_read_arduino.engee contains two blocks from the section Interfaces -> COM Engee block libraries:

  • COM Setup - to configure the connection,
  • COM RX - for data transmission to Engee via the serial interface.
image_3.png

Interface settings, block COM Setup

  • the port name is for Windows OS, it is, for example, "COM5";
  • Transmission rate, baud - 9600;
  • the amount of data to receive and transmit, bit - 8;
  • number of receive-transmit stop bits, bit - 1;
  • There is no parity bit.

In the transmitter unit COM RX it is necessary to determine the port name - "COM5" and the buffer size for transmission, for example, - "1".

At the outputs of this block, we get:

  • number of elements in the received data array: a number in the format UInt32,
  • vector of received data in the format UInt8.

Since the data transmitted by Arduino is encoded according to the ASCII standard, the output data will be decoded using the block Engee Function "ASCII decoder".

Preparing for the execution of the model

Before launching the model in Engee, you need to perform several preparatory actions, as described in the manual.

In addition, similar preparation steps can be found in previous примере. The only difference is the program that is loaded into the Arduino MEGA.

Programming Arduino

The program for processing data received by Engee from Arduino via the serial port is shown in the sketch arduino_com_read_sketch.ino.

The principle of operation of the program is described in detail in the comments of the sketch. The essence of the work is as follows.

To program the Arduino Mega, just do the following:

  • download the folder arduino_com_read_sketch with a sketch,
  • open a sketch in the Arduino IDE,
  • Identify the board model - Arduino Mega or Mega 2560 and the port name - in the case of this example, it is COM5.
  • check the sketch,
  • Upload it to the board.

If the download and compilation are successful, you can proceed to executing the model in Engee, which will receive signals from the Arduino MEGA via COM.

Executing the example model

Before launching the model in Engee, make sure to follow
the following steps: >

  1. The external hardware support package is installed.
  2. The client program from the support package has been launched.
  3. The Engee connection status in the client program is "Connected".
  4. The controller is connected in the COM port.
  5. The COM signal processing program is running on the controller.
  1. The name of the COM port to which the controller is connected corresponds to the one set in the interface blocks of the model.

Let's run the model in Engee, and collect the data in simout:

In [ ]:
# @markdown **Программное управление моделированием:**  
# @markdown Требуется ввести только имя модели
имя_модели = "com_read_arduino" # @param {type:"string"}
if имя_модели in [m.name for m in engee.get_all_models()]
    модель = engee.open( имя_модели );
else
    модель = engee.load( "$(@__DIR__)/"*имя_модели*".engee" );
end
engee.run(модель);
engee.close(модель, force=true);

The data obtained as a result of the simulation can be reflected on the graph.:

In [ ]:
using Plots; gr();
plot(simout["$имя_модели/data_decode"].time,
     simout["$имя_модели/data_decode"].value;
     legend = :topright, label = "Данные из Arduino", title = "Пример чтения из COM",
     xlabel = "Время, с", ylabel = "Декодированные значения",
     seriestype=:sticks, linestyle=:dash, seriescolor = :green,
     marker = :circle, markersize = 3, markercolor = :green, markerstrokecolor = :green)
Out[0]:

You can see that the values obtained by Engee correspond to those defined in the sequence output code to the COM port on the Arduino MEGA.

Conclusion

In this example, we got acquainted with the operation of the COM RX Engee unit for receiving messages via the serial port from external equipment - Arduino MEGA. The Engee model, as we have seen, receives and decodes data from the serial port after setting up the connection.

Blocks used in example