cyclic_read/
cyclic_read.rs

1use slmp::{CPU, DataType, Device, DeviceType, MonitorRequest, 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 cycle_ms: u64 = 100;
22    let cyclic_task = async |data| {
23        for x in data {
24            println!("{:?}", x);
25        }
26        println!();
27        Ok(())
28    };
29
30    manager.connect(&connection_props, cyclic_task, cycle_ms).await?;
31
32    let target_devices = [
33        MonitorRequest {
34            connection_props: &connection_props,
35            monitor_device: TypedDevice {
36                device: Device { device_type: DeviceType::D, address: 4001 },
37                data_type: DataType::U16
38            }
39        },
40        MonitorRequest {
41            connection_props: &connection_props,
42            monitor_device: TypedDevice {
43                device: Device { device_type: DeviceType::D, address: 4002 },
44                data_type: DataType::U16
45            }
46        },
47        MonitorRequest {
48            connection_props: &connection_props,
49            monitor_device: TypedDevice {
50                device: Device { device_type: DeviceType::D, address: 4003 },
51                data_type: DataType::U16
52            }
53        },
54    ];
55    manager.register_monitor_targets(&target_devices).await?;
56
57    tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
58
59    manager.disconnect(&connection_props).await?;
60
61    Ok(())
62}