random_access/
random_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    // Write
22    let wr_data = [
23        DeviceData{device: Device{device_type: DeviceType::D, address: 25}, data: TypedData::U16(10)},
24        DeviceData{device: Device{device_type: DeviceType::D, address: 35}, data: TypedData::U32(80000)},
25        DeviceData{device: Device{device_type: DeviceType::D, address: 20}, data: TypedData::U16(20)},
26        DeviceData{device: Device{device_type: DeviceType::D, address: 30}, data: TypedData::I16(-40)},
27        DeviceData{device: Device{device_type: DeviceType::M, address: 10}, data: TypedData::Bool(true)},
28        DeviceData{device: Device{device_type: DeviceType::M, address: 11}, data: TypedData::Bool(false)},
29        DeviceData{device: Device{device_type: DeviceType::M, address: 12}, data: TypedData::Bool(true)},
30        DeviceData{device: Device{device_type: DeviceType::M, address: 13}, data: TypedData::Bool(true)},
31        DeviceData{device: Device{device_type: DeviceType::D, address: 40}, data: TypedData::from(("test", 4))},
32        DeviceData{device: Device{device_type: DeviceType::D, address: 50}, data: TypedData::from(("TEST", 4))},
33    ];
34    client.random_write(&wr_data).await.unwrap();
35
36    // Read
37    let devices = [
38        TypedDevice{device: Device{device_type: DeviceType::D, address: 40}, data_type: DataType::String(10)},
39        TypedDevice{device: Device{device_type: DeviceType::D, address: 50}, data_type: DataType::String(10)},
40        TypedDevice{device: Device{device_type: DeviceType::D, address: 25}, data_type: DataType::U16},
41        TypedDevice{device: Device{device_type: DeviceType::D, address: 20}, data_type: DataType::U16},
42        TypedDevice{device: Device{device_type: DeviceType::D, address: 35}, data_type: DataType::U32},
43        TypedDevice{device: Device{device_type: DeviceType::D, address: 30}, data_type: DataType::I16},
44        TypedDevice{device: Device{device_type: DeviceType::M, address: 13}, data_type: DataType::Bool},
45        TypedDevice{device: Device{device_type: DeviceType::M, address: 12}, data_type: DataType::Bool},
46        TypedDevice{device: Device{device_type: DeviceType::M, address: 11}, data_type: DataType::Bool},
47        TypedDevice{device: Device{device_type: DeviceType::M, address: 10}, data_type: DataType::Bool},
48        TypedDevice{device: Device{device_type: DeviceType::M, address: 10}, data_type: DataType::BitArray16},
49    ];
50
51    let ret = client.random_read(&devices).await.unwrap();
52    println!("\nDevice access:");
53    for x in ret {
54        println!("{:?}", x);
55    }
56    println!();
57
58    client.close().await;
59}