bulk_access/
bulk_access.rs

1use slmp::*;
2
3#[tokio::main]
4async fn main() {
5
6    let connection_props: SLMP4EConnectionProps = SLMP4EConnectionProps {
7        ip: String::from("192.168.3.10"),
8        port: 5007,
9        cpu: CPU::R,
10        serial_id: 0x0001,
11        network_id: 0x00,
12        pc_id: 0xff,
13        io_id: 0x03ff,
14        area_id: 0x00,
15        cpu_timer: 0x0010,
16    };
17
18    let mut client = SLMPClient::new(connection_props);
19    client.connect().await.unwrap();
20
21    // Word data
22    let start_device: Device = Device{device_type: DeviceType::D, address: 0};
23
24    let data: Vec<TypedData> = [0u16; 120]
25        .iter()
26        .enumerate()
27        .map(|(j, _)| TypedData::U16(j as u16))
28        .collect();
29
30    client.bulk_write(start_device, &data).await.unwrap();
31
32    let ret: Vec<DeviceData> = client.bulk_read(start_device, 8, DataType::U16).await.unwrap();
33    println!("\nDevice access:");
34    for x in ret {
35        println!("{:?}", x);
36    }
37
38    // Float value
39    let start_device: Device = Device{device_type: DeviceType::D, address: 100};
40
41    let data: Vec<TypedData> = vec![
42        TypedData::from(100.0f64),
43        TypedData::from(200.0f64),
44    ];
45
46    client.bulk_write(start_device, &data).await.unwrap();
47
48    let ret: Vec<DeviceData> = client.bulk_read(start_device, 2, DataType::F64).await.unwrap();
49    println!("\nDevice access:");
50    for x in ret {
51        println!("{:?}", x);
52    }
53
54    // String data
55    let start_device: Device = Device{device_type: DeviceType::D, address: 10};
56
57    let device_size: u8 = 10;
58    let data: Vec<TypedData> = vec![
59        TypedData::from(("ABcd", device_size)),
60        TypedData::from(("character", device_size)),
61        TypedData::from(("日本語", device_size)),
62    ];
63
64    client.bulk_write(start_device, &data).await.unwrap();
65
66    let ret: Vec<DeviceData> = client.bulk_read(start_device, 3, DataType::String(10)).await.unwrap();
67    println!("\nDevice access:");
68    for x in ret {
69        println!("{:?}", x);
70    }
71
72    // Bit data
73    let start_device: Device = Device{device_type: DeviceType::M, address: 0};
74    let data = vec![
75        TypedData::Bool(true),
76        TypedData::Bool(false),
77        TypedData::Bool(false),
78        TypedData::Bool(true),
79    ];
80
81    client.bulk_write(start_device, &data).await.unwrap();
82
83    let ret: Vec<DeviceData> = client.bulk_read(start_device, data.len(), DataType::Bool).await.unwrap();
84    println!("\nBit access:");
85    for x in ret {
86        println!("{:?}", x);
87    }
88    println!();
89
90    client.close().await;
91}