cyclic_read/
cyclic_read.rs

1use slmp::{CPU, DataType, Device, DeviceType, MonitorDevice, PollingInterval, SLMP4EConnectionProps, SLMPConnectionManager, TypedDevice};
2
3const SLMP_PROPS: SLMP4EConnectionProps = SLMP4EConnectionProps {
4    ip: "192.168.3.10",
5    port: 5007,
6    cpu: CPU::Q,
7    serial_id: 0x0001,
8    network_id: 0x00,
9    pc_id: 0xff,
10    io_id: 0x03ff,
11    area_id: 0x00,
12    cpu_timer: 0x0010,
13};
14
15#[tokio::main]
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17
18    let manager = SLMPConnectionManager::new();
19
20    let cyclic_task = async |data| {
21        for x in data {
22            println!("{:?}", x);
23        }
24        println!();
25        Ok(())
26    };
27
28    manager.connect(SLMP_PROPS, cyclic_task).await?;
29
30    let target_devices = [
31        MonitorDevice {
32            inverval: PollingInterval::Fast,
33            device: TypedDevice {
34                device: Device { device_type: DeviceType::D, address: 4001 },
35                data_type: DataType::U16
36            },
37        },
38        MonitorDevice {
39            inverval: PollingInterval::Slow,
40            device: TypedDevice {
41                device: Device { device_type: DeviceType::D, address: 4005 },
42                data_type: DataType::U16
43            },
44        },
45        MonitorDevice {
46            inverval: PollingInterval::Meduim,
47            device: TypedDevice {
48                device: Device { device_type: DeviceType::D, address: 4006 },
49                data_type: DataType::U16
50            },
51        },
52        MonitorDevice {
53            inverval: PollingInterval::Meduim,
54            device: TypedDevice {
55                device: Device { device_type: DeviceType::D, address: 4007 },
56                data_type: DataType::U16
57            },
58        },
59    ];
60    manager.register_monitor_targets(SLMP_PROPS, &target_devices).await?;
61
62    tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
63
64    manager.disconnect(SLMP_PROPS).await?;
65
66    Ok(())
67}