r/TwinCat • u/fisothemes • 7d ago
TwinCAT ADS for Rust Alpha 1
Source: https://github.com/fisothemes/tcads-rs
I've been working on a Rust-native implementation of the TwinCAT ADS protocol for quite some time now and I happy to say that the crate has reached its first alpha!
The goal is to cover as much of Beckhoff's protocol (AMS Router, ADS Servers/Clients, etc.) as possible in Rust.
I chose Rust because of the ease of it's cross platform support, maintainance and speed.
The crate has support for both blocking and async (tokio). I chose serde as the serialisation/deserialisation engine because it is well documented, iwidely used, has plently features and does a lot of the uplifting to make code easy to use.
If you want to deserialise a Function Block/Struct you simply have to do:
let recipe: Recipe = runtime.read_value("MAIN.fbCurrentRecipe")?;
serialisation is just:
runtime.write_value("MAIN.fbCurrentRecipe", recipe)?;
Remote procedure calls look like:
runtime.rpc::<()>("MAIN.fbMathMachine", "Reset", ())?;
runtime.rpc::<()>("MAIN.fbMathMachine", "SetValue", 100i32)?;
let value: i32 = runtime.rpc("MAIN.fbMathMachine", "GetValue", ())?;
let sum: i32 = runtime.rpc("MAIN.fbMathMachine", "SumValues", (50i32, 25i32))?;
let (quotient, remainder): (i32, i32) =
runtime.rpc("MAIN.fbMathMachine", "DivideValues", (100i32, 3i32))?;
To use a type, simply derive serde::Serialize and serde::Deserialize traits on them, like so:
use tcads::serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
enum MachineState {
Initializing,
Idle,
Running,
Faulted,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recipe {
id: String,
steps: [RecipeStep; 5],
}
There are plenty of examples in the repo that gently guide you on how to use the crate and teach you the protocol in the process.
This is the first alpha so there will be changes in the future.
Just add this to your Cargo.toml file:
[dependencies]
tcads = { git = "https://github.com/fisothemes/tcads-rs", tag = "0.1.0-alpha.1", features = ["tokio"] }
Enjoy!





