make sending commands modular, and bring back menu selection
This commit is contained in:
79
src/main.rs
79
src/main.rs
@@ -1,10 +1,12 @@
|
|||||||
mod xm;
|
mod xm;
|
||||||
mod hw;
|
mod hw;
|
||||||
|
|
||||||
use xm::command::{send_command, XMCommand};
|
use text_io::scan;
|
||||||
|
use xm::packet::XMPacket;
|
||||||
|
use xm::command::{XMCommand, send_command};
|
||||||
use hw::serial::xm_pcr_serial;
|
use hw::serial::xm_pcr_serial;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::io;
|
use std::io::{self, Write};
|
||||||
|
|
||||||
fn main() -> Result<(), io::Error> {
|
fn main() -> Result<(), io::Error> {
|
||||||
let port_name: &str = "/dev/ttyUSB0"; // Adjust as per your system
|
let port_name: &str = "/dev/ttyUSB0"; // Adjust as per your system
|
||||||
@@ -19,14 +21,77 @@ fn main() -> Result<(), io::Error> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match send_command(&mut *port, XMCommand::get_radio_id(), timeout) {
|
println!("Select the command you want to send: \n");
|
||||||
Ok(serial_number_str) => {
|
println!("1. Power Radio On");
|
||||||
println!("Received valid XMRadioID: {}", serial_number_str);
|
println!("2. Power Radio Off");
|
||||||
|
println!("3. Select Channel");
|
||||||
|
println!("4. Get Radio ID");
|
||||||
|
println!("0. Exit");
|
||||||
|
|
||||||
|
let a: i32;
|
||||||
|
scan!("{}", a);
|
||||||
|
|
||||||
|
match a {
|
||||||
|
1 => {
|
||||||
|
match send_command(&mut *port, XMCommand::power_on(), timeout, false) {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Power on command sent.");
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
2 => {
|
||||||
eprintln!("Error: {}", e);
|
match send_command(&mut *port, XMCommand::power_off(), timeout, false) {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Power off command sent.");
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
3 => {
|
||||||
|
println!("Enter the channel number (0-255): ");
|
||||||
|
let channel: u8;
|
||||||
|
scan!("{}", channel);
|
||||||
|
|
||||||
|
match send_command(&mut *port, XMCommand::select_channel(channel), timeout, false) {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Channel selection command sent.");
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
4 => {
|
||||||
|
match send_command(&mut *port, XMCommand::get_radio_id(), timeout, true) {
|
||||||
|
Ok(Some(serial_number_str)) => {
|
||||||
|
println!("Received valid XMRadioID: {}", serial_number_str);
|
||||||
|
},
|
||||||
|
Ok(None) => {
|
||||||
|
println!("No response expected.");
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
0 => {
|
||||||
|
return Ok(());
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
eprintln!("Invalid command.");
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Example of sending a command that expects a response
|
||||||
|
|
||||||
|
|
||||||
|
// Example of sending a command that does not expect a response
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@@ -1,10 +1,9 @@
|
|||||||
use serialport::SerialPort;
|
|
||||||
use std::thread::sleep;
|
|
||||||
use std::time::Duration;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::time::Duration;
|
||||||
use super::packet::XMPacket;
|
use std::thread::sleep;
|
||||||
use super::radio_id::XMRadioID;
|
use serialport::SerialPort;
|
||||||
|
use crate::xm::packet::XMPacket;
|
||||||
|
use crate::xm::radio_id::XMRadioID;
|
||||||
|
|
||||||
pub struct XMCommand;
|
pub struct XMCommand;
|
||||||
|
|
||||||
@@ -27,12 +26,16 @@ impl XMCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_command(port: &mut dyn SerialPort, command: XMPacket, timeout: Duration) -> Result<String, io::Error> {
|
pub fn send_command(port: &mut dyn SerialPort, command: XMPacket, timeout: Duration, expect_response: bool) -> Result<Option<String>, io::Error> {
|
||||||
loop {
|
// Send the command
|
||||||
// Send the command
|
port.write_all(&command.to_bytes())?;
|
||||||
port.write_all(&command.to_bytes())?;
|
port.flush()?;
|
||||||
port.flush()?;
|
|
||||||
|
|
||||||
|
if (!expect_response) {
|
||||||
|
return Ok(None); // Return immediately if no response is expected
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
// Read the response from the serial port
|
// Read the response from the serial port
|
||||||
let mut response: Vec<u8> = vec![0; 1024]; // Buffer for response
|
let mut response: Vec<u8> = vec![0; 1024]; // Buffer for response
|
||||||
match port.read(&mut response) {
|
match port.read(&mut response) {
|
||||||
@@ -48,7 +51,7 @@ pub fn send_command(port: &mut dyn SerialPort, command: XMPacket, timeout: Durat
|
|||||||
Ok(radio_id) => {
|
Ok(radio_id) => {
|
||||||
let serial_number_bytes = radio_id.get_radio_id();
|
let serial_number_bytes = radio_id.get_radio_id();
|
||||||
let serial_number_str = String::from_utf8_lossy(serial_number_bytes).to_string();
|
let serial_number_str = String::from_utf8_lossy(serial_number_bytes).to_string();
|
||||||
return Ok(serial_number_str); // Return the serial number string
|
return Ok(Some(serial_number_str)); // Return the serial number string
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error parsing XMRadioID: {}", e);
|
eprintln!("Error parsing XMRadioID: {}", e);
|
||||||
|
Reference in New Issue
Block a user