Table of Contents

MP STUDIO 2 · User Protocol

The user protocol lets the PLC exchange data in byte layouts you define with devices that do not use Modbus, such as CO2 sensors, barcode and QR readers, inverters and temperature controllers that take ASCII commands, and numeric displays (V1.3.0 and later). In the User Protocol window, you define the layout of the bytes and when they are sent, and the PLC runs that communication together with the program. You do not have to handle bytes one at a time with PUT and GET in the ladder, and received values are stored in the memory you specify only after the whole frame, checksum included, matches.

Figure 1. How the frames and transactions defined in the User Protocol window run on the PLC

Supported Products and Communication Ports

The user protocol is set on the serial channels of the PLC (Comm Port 1 to Comm Port 3). The number of communication ports available depends on the model.

Communication port Model
Comm Port 1 MPS-8A4R(T)-S · MPS-8A8R(T)-S · MPS-16A8R(T)-R2 · MPS-16A16R(T)
Comm Port 1 to Comm Port 3 MPS-8A8R · MPS-16A8R(T) · MPS-16A8R8T · MPA series

For the terminals of each communication port (RS-232, RS-485 and UART) and the connector diagrams, see the product pages: MPS-8A4R-S · MPS-8A4T-S · MPS-8A8R-S · MPS-8A8T-S · MPS-16A16R · MPS-8A8R · MPS-16A8R · MPS-16A8R8T · MPA Series. On the MPA series, Comm Port 1 is RS-232, Comm Port 2 is RS-485 and Comm Port 3 is UART.

How It Works

The user protocol is defined for each communication port with frames and transactions.

Exchange pattern Start Behavior Typical uses
Ask & receive Period, ladder UPTRIG or value change Sends a frame and waits for the reply frame. CO2 sensors, reading inverter status, temperature controllers
Receive only Always listening Stores the values when a matching frame arrives. Sends nothing. Barcode and QR readers, scales with continuous output
Answer on receive Always listening Sends the defined answer frame as soon as a matching frame arrives. PC programs, HMIs without Modbus
Send only Period, ladder UPTRIG or value change Sends a frame and does not wait for a reply. Numeric displays, printers, setting commands

Figure 2. The order in which the PLC and the device exchange frames in the four exchange patterns

On the PLC, the user protocol runs as follows.

Preparing the Communication Port

A serial channel used for the user protocol must be in User Protocol mode with Use this channel for download turned off.

Figure 3. The Communication Setting window with the mode of Serial 1 set to User Protocol

  1. Select Setting → Communication Port, and on the tab of the serial channel you use (for example Serial 1), set the baud rate, data bits, parity and stop bits to match the device. To use a baud rate other than 115200, first turn off Use this channel for download.
  2. You can set the mode to User Protocol in this window or leave it as it is. When you click OK in the User Protocol window, a port in Normal mode changes to User Protocol, and download on that port is turned off.

User Protocol Window

Opening the Window

Figure 4. User Protocol… in the Setting menu

You can edit the ladder while the window stays open. If a communication port has no definition, + Create on this port and a list of templates are shown.

Figure 5. A communication port without a definition, where you start with an empty port or choose a template

Template What it contains
CO2 / gas sensor — ask every second An Ask & receive transaction (1-second period) and a Send only transaction that sends the zero command with UPTRIG 3. Binary value in D100, two's complement sum checksum
CO2 / gas sensor — receive only (sensor communication example · the sensor sends by itself) A Receive only transaction that always listens for the same CO2 reply frame without sending a request
Barcode / QR reader A Receive only transaction. Stores the text up to the end characters CR LF from D200 and the number of characters in D190
Inverter / temperature controller (ASCII station commands) Ask & receive (0.5-second period). ASCII decimal and hex values, sum checksum (2 ASCII hex digits), NAK error reply

You can freely change the template names and cells (D100, M100 and so on) after adding a template. You can also add templates to a port that already has a definition with + Load from a template…; if a name is already taken, a number is added to the end.

Window Layout

Figure 6. The User Protocol window (the Ask & receive transaction of the CO2 sensor template selected)

Shortcut Keys and Undo

Key Action
Ctrl+Enter OK
Esc Closes the window. If there are changes, asks Discard your changes?
Ctrl+Z / Ctrl+Y Undoes or redoes changes made in the window, one step at a time. When there is nothing left to undo, it goes on to Undo and Redo for the whole program.
Ctrl+S Saves the project. Changes made in the window are not saved until you click OK.
Ctrl+T / Ctrl+M Download / monitoring
← → · Alt+← Alt+→ · Delete · Insert In a chip strip: move between chips · move a chip · delete a chip · add a chip

Frames

A frame is made of chips (fields) placed in byte order. The byte positions (counting from 1) are shown under the chips, and when you select a checksum chip, the range it covers is highlighted on the strip. Click + at the end of the chip strip and choose a field type to add a chip.

Figure 7. Choosing a field type with + on the chip strip

Field When sending When receiving
Fixed Sends the entered bytes as they are. The frame matches only if the bytes are the same (the chip shows Must match).
Value Converts the value of a PLC cell into bytes in the set format and sends them. Converts the bytes into a value and stores it in the PLC cell.
Text Sends the characters in the PLC cells. Stores the received characters in the PLC cells. The number of characters can be stored separately.
Skip Sends the set number of fill bytes. Ignores the bytes at that position.
Length Sends the number of bytes in the specified fields. Checks it against the actual number of bytes, and marks where a variable-length frame ends.
Checksum Calculates and inserts it. Calculates it and compares (the chip shows Checksum · checked). If it is wrong, no value is stored.

Figure 8. The fields and the checksum range of the 9-byte CO2 reply frame

Fixed Bytes

In the Bytes box, enter hex bytes, text in double quotes and control character names, separated by spaces. The actual bytes and the number of bytes are shown below.

How to write Example Bytes
2-digit hex FF 01 86 FF 01 86
Text in double quotes (ASCII) “01R” 30 31 52
Control character names (NUL to US, SP, DEL) STX ETX CR LF ACK NAK 02 03 0D 0A 06 15
Repeat (*N, 1 to 255) 00*5 00 00 00 00 00

One chip can hold up to 255 bytes. Characters outside ASCII must be written in hex. FF is read as the hex byte 0xFF, not as the control character name for form feed.

Value

In PLC address, enter the cell that exchanges the value (for example D100) or a symbol name, and choose the Format.

Figure 9. Properties of a value field (binary, 2 bytes, big-endian)

Format Settings Example
Binary Bytes (1, 2 or 4), order (big-endian or little-endian), sign (unsigned or signed) 2 bytes, big-endian: 01 9A ↔ 410
ASCII decimal Digits (1 to 10; leave empty for a variable number), padding (zeros or spaces), sign (none, - for negative values only, or always +/-), decimal places (0 to 9), decimal point (not written or written) 4 digits, zero-padded: 100 ↔ “0100” · 2 decimal places, point written: 1234 ↔ “12.34”
ASCII hex Digits, letter case when sending (upper or lower) 4 digits: 6699 ↔ “1A2B”
BCD Bytes (1, 2 or 4), order 2 bytes: 1234 ↔ 12 34
REAL Order (4-byte real number) Exchanged with real-number cells (such as R).

Text

Length

Set the Format (1-byte binary, 2-byte binary, ASCII decimal or ASCII hex) and the Counted range (from one field number to another). When sending, the number of bytes in that range is inserted; when receiving, it is checked against the actual number of bytes. If the device adds a fixed number to the byte count, enter that number in Adjustment (added).

Checksum

Type Calculation
Sum (8 bit) · Sum (16 bit) The lower 8 or 16 bits of the sum of the bytes in the range
Two's complement sum The two's complement of the byte sum (the sum subtracted from 0x100)
One's complement sum The byte sum with all bits inverted
XOR (BCC) The XOR of the bytes in the range
CRC-16 One of the variants below, or custom values
LRC The two's complement of the sum of the bytes made from the ASCII hex characters in the range, two characters per byte (the Modbus ASCII method)

CRC-16 is calculated differently from device to device. When you choose a CRC-16 variant, the five boxes for the polynomial, initial value, reflect in, reflect out and final XOR are filled in, and the check value calculated from the string “123456789” is shown. Choose the variant whose check value matches the check value in the device manual. If you change any of the five boxes, the variant becomes Custom.

Figure 10. The list of CRC-16 variants in the checksum properties

Variant Polynomial Initial value Reflect (in, out) Final XOR Check value
MODBUS 8005 FFFF Yes 0000 4B37
CCITT-FALSE 1021 FFFF No 0000 29B1
XMODEM 1021 0000 No 0000 31C3
KERMIT 1021 0000 Yes 0000 2189
X-25 1021 FFFF Yes FFFF 906E
ARC 8005 0000 Yes 0000 BB3D
USB 8005 FFFF Yes FFFF B4C8
DNP 3D65 0000 Yes FFFF EA82

The default layout is little-endian for MODBUS and big-endian for every other variant.

End of a Received Frame

Where a received frame ends follows from the layout of the frame, and is shown above the chip strip as end: ….

Shown as What sets the end
end: fixed length N Every field of the frame has a set length.
end: length field (field N) The value of a length field earlier in the frame
end: end characters … The fixed bytes right after a variable-length field (for example CR LF or ETX)
end: after N ms of quiet No byte arrives for the End quiet time (ms) of the frame.
end: unknown The end cannot be determined, so it is shown as an item to fix.

End quiet time (ms) appears in the properties on the right when you select a frame, and can be set from 20 to 10000 ms (leave it empty to not use it). A last field of unknown length is treated as finished when the line stays quiet for this time. When partly received bytes are left and the line stays quiet for 100 ms or more, those bytes are also dropped from the front.

Figure 11. Four ways to find the end of a received frame, and matching again by shifting one byte

Preview and Decoding Received Bytes

The preview at the bottom of the window shows the bytes of the frame to send in hex (value cells as 0, length and checksum as calculated). Paste the bytes the device sent into the box below, one frame per line, and they are decoded as the frame to receive, with the value and result shown for each field. You can use both hex (FF 86 01) and fixed-byte notation such as STX “01” ETX.

Figure 12. CO2 reply bytes pasted and decoded as D100 = 410

Result Meaning
✓ … The fixed bytes, length and checksum all match. After ✓, the value to be stored is shown for each field (Figure 12). A frame with no value fields shows this frame matches.
… incomplete More bytes must arrive before the frame ends.
✕ … byte … differs from the fixed bytes The bytes do not belong to this frame.
✕ checksum — expected … The checksum is wrong. The calculated value is shown with it.
✕ … cannot be converted to a value The data is not a number or does not fit the cell.

Finding the Matching Checksum Variant

Use this when the device manual does not describe the checksum method or the check value does not match. Paste two or more frames that the device actually sent. When the checksum does not match, MP STUDIO 2 automatically tries each checksum type, CRC-16 variant, layout and range start, and lists the combinations that fit every sample on the Find the matching variant line, each with a Change to “…” button. With only one sample, a combination can match by chance, so Change to “…” is enabled only when there are two or more samples. The change goes into the definition only when you click OK.

Figure 13. "Two's complement sum · from byte 2" found from two samples

Transactions

When you select a transaction in the transaction list, its settings appear at the top center, with the chip strips of the frame to send and the frame to receive below them.

Figure 14. An ASCII station read transaction — the frame to send, the frame to receive and the error reply (NAK)

Item Default Description
Name Exchange pattern name The name of the transaction. It must be unique within the port.
Exchange pattern — Ask & receive, Receive only, Answer on receive or Send only
Start Period Ask & receive and Send only: Period, Ladder UPTRIG number or When a value changes · Receive only and Answer on receive: always listening
Period (ms) 1000 10 ms to 1 hour, counted from the previous start
Number 1 The UPTRIG number in the ladder (1 to 255)
Watched cell · Cells — When a value changes: sends when any of the values in the given number of cells (1 to 32), starting at the watched cell, changes. Also sends once when the PLC starts.
Reply timeout (ms) 200 How long Ask & receive waits for a reply (20 to 60000 ms)
Retries 2 How many times to send again when there is no reply or only wrong replies arrive (0 to 10)
Status word Empty (not used) The word address that receives the result of the transaction (see below)
Done bit Empty (not used) The bit address that turns on for one scan when the transaction ends
Received-count cell Empty (not used) Receive only and Answer on receive: a word address that increases by 1 each time a matching frame is received (65535 rolls over to 0)

Behavior by Exchange Pattern

Status Word

The status word is one word that shows the last result of the transaction.

Value (hex) Shown as Meaning
0000 Never succeeded yet This transaction has not finished even once since the PLC was turned on. The value is also 0 when this definition is not in the program on the PLC.
0001 Last transaction succeeded The last transaction finished without an error.
F000 No reply (F0) No reply arrived.
F100 Checksum error (F1) A reply with a wrong checksum arrived.
F200 Only bytes of another shape arrived (F2) Only bytes that do not match the layout of the frame to receive arrived.
F300 Line busy (F3) The frame could not be sent because transmission did not proceed.
F400 Value conversion failed (F4) The received value is not a number or does not fit the cell. F4 is also set when a value to send cannot be made into a frame.
F500 Receive buffer overflow (F5) The receive buffer overflowed while waiting for a reply.
F600 Error reply (F6) A frame defined as the error reply (NAK and so on) was received.

Figure 15. When the status word and the done bit change with the reply timeout and retries

Gaps Between Characters Are OK

This is set for each port, and is on for a new port.

Figure 16. How request bytes go out with Gaps between characters are OK turned on and off

Starting from the Ladder (UPTRIG)

When you set the Start of a transaction to Ladder UPTRIG number, the UPTRIG function in the ladder runs that transaction. Use this to send a setting command only when a button is pressed, or to read a value only when you need it.

  1. Set the Start of the transaction to Ladder UPTRIG number and enter the Number (1 to 255). If needed, also enter the done bit and the Status word.
  2. Click OK.
  3. Add an UPTRIG function to the ladder and enter the same number. Choose UPTRIG: Start User Protocol Trigger in the Serialport category of the Functions panel, or type UPTRIG 3 in the Insert Function window (Ctrl+E).

Figure 17. A rung that sends the transaction of trigger 3 (zero command) when M3 turns on

When you edit the number cell of UPTRIG, the trigger numbers defined in the user protocol are listed with their communication port and transaction name, and you can click one to choose it. Click Open user protocol in the same place to open the User Protocol window.

Figure 18. Choosing a defined trigger number in the UPTRIG number cell

Checks and Warnings

The User Protocol window marks items to fix by turning chips and boxes red or orange, and shows the first message in the bar at the bottom of the window. Click a message to go to that place, or click N more to expand all messages. While there are red items to fix, N to fix — the program is not built until they are fixed is shown and you cannot download. OK still works while there are items to fix, so you do not lose a definition in progress.

Figure 19. Items to fix in the bar at the bottom of the window and a box marked in red

The ladder and other features are checked for the following cases.

Case Shown as
PUT, GET, PUT_LEN or GET_LEN is used on the same communication port A red cell if the channel is a constant, a warning if it is a variable. If two features read the same receive buffer, the bytes they exchange get mixed up.
The ladder writes to a cell that the user protocol receives into Warning. The value written by the ladder is overwritten at the next reception.
Receiving cells overlap each other · a status, done or received-count cell overlaps a receiving cell Warning
Cells overlap those used by the Modbus device table or the device clock (RTC) block Warning
The port mode is not User Protocol · the mode is User Protocol but there is no definition · download is turned on Error. Clicking OK in the User Protocol window fixes most of these at once.
The PLC does not have enough RAM · the program does not fit in the flash Error. Reduce the ports, transactions or frames, or the memory area sizes.
A frame is longer than 255 bytes or has more than 32 fields Error
The send or receive buffer is smaller than a frame Error. Enlarge the buffer in the SERIAL field of Setting → Data Memory, or shorten the frame.

Cells used by the user protocol also appear in Find, Replace All, the Used at list of the Inspector and Used Data. When you change an address with Replace All, the user protocol cells change too.

Simulation

During simulation, a virtual device is attached to the user protocol of each communication port, and the Simulated device view is added to the window. You can check the ladder's behavior and the status word and done bit without a device.

Figure 20. The Simulated device view (the replies and traffic log of an ASCII station read transaction)

Only definitions confirmed with OK are simulated. A definition with items to fix is not simulated. Line busy (F3) and receive buffer overflow (F5) are not simulated, and the scan time and the processing time of the device are not included. The virtual device settings are not saved.

Monitoring

Status on Transaction Cards

If you open the User Protocol window during monitoring, the card of each transaction that has a status word shows the last result. Receive only and Answer on receive transactions also show the value of the received-count cell and the time since it last increased. Status is read only for the transactions of the selected communication port.

Figure 21. Transaction card status during monitoring (Read CO2 concentration — Last transaction succeeded)

The card shows the same text as the “Shown as” column of the Status Word table. When the status shown cannot be trusted, the reason is shown in gray.

Shown in gray Meaning
Different program on PLC The program on the PLC may differ from this document. The status is read after you download.
Not downloaded yet The transaction was not in the definition when monitoring started. Click OK, download and restart monitoring to read it.
PLC not answering · unknown · No new value · unknown No new values are arriving over the monitoring connection.
Limited monitoring · not read While there are program check errors, status words in user memory are not read.
Not written by the runtime The user protocol does not write this value. The program on the PLC is different, or the ladder writes to this cell.

During simulation, the card also shows Virtual. A transaction that has not been confirmed with OK shows Not applied yet, and a stopped simulation shows Simulation stopped · ….

Traffic Log

Turn on Comm log while monitoring for a port, click OK and download. During monitoring, the Traffic log view then shows the bytes the PLC sent and received on that communication port, one line each. Turning on the traffic log uses a little more RAM and flash on the PLC.

Figure 22. The Traffic log view (CO2 request and reply)

Direct PC Test

The direct PC test exchanges data directly with a device through a COM port on the PC (through a USB-RS485 or USB-RS232 converter, for example) instead of the PLC. Use it to check that the frames and checksum are right before you connect the device to the PLC. The test uses the definition as edited in the window, before OK, and is available only in the desktop app.

Figure 23. The Direct PC test view

Examples

In the examples below, the three templates are set on Comm Port 1 to Comm Port 3 of MPS-8A8R, one template per port. On other models, you can follow the same steps with only the communication port numbers changed.

Example 1. Reading a CO2 Sensor Every Second

In this example, a CO2 sensor connected to Comm Port 1 (9600 · 8-N-1) is read every second for its concentration, and a button input sends the zero command.

  1. In Setting → Communication Port, set Serial 1 to a baud rate of 9600, 8 data bits, no parity and 1 stop bit.
  2. In Setting → User Protocol…, select the Comm Port 1 tab and click the template CO2 / gas sensor — ask every second.
  3. Click OK. Comm Port 1 changes to User Protocol mode.
  4. Add a rung to the ladder that sends the zero command.
Rung Input Function What it does
0 M3 (NO) UPTRIG 3 Sends the zero command once when M3 turns on.
Frame Bytes
CO2 request FF 01 86 00 00 00 00 00 79 (fixed FF, 01, 86 and 00*5 + two's complement sum from byte 2)
CO2 reply FF 86 + concentration in 2 bytes (big-endian → D100) + 4 skipped bytes + two's complement sum
Zero command FF 01 87 00 00 00 00 00 78

When the sensor sends FF 86 01 9A 00 00 00 00 DF, D100 becomes 410 (ppm), the status word D101 becomes 0001 (1 in decimal), and the done bit M100 turns on for one scan.

Figure 24. Monitoring Example 1 (D100 concentration, D101 status and M100 done bit in the Watch panel)

Example 2. Receiving Text from a Barcode Reader

In this example, a barcode reader is connected to Comm Port 2 (9600 · 8-N-1), and the scanned text is received starting at D200.

  1. Set Serial 2 to 9600 · 8-N-1.
  2. On the Comm Port 2 tab of the User Protocol window, choose the template Barcode / QR reader and click OK.
Item Value
Frame “Barcode” Text (D200 · up to the end character · at most 32 · received length in D190) + fixed CR LF
Transaction “Receive barcode” Receive only · always listening · done bit M101 · received-count cell D191

When the reader sends ILOGICS-1234 followed by CR LF, D190 becomes 12, and the characters are stored from D200 on, two per word (D200 = I and L). M101 turns on for one scan, and D191 increases by 1. An empty line (only CR LF) is received as a frame with 0 characters.

Figure 25. The traffic log of Example 2 (text view) and the received count

Example 3. Reading an Inverter Value with ASCII Station Commands

In this example, an ASCII command device with station number 01 is connected to Comm Port 3 (9600 · 7-E-1), and the value at address 100 is read into D11 every 0.5 seconds.

  1. Set Serial 3 to a baud rate of 9600, 7 data bits, even parity and 1 stop bit.
  2. On the Comm Port 3 tab of the User Protocol window, choose the template Inverter / temperature controller (ASCII station commands) and click OK.
  3. Add a rung to the ladder that sets the address to read.
Rung Input Function What it does
0 @ON MOVE 100 D10 Puts the address to read, 100, in D10.
Frame Layout Example
Station request STX “01” “R” + address (ASCII decimal, 4 digits · D10) + ETX + sum (8 bit) as 2 ASCII hex digits (from byte 2) 02 “01R0100” 03 “77”
Station reply ACK “01” “R” + value (ASCII hex, 4 digits · D11) + ETX + sum (8 bit) as 2 ASCII hex digits (from byte 2) 06 “01R03E8” 03 “96” → D11 = 1000
Station error (NAK) NAK “01” ETX When it is received, the status word D12 = F600

The transaction “Read station” is Ask & receive, with a period of 500 ms, a reply timeout of 300 ms, 3 retries and the status word D12. In this template, Gaps between characters are OK is off, so the request is sent in one go.

Figure 26. The traffic log of Example 3 (Both view — hex and control characters)

Troubleshooting

Symptom What to check
The status word stays at 0000 Check that the downloaded program contains the user protocol. For a transaction started by a trigger, also check that the UPTRIG input in the ladder changed from off to on.
F000 (no reply) Use the direct PC test to check whether the device answers the same frame. If it does, check the communication port number and terminals (RS-232, RS-485 or UART), the + and - wiring of RS-485, and the communication settings. If the device is slow to reply, increase the Reply timeout (ms).
F100 (checksum error) Paste two or more frames that the device sent and check them with Finding the Matching Checksum Variant.
F200 (only bytes of another shape) Paste the bytes the device sent into the preview to see which byte differs. Check the station number, the header bytes, the data bits (7 or 8) and the parity.
F400 (value conversion failed) In the preview, check the format of the value field (digits, decimal places, hex) and the size of the PLC cell.
F500 (receive buffer overflow) If the scan time is long for the baud rate, more bytes than the receive buffer holds pile up within one scan. Shorten the scan time, or enlarge the receive buffer in the SERIAL field of Setting → Data Memory. With a 128-byte buffer at 115200 bps, the scan time must not exceed about 11 ms.
Requests go out but the device does not answer (gaps between characters) The device may not accept gaps between characters. Turn off Gaps between characters are OK for the port, download and check again.
Only the end of a long barcode is stored When a code longer than the maximum number of characters of the text field arrives, only its last characters, up to that maximum, are stored. Set N larger than the longest code.
Values are stored even though the line is noisy The PLC does not check for parity errors. Define frames that include a checksum.
The port does not open in the direct PC test Check that monitoring, download or another program is not using that COM port.

Things to Check

← Back to the MP STUDIO 2 User Manual