Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{sync::Arc, time::Duration};
- use iced::{
- widget::{column, container, row, svg, tooltip, Button, Tooltip},
- Alignment, Element,
- Length::{self, Fill},
- Task, Theme,
- };
- use tokio::{runtime::Builder, sync::Mutex};
- use tokio_serial::SerialPort;
- #[derive(Debug)]
- pub struct Keypad {
- pub serial_port: Arc<Mutex<Box<dyn SerialPort>>>,
- }
- impl Clone for Keypad {
- fn clone(&self) -> Self {
- Keypad {
- serial_port: self.serial_port.clone(),
- }
- }
- }
- pub fn new() -> (Self, Task<Message>) {
- let initial_screen = Screen::default(); // Установка стартового экрана
- let runtime = Builder::new_current_thread().enable_all().build().unwrap();
- let port_name = runtime.block_on(async { get_keypad_port().await });
- // let serial_port: Rc<Box<dyn SerialPort>> = tokio_serial::new(port_name, 115_200)
- // .timeout(Duration::from_millis(10))
- // .open()
- // .expect("Failed to open port")
- // .into();
- let serial_port: Arc<Mutex<Box<dyn SerialPort>>> = Arc::new(Mutex::new(
- tokio_serial::new(port_name, 115_200)
- .timeout(Duration::from_millis(10))
- .open()
- .expect("Failed to open port"),
- ));
- let _ = runtime.block_on(async {
- let mut port = serial_port.lock().await;
- port.write_data_terminal_ready(true)
- });
- let keypad = Keypad { serial_port };
- (
- Self {
- screen: initial_screen,
- keypad,
- },
- Task::none(),
- )
- }
- async fn process_ports(ports: Vec<String>) -> String {
- let mut result = String::new();
- let write_data_array: [u16; ARRAY_LEN] = [101, 0, 0, 0, 0, 0, 0, 0, 0];
- for port_name in ports.iter() {
- let port = match new(port_name, 115_200)
- .timeout(Duration::from_millis(10))
- .open()
- {
- Ok(port) => Arc::new(Mutex::new(port)),
- Err(err) => {
- eprintln!("Ошибка открытия порта {}: {}", port_name, err);
- continue;
- }
- };
- // Пишем данные в порт
- match write_keypad_port(port.clone(), write_data_array).await {
- Ok(_) => {
- match read_keypad_port(port.clone()).await {
- Ok(_) => result.push_str(port_name),
- Err(err) => {
- eprintln!("Ошибка чтения из порта {}: {}", port_name, err);
- continue;
- }
- };
- }
- Err(err) => {
- eprintln!("Ошибка записи в порт {}: {}", port_name, err);
- continue;
- }
- };
- }
- result
- }
- #[cfg(target_os = "linux")]
- pub async fn get_keypad_port() -> String {
- let ports = tokio_serial::available_ports().expect("No ports found!");
- let mut vec_ports_str = Vec::new();
- let regex = Regex::new(r"/dev/ttyACM\d+").unwrap();
- for port in ports.into_iter() {
- let port_name_string = port.port_name;
- vec_ports_str.push(port_name_string);
- }
- let filtered_ports: Vec<String> = vec_ports_str
- .into_iter()
- .filter(|port_name| regex.is_match(port_name))
- .collect();
- process_ports(filtered_ports).await
- }
- #[cfg(target_os = "windows")]
- pub async fn get_keypad_port() -> String {
- let mut vec_ports_str: Vec<String> = Vec::new();
- let ports = tokio_serial::available_ports().expect("No ports found!");
- for port in ports.into_iter() {
- let port_name = port.port_name;
- vec_ports_str.push(port_name);
- }
- let filtered_ports: Vec<String> = vec_ports_str
- .into_iter()
- .filter(|port| !port.contains("COM1"))
- .collect();
- process_ports(filtered_ports).await
- }
- pub async fn write_keypad_port(
- port: Arc<Mutex<Box<dyn SerialPort>>>,
- write_data_array: [u16; ARRAY_LEN],
- ) -> Result<String, tokio_serial::Error> {
- let write_data = command_to_string(&write_data_array).await;
- let mut port_lock = port.lock().await;
- port_lock.write_all(write_data.as_bytes())?;
- Ok(String::new())
- }
- pub async fn read_keypad_port(
- port: Arc<Mutex<Box<dyn SerialPort>>>,
- ) -> Result<String, tokio_serial::Error> {
- let mut serial_buf = vec![0; 256];
- let mut port_lock = port.lock().await;
- match port_lock.read(serial_buf.as_mut_slice()) {
- Ok(_m) => {
- let buf = String::from_utf8(serial_buf).unwrap();
- let trimmed_buf = buf.trim_end_matches(|c: char| c.is_control()).to_string();
- let ok_trimmed_buf = Ok(trimmed_buf);
- println!("{:?}", ok_trimmed_buf);
- ok_trimmed_buf
- }
- Err(err) => {
- // eprintln!("Ошибка чтения из порта: {}", err);
- Err(tokio_serial::Error::from(err))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement