atcommand_set/
atcommand_set.rs

1//! AtCommand API Query example
2//!
3//! This example shows how to set the NodeId on connected XBee Device
4//!
5//!
6
7use rustbee::{api, device::DigiMeshDevice};
8use std::error;
9
10#[cfg(target_os = "linux")]
11static PORT: &'static str = "/dev/ttyUSB0";
12
13#[cfg(target_os = "windows")]
14static PORT: &'static str = "COM1";
15
16static NODE_ID: &'static str = "MY_NODE";
17
18fn main() -> Result<(), Box<dyn error::Error>> {
19    // first create instance of device
20    let mut device = DigiMeshDevice::new(PORT, 9600)?;
21
22    // Construct At command and set node_id of device by supplying valid [u8] but None
23    let _ = api::AtCommandFrame("NI", Some(NODE_ID.as_bytes()));
24
25    // Now query new node_id
26    let new_node_id = api::AtCommandFrame("NI", None);
27
28    // returns dyn trait RecieveApiFrame
29    let response = device.send_frame(new_node_id)?;
30
31    // We can downcast to original AtCommandResponse struct to access members
32    let atcommand_response = response.downcast_ref::<api::AtCommandResponse>();
33
34    if let Some(obj) = atcommand_response {
35        let cmd_data = &obj.command_data;
36        println!("{:?}", cmd_data); // Some(b"MY_NODE")
37    }
38
39    Ok(())
40}