use env_logger::Env;
use ethercrab::{MainDevice, MainDeviceConfig, PduStorage, Timeouts, std::ethercat_now};
use std::{str::FromStr, sync::Arc};
const MAX_SUBDEVICES: usize = 128;
const MAX_PDU_DATA: usize = PduStorage::element_size(1100);
const MAX_FRAMES: usize = 16;
const PDI_LEN: usize = 64;
static PDU_STORAGE: PduStorage<MAX_FRAMES, MAX_PDU_DATA> = PduStorage::new();
fn main() {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let interface = std::env::args()
.nth(1)
.expect("Provide network interface as first argument.");
log::info!("Discovering EtherCAT devices on {}...", interface);
let (tx, rx, pdu_loop) = PDU_STORAGE.try_split().expect("can only split once");
let maindevice = Arc::new(MainDevice::new(
pdu_loop,
Timeouts::default(),
MainDeviceConfig {
dc_static_sync_iterations: 0,
..MainDeviceConfig::default()
},
));
smol::block_on(async {
#[cfg(target_os = "windows")]
std::thread::spawn(move || {
ethercrab::std::tx_rx_task_blocking(
&interface,
tx,
rx,
ethercrab::std::TxRxTaskConfig { spinloop: false },
)
.expect("TX/RX task")
});
#[cfg(not(target_os = "windows"))]
smol::spawn(ethercrab::std::tx_rx_task(&interface, tx, rx).expect("spawn TX/RX task"))
.detach();
let group = maindevice
.init_single_group::<MAX_SUBDEVICES, PDI_LEN>(ethercat_now)
.await
.expect("Init");
log::info!("Discovered {} SubDevices", group.len());
for subdevice in group.iter(&maindevice) {
log::info!(
"--> SubDevice {:#06x} name {}, description {}, {}",
subdevice.configured_address(),
subdevice.name(),
subdevice
.description()
.await
.expect("Failed to read description")
.unwrap_or(heapless::String::from_str("[no description]").unwrap()),
subdevice.identity()
);
}
});
log::info!("Done.");
}