Getting started
Run a local IEC 104 interrogation, keep a Tokio session connected, and serve points.
A controlling station (client) asks an outstation (server) for its current points. IEC 104 carries these messages over TCP. Each application service data unit (ASDU) identifies the station, the type of data and why it was sent.
To inspect a frame or run the simulator with Homebrew, follow the CLI installation guide.
Run a loopback interrogation#
From a source checkout with Rust 1.88 or newer, run the codec example and then the station example.
cargo run --example asdu
cargo run --example clientWith no address argument, the client starts an outstation on loopback, starts data transfer and prints the points returned by a general interrogation. To inspect the public API and examples for every feature locally, run cargo doc --all-features --no-deps --open.
The demo reports these points among its events:
CA 1 IOA 100 = ON, interrogated by station (20)
CA 1 IOA 101 = OFF, interrogated by station (20)
CA 1 IOA 200 = 21.5, interrogated by station (20)Print a station event with println!("{event}"). A value such as 21.5 [NT IV] carries both the not-topical and invalid flags. A nonzero originator, test or negative indication, and a time tag appear on the same point line when present. This point carries all three cause indicators and a station time tag.
CA 1 IOA 200 = 21.5 [NT IV], interrogated by station (20), negative, test, originator 3 at 2026-11-18 09:46:40.000
CA 1 station interrogation completeUse the typed event fields for filtering, storage and serialization.
Enable cs101 for the FT 1.2 link, serial for a serial port, and tokio for async stations.
Stay connected with Tokio#
A Session connects, starts data transfer and interrogates on every (re)connect. After a loss it reconnects with exponential backoff (1 s doubling to 60 s by default). Link state and points arrive in order on one stream.
use iec60870::{prelude::*, Session, SessionEvent};
use std::time::Duration;
let (session, mut events) = Session::builder("10.0.0.5:2404")
.interrogate(CommonAddress(1))
.interrogation_period(Duration::from_secs(900))
.spawn();
while let Some(event) = events.next().await {
println!("{event}"); // connected to …, points, disconnected: … retrying in 1s
}SessionEvents also implements futures_core::Stream. Clones of session send read, interrogate, clock_sync, command and select_and_execute from any task, and each resolves with its outcome. A request fails at once while the session is disconnected, and fails with the disconnect if the connection drops mid-request. Requests are never sent again on a later connection. The event queue is bounded (event_capacity, default 1024): a consumer that stops reading pauses reading from the outstation instead of growing memory. cargo run --features tokio --example session runs a complete monitor.
For a non-default link, station configuration, clock or TLS, start from Client::builder(). The same builder ends in connect (blocking), connect_async (one Tokio connection) or session(addr).
Time tags and time zones#
Database::insert sends spontaneous events with a CP56Time2a time tag, so a short float goes out as M_ME_TF_1. Use .time_format(TimeFormat::None) to send M_ME_NC_1 instead. CP56Time2a carries no offset. If the stations agree on local time, TimeZone::CentralEurope reads the summer-time (SU) flag to choose between UTC+1 and UTC+2, and sets the flag by the EU rule:
let utc_millis = point.time.and_then(|t| t.to_unix_millis_in(TimeZone::CentralEurope));
let clock = TimeZone::CentralEurope.clock(SystemClock); // a station that publishes local timeTimeZone::Fixed(minutes) covers zones without summer time.
Test a monitor-only client#
Every command the outstation decided is a ServerEvent::CommandDecision on the server handle. A test asserts that none arrived:
while let Some(event) = handle.next_event(Duration::from_millis(100)).await {
assert!(!matches!(event, ServerEvent::CommandDecision { .. }));
}Serve a point on an embedded board#
The embedded example registers temperature at common address 1, information object address 200. From the checkout, cross-compile the library and run the same station logic on your development machine.
rustup target add thumbv7em-none-eabihf
cargo build --locked --manifest-path examples/embedded/Cargo.toml --target thumbv7em-none-eabihf
cargo test --locked --manifest-path examples/embedded/Cargo.tomlThe host test starts data transfer, sends a read and verifies a 21.5 °C reply. In firmware, supply the allocator, panic handler, clock and transport. The board loop feeds received bytes to the station and drains its outputs. See examples/embedded/README.md in the checkout for the firmware integration steps.
Gateway point sources#
Both blocking and Tokio servers accept a PointSource. A map is enough for a monitoring gateway.
use iec60870::{Server, prelude::*};
use std::collections::BTreeMap;
let ca = CommonAddress(1);
let points = BTreeMap::from([((ca, Ioa(200)), PointEntry::new(21.5_f32))]);
let server = Server::bind("127.0.0.1:0", points)?;
let handle = server.spawn(iec60870::station::RefuseCommands)?;
handle.update(ca, Ioa(200), 22.0_f32)?;
handle.shutdown();Keep the point addresses and kinds fixed while serving. Fetch slow upstream data outside the station thread. Submit readings through the handle so the station timestamps each update and queues spontaneous events. For a custom source, implement ordered enumeration and lookup against the same local process image.
A server command handler can use handle.peer(request.connection) to include the remote socket address in its audit log. Blocking and Tokio handles both return Option<SocketAddr>. Use ServerHandle::next_event_with_peer when a blocking event consumer also needs the address after disconnection.
Long-running commands#
A handler that answers Decision::Pending gets ACTCON sent at once. The station sends ACTTERM when the application calls terminate(request.operation, success) on the server handle, or on ControlledStation without I/O. Pass false to send a negative termination. A successful termination asks the handler's feedback first.
use iec60870::{Server, prelude::*};
use std::sync::mpsc;
let mut db = Database::new();
db.command(CommonAddress(1), Ioa(5000), CommandKind::Double);
let (tx, operations) = mpsc::channel();
let handle = Server::bind("127.0.0.1:0", db)?.spawn(move |r: &CommandRequest| {
let _ = tx.send(r.operation);
Decision::Pending
})?;
for operation in operations {
// ... the breaker reports its end position ...
handle.terminate(operation, true)?;
}Each operation terminates once. If its connection closes first, the station emits ServerEvent::CommandAbandoned, and a later terminate fails with TerminateError::Abandoned without sending anything. A connection can have at most MAX_PENDING_COMMANDS (16) pending executes. The station refuses further executes on that connection without calling the handler.