Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

An ADS client to use in combination with the TC1000 ADS router.

The client opens a port on the local ADS router in order to submit ADS requests. Use the Client::new method to create an instance.

Implementations§

Source§

impl Client

Source

pub async fn read( &self, idx_grp: u32, idx_offs: u32, data: &mut [u8], ) -> Result<u32>

Submit an asynchronous ADS Read request.

§Example
use ads_client::{ClientBuilder, Result};

#[tokio::main]
async fn main() -> Result<()> {
   let ads_client = ClientBuilder::new("5.80.201.232.1.1", 851).build().await?;

   // Get symbol handle
   let mut hdl : [u8; 4] = [0; 4];
   let symbol = b"MAIN.n_cnt_a";

   if let Err(err) = ads_client.read_write(0xF003, 0, &mut hdl, symbol).await{
       println!("Error: {}", err.to_string());
   }

   let n_hdl = u32::from_ne_bytes(hdl.try_into().unwrap());

   if n_hdl != 0 {
       println!("Got handle!");

       let mut plc_n_cnt_a : [u8; 2] = [0; 2];
       

       let read_hdl = ads_client.read(0xF005, n_hdl, &mut plc_n_cnt_a).await;

       match read_hdl {
           Ok(_bytes_read)     => {
               let n_cnt_a = u16::from_ne_bytes(plc_n_cnt_a.try_into().unwrap());
               println!("MAIN.n_cnt_a: {}", n_cnt_a);
           },
           Err(err) => println!("Read failed: {}", err.to_string())
       }
   }
   Ok(())
}

Checkout the examples read_symbol and read_symbol_async.

Source§

impl Client

Source

pub async fn write( &self, idx_grp: u32, idx_offs: u32, data: &[u8], ) -> Result<()>

Submit an asynchronous ADS Write request.

§Example
use ads_client::{ClientBuilder, Result};

#[tokio::main]
async fn main() -> Result<()> {
   let ads_client = ClientBuilder::new("5.80.201.232.1.1", 851).build().await?;

   // Get symbol handle
   let mut hdl : [u8; 4] = [0; 4];
   let symbol = b"MAIN.n_cnt_a";

   if let Err(err) = ads_client.read_write(0xF003, 0, &mut hdl, symbol).await{
       println!("Error: {}", err.to_string());
   }

   let n_hdl = u32::from_ne_bytes(hdl.try_into().unwrap());

   if n_hdl != 0 {
       println!("Got handle!");
       
       let n_cnt_a : u16 = 1000;
       
       match ads_client.write(0xF005, n_hdl, &n_cnt_a.to_ne_bytes()).await{
           Ok(_)     => println!("Variable successfully written!"),
           Err(err) => println!("Error: {}", err.to_string())
       }
   }
   Ok(())
}

Checkout the examples write_symbol and write_symbol_async.

Source§

impl Client

Source

pub async fn read_state(&self) -> Result<StateInfo>

Submit an asynchronous ADS Read State request.

§Example
use ads_client::{ClientBuilder, Result};
#[tokio::main]
async fn main() -> Result<()> {

   let ads_client = ClientBuilder::new("5.80.201.232.1.1", 10000).build().await?;

   match ads_client.read_state().await {
       Ok(state) => println!("State: {:?}", state),
       Err(err) => println!("Error: {}", err.to_string())
   }
   Ok(())
}

Checkout the examples read_state and read_state_async.

Source§

impl Client

Source

pub async fn read_write( &self, idx_grp: u32, idx_offs: u32, read_data: &mut [u8], write_data: &[u8], ) -> Result<u32>

Submit an asynchronous ADS Read Write request.

§Example
use ads_client::{ClientBuilder, Result};
#[tokio::main]
async fn main() -> Result<()> {
    let ads_client = ClientBuilder::new("5.80.201.232.1.1", 851).build().await?;

    // Get symbol handle
    let mut hdl : [u8; 4] = [0; 4];
    let symbol = b"MAIN.n_cnt_a";

    if let Err(err) = ads_client.read_write(0xF003, 0, &mut hdl, symbol).await{
        println!("Error: {}", err.to_string());
    }
    Ok(())
}

Checkout the examples read_symbol and read_symbol_async.

Source§

impl Client

Source

pub async fn add_device_notification( &self, idx_grp: u32, idx_offs: u32, attributes: &AdsNotificationAttrib, handle: &mut u32, callback: Notification, user_data: Option<&Arc<Mutex<BytesMut>>>, ) -> Result<()>

Submit an asynchronous ADS Add Device Notification request.

Checkout the extensive examples notification and notification_async.

Source§

impl Client

Source

pub async fn delete_device_notification(&self, handle: u32) -> Result<()>

Submit an asynchronous ADS Delete Device Notification request.

Checkout the extensive examples notification and notification_async.

Source§

impl Client

Source

pub async fn write_control( &self, state: &StateInfo, data: Option<&[u8]>, ) -> Result<()>

Source§

impl Client

Source

pub async fn read_device_info(&self) -> Result<DeviceStateInfo>

Submit an asynchronous ADS Read Device Info request.

§Example
use ads_client::{ClientBuilder, Result};

#[tokio::main]
async fn main() -> Result<()> {

   let ads_client = ClientBuilder::new("5.80.201.232.1.1", 10000).build().await?;
   
   match ads_client.read_device_info().await {
       Ok(device_info) => {
           println!("DeviceInfo: TwinCAT {}.{}.{} , Device Name: {}", 
               device_info.major, 
               device_info.minor,
               device_info.build,
               device_info.device_name)
       }
       Err(err) => println!("Error: {}", err.to_string())
   }
   Ok(())
}

Checkout the examples read_device_info and read_device_info_async.

Trait Implementations§

Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.