-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rs
131 lines (108 loc) · 3.91 KB
/
example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use chrono::Local;
use colored::Colorize;
use lib::devices::joystick::device::{Joystick, JoystickType};
use lib::devices::joystick::requests::{JoystickRequest, JoystickResponse};
use lib::devices::vfd::device::Vfd;
use lib::devices::vfd::encoder::{VfdCommands, FRECON, MEGMEET};
use lib::devices::vfd::requests::{VfdRequest, VfdResponse};
use lib::poller::ModbusPoller;
use lib::router::StdRouter;
use lib::soft_request::{SoftRequest, SoftResponse};
use lib::traits::device::Device;
use serial_thread::serial::Baud115200;
use std::time::Duration;
use std::{env, io};
fn vfd(
id: u8,
vfd: VfdCommands,
router: &mut StdRouter<SoftRequest, SoftResponse>,
poller: &mut ModbusPoller<VfdRequest, VfdResponse>,
list: &mut Vec<Vfd>,
) {
let mut vfd_list = Vfd::new(id.into(), vfd, false);
vfd_list.connect_poller(poller);
vfd_list.connect_router(router);
list.push(vfd_list);
}
fn joystick(
id: u8,
joystick_type: JoystickType,
router: &mut StdRouter<SoftRequest, SoftResponse>,
poller: &mut ModbusPoller<JoystickRequest, JoystickResponse>,
) -> Joystick {
let mut joy = Joystick::new(id.into(), joystick_type);
joy.connect_poller(poller);
joy.connect_router(router);
joy
}
#[tokio::main]
async fn main() {
let port0 = "/dev/ttyXR6";
let port1 = "/dev/ttyXR7";
let port2 = "/dev/ttyXR2";
let port3 = "/dev/ttyXR3";
let port4 = "/dev/ttyXR4";
let mut router = StdRouter::new(io::stdin(), io::stdout());
// Serial port #0
let poller_0 = {
let mut poller = ModbusPoller::new(port0, Baud115200, Some(1), Some(1), Some(5));
joystick(0x05, JoystickType::Joystick, &mut router, &mut poller).start();
poller
};
poller_0.start();
// Serial port #1
let poller_1 = {
let mut poller = ModbusPoller::new(port1, Baud115200, Some(1), Some(1), Some(5));
joystick(0x06, JoystickType::Joystick, &mut router, &mut poller).start();
poller
};
poller_1.start();
// Serial port #2
let poller_2 = {
let mut poller = ModbusPoller::new(port2, Baud115200, Some(1), Some(1), Some(5));
let mut vfd_list: Vec<Vfd> = Vec::new();
vfd(10, MEGMEET, &mut router, &mut poller, &mut vfd_list);
vfd(11, MEGMEET, &mut router, &mut poller, &mut vfd_list);
vfd(60, MEGMEET, &mut router, &mut poller, &mut vfd_list);
vfd(61, MEGMEET, &mut router, &mut poller, &mut vfd_list);
for vfd in vfd_list {
vfd.start()
}
poller
};
poller_2.start();
// Serial port #3
let poller_3 = {
let mut poller = ModbusPoller::new(port3, Baud115200, Some(3), Some(6), Some(6));
let mut vfd_list: Vec<Vfd> = Vec::new();
vfd(12, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(20, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(21, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(26, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(27, FRECON, &mut router, &mut poller, &mut vfd_list);
for vfd in vfd_list {
vfd.start()
}
poller
};
poller_3.start();
// Serial port #4
let poller_4 = {
let mut poller = ModbusPoller::new(port4, Baud115200, Some(3), Some(6), Some(6));
let mut vfd_list: Vec<Vfd> = Vec::new();
vfd(30, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(31, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(40, MEGMEET, &mut router, &mut poller, &mut vfd_list);
vfd(43, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(50, FRECON, &mut router, &mut poller, &mut vfd_list);
vfd(51, MEGMEET, &mut router, &mut poller, &mut vfd_list);
for vfd in vfd_list {
vfd.start()
}
poller
};
poller_4.start();
tokio::time::sleep(Duration::from_millis(100)).await;
// start router
router.start().await;
}