Rust EtherCAT equivalent to ecrt_request_master()

Whereas in C++ you would use

ethercat_request_master.cpp
// Request master
master = ecrt_request_master(0);
if (!master) {
    std::cerr << "Failed to request master!" << std::endl;
    return -1;
}

in Rust, using the ethercat crate, you would do it like this:

ethercat_request_master.rs
use ethercat::{Master, MasterAccess};

// You may also use MasterAccess::ReadOnly if you don't need write access
// 0 is the index of the master to open (usually 0)
let mut master = Master::open(0, MasterAccess::ReadWrite)?;
// Reserve (aka "request") the master (for exclusive access, basically)
master.reserve()?;

Note that the Master::open function opens the master device, and the reserve method is used to request exclusive access to the master.

The C++ function ecrt_request_master combines both opening (ecrt_open_master()) and reserving (ecrt_master_reserve()) the master, while in Rust these are two separate steps. Check out the ecrt_request_master() source code for more details.


Check out similar posts by category: Rust, EtherCAT