[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.
Preparation of the solution
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.
Available functions in Mervis
A total of 5 functions for working with (serial) ports are available in Mervis:
Open Port - opens a port communication according to the entered data
Get Port Status - gets the communication status of a handle
Write Port - writes data to a given communication port
Read Port - reads data from a given communication port
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)
Sample project
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.
Marking of Unipi serial ports in ST language
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 |
| E410 | 1 | 2 |
| E411 | 1 | - |
| E412 | 1 | 2 |
| E413 | 1 | 2 |
Common
Unipi Patron
| | RS485-1 | RS232(485)-2 | RS485-3 | RS485-4 |
| S107 | 6 | 2 | 1 | - |
| S117 | 6 | 3 | 2 | 1 |
| S167 LTE | 6 | 1 | - | - |
| S207 | 1 | - | - | - |
| | RS485-1.1 | RS232(485)-1.2 | RS485-1.3 | RS485-2.1 |
| M207 | 6 | 2 | 1 | - |
| M267 LTE | 6 | 1 | - | - |
| M527 | 6 | 2 | 1 | 7 |
| M537 | 6 | 2 | 1 | 7 |
| M567 LTE | 6 | 1 | - | 7 |
| L207 | 6 | 2 | 1 | - |
| L527 | 6 | 2 | 1 | 7 |
Common
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 |
Unipi Gate
| | RS485-1 | RS485-2 |
| G100 | 2 | - |
| G110 | 2 | 3 |
Common
Unipi 1.1 / Lite
| USB1 | 10 |
| USB2 | 11 |
| USB3 | 12 |
| USB4 | 13 |
Unipi Axon
| | RS485-1 | RS232(485)-2 | RS485-3 | RS485-4 |
| S105 | 6 | 3 | 2 | - |
| S115 | 6 | 1 | 3 | 2 |
| S155 | 6 | 3 | 2 | - |
| S165 LTE | 6 | 2 | - | - |
| S205 | 2 | - | - | - |
| S215 | 2 | 3 | - | - |
| S505 | 2 | - | - | - |
| S515 | 2 | 3 | - | - |
| | RS485-1.1 | RS232(485)-1.2 | RS485-1.3 | RS485-2.1 |
| M205 | 6 | 3 | 2 | - |
| M265 LTE | 6 | 2 | - | - |
| M505 | 6 | 3 | 2 | 7 |
| M515 | 2 | 3 | - | 6 |
| M525 | 6 | 3 | 2 | 7 |
| M535 | 2 | 3 | - | 6 |
| M565 LTE | 6 | 2 | - | 7 |
| L205 | 6 | 3 | 2 | - |
| L505 | 6 | 3 | 2 | 7 |
| L525 | 6 | 3 | 2 | 7 |
Common