cyclic_read/
cyclic_read.rs

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