use profirust::fdl;
use profirust::phy;
const MASTER_ADDRESS: u8 = 3;
const BUS_DEVICE: &'static str = "/dev/ttyUSB0";
const BAUDRATE: profirust::Baudrate = profirust::Baudrate::B500000;
fn main() -> ! {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.format_timestamp_micros()
.init();
log::info!("PROFIBUS Live List:");
let mut live_list = fdl::live_list::LiveList::new();
let mut fdl = fdl::FdlActiveStation::new(
fdl::ParametersBuilder::new(MASTER_ADDRESS, BAUDRATE)
.slot_bits(4000)
.max_retry_limit(3)
.gap_wait_rotations(1)
.build(),
);
let sleep_time = std::time::Duration::from_micros(3500);
log::warn!(
"This station has address #{}. No other station with this address shall be present.",
fdl.parameters().address
);
log::info!("Connecting to the bus...");
let mut phy = phy::SerialPortPhy::new(BUS_DEVICE, fdl.parameters().baudrate);
let mut last = profirust::time::Instant::now();
fdl.set_online();
loop {
fdl.poll(profirust::time::Instant::now(), &mut phy, &mut live_list);
let event = live_list.take_last_event();
match event {
Some(fdl::live_list::StationEvent::Discovered(station)) => {
log::info!("Discovered #{} ({:?})", station.address, station.state);
}
Some(fdl::live_list::StationEvent::Lost(station_address)) => {
log::info!("Lost station #{station_address}");
}
None => (),
}
if (profirust::time::Instant::now() - last).secs() > 5 {
let live_list: Vec<_> = live_list
.iter_stations()
.map(|addr| format!("#{}", addr))
.collect();
log::info!("Live Stations: {}", live_list.join(", "));
last = profirust::time::Instant::now();
}
std::thread::sleep(sleep_time);
}
}