[ST] Custom serial protocol

Patron Neuron Gate Unipi 1.1 Axon

When implementing the system, it is sometimes necessary to use a protocol that is not available in the Mervis environment. Such a situation can occur, for example, when you install a Unipi PLC as a replacement for another PLC and you need to connect some of the original peripherals, or when you need to connect the Unipi PLC to your own device with its own protocol.

The following article describes the basic information needed to successfully implement a custom serial protocol and a sample project.

Prerequisites:

  • Unipi controller running Mervis OS and Mervis IDE version 2.4.2 or higher
  • local network connectivity (any switch or router)
  • Basic knowledge of programming in structured text (ST)

Since the implementation of a serial protocol with a function block diagram (FBD) is more demanding and complex than the implementation in structured text (ST), we recommend the implementation in the ST language.

In order to create programs, functions or function blocks in the ST language, it is first necessary to switch the solution to full mode.

A total of 5 functions for working with (serial) ports are available in Mervis:

  1. Open Port - opens a port communication according to the entered data
  2. Get Port Status - gets the communication status of a handle
  3. Write Port - writes data to a given communication port
  4. Read Port - reads data from a given communication port
  5. Close Port - closes communication at the given port

A description of the inputs and outputs of the above functions is available in the Mervis IDE help
(path: Software → Libraries → Communications → Input/Output Functions)

As part of the sample project, a state machine is implemented for communication via the selected serial port on the query/response principle, when a response is expected after sending a query. Therefore, this sample project does not include a state machine that expects a spontaneous flow of data from a remote device.

A table of serial ports is also implemented (according to the tables below) using derived data types (structures) and an HMI web interface simulating a terminal window for simple communication test.

The project is created in Mervis IDE 2.6.5.

Since Mervis IDE 2.6.5 the serial port table (structure) is included in Lib.Unipi, which allows easy selection by controller type.

Example of use:

FUNCTION_BLOCK Custom_Serial

  VAR
    serialPort : unipi_serial.v1_0.Patron_M567;
    str,def : string;
  END_VAR

  INIT
    def := 'serial:'; // the first part of the definition
    def := CONCAT(IN1:=def,IN2:=serialPort.RS485_1_1); // merge the first part of the definition with the serial port
    def := CONCAT(IN1:=def,IN2:=':9600,8,N,1'); //merge with the last part of the definition
  END_INIT

  str := serialPort.RS485_1_1;

END_FUNCTION_BLOCK

Unipi Edge

RS485-S1 RS232-S2
E41012
E4111-
E41212
E41312

Common

USB110
USB211
LTE20
M-Bus M130
M-Bus M231

Unipi Patron

RS485-1 RS232(485)-2 RS485-3 RS485-4
S107621-
S1176321
S167 LTE61--
S2071---
RS485-1.1 RS232(485)-1.2 RS485-1.3 RS485-2.1
M207621-
M267 LTE61--
M5276217
M5376217
M567 LTE61-7
L207621-
L5276217

Common

USB110
USB211
LTE20

Unipi Neuron

RS485-1.1 RS485-2.1 RS485-3.1
S103 6 - -
S103-G 6 - -
M103 6 - -
M203 6 - -
M303 6 - -
M403 6 - -
M503 6 7 -
M523 6 7 -
L203 6 - -
L303 6 - -
L403 6 - -
L503 6 7 -
L523 6 7 -
L513 6 7 8
L533 6 7 8

Common

USB1 10
USB2 11
USB3 12
USB4 13
GSM1

Unipi Gate

RS485-1 RS485-2
G1002-
G11023

Common

USB 10

Unipi 1.1 / Lite

UART 1
USB1 10
USB2 11
USB3 12
USB4 13

Unipi Axon

RS485-1 RS232(485)-2 RS485-3 RS485-4
S105632-
S1156132
S155632-
S165 LTE62--
S2052---
S21523--
S5052---
S51523--
RS485-1.1 RS232(485)-1.2 RS485-1.3 RS485-2.1
M205632-
M265 LTE62--
M5056327
M51523-6
M5256327
M53523-6
M565 LTE62-7
L205632-
L5056327
L5256327

Common

USB110
USB211
LTE20
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies