Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Device Communication

The PLC runtime communicates with external devices (I/O racks, VFDs, sensors) through native function blocks. Device types are derived from YAML profiles and appear as normal function blocks in your ST code — with full IDE support (code completion, type checking, debugger variable expansion).

How it works

  1. You create a device profile (YAML file) that describes the device’s registers — field names, data types, directions, and Modbus addresses.

  2. The runtime discovers profiles in your project’s profiles/ directory and makes them available as function block types.

  3. In your ST program, you declare instances of these types and call them each scan cycle. The runtime handles the actual serial communication.

Quick example

PROGRAM Main
VAR
    serial   : SerialLink;
    io_rack  : MyIoModule;
END_VAR
    (* Open the serial port — runs once on first call *)
    serial(port := '/dev/ttyUSB0', baud := 9600, parity := 'N',
           data_bits := 8, stop_bits := 1);

    (* Read/write I/O every 50ms *)
    io_rack(link := serial.port, slave_id := 1, refresh_rate := T#50ms);

    (* Use the I/O fields *)
    io_rack.DO_0 := io_rack.DI_0;
    io_rack.AO_0 := io_rack.AI_0 * 2;
END_PROGRAM

Supported protocols

ProtocolStatusLink typeCrate
SimulatedWorkingNone (in-memory)st-comm-sim
Modbus RTUWorkingRS-485/RS-232 serialst-comm-modbus
Modbus TCPPlannedTCP/IP
EtherNet/IPPlannedTCP/IP

Architecture

┌──────────────────┐     ┌────────────────────┐
│  ST Program       │     │  Device Profile     │
│                   │     │  (YAML)             │
│  serial(...)      │     │  - register map     │
│  io_rack(...)     │     │  - field types      │
│  io_rack.DO_0 :=  │     │  - scaling/offset   │
└────────┬──────────┘     └─────────┬───────────┘
         │                          │
         ▼                          ▼
┌──────────────────────────────────────────────┐
│  NativeFb Registry                           │
│  (built from profiles at startup)            │
│                                              │
│  SerialLink → opens /dev/ttyUSB0             │
│  MyIoModule → Modbus RTU FC01-FC10           │
└──────────────────────────────────────────────┘
         │
         ▼
┌──────────────────────────────────────────────┐
│  Serial Transport (RS-485 bus)               │
│  - inter-frame timing                        │
│  - bus access mutex (multiple devices share)  │
│  - CRC16 validation                          │
└──────────────────────────────────────────────┘

Diagnostic fields

Every device FB includes diagnostic fields that update automatically:

FieldTypeDescription
connectedBOOLTRUE if the device is responding
error_codeINT0 = OK, non-zero = error
io_cyclesUDINTNumber of successful I/O cycles
last_response_msREALLast round-trip time in milliseconds
IF NOT io_rack.connected THEN
    (* Handle communication failure *)
    alarm := TRUE;
END_IF;

Next steps

  • Device Profiles — how to create a YAML profile for your hardware
  • Modbus RTU — detailed Modbus RTU setup and wiring guide