Skip to main content

UdtData

Struct UdtData 

Source
pub struct UdtData {
    pub symbol_id: i32,
    pub data: Vec<u8>,
}
Expand description

Represents the different data types supported by Allen-Bradley PLCs

These correspond to the CIP data type codes used in EtherNet/IP communication. Each variant maps to a specific 16-bit type identifier that the PLC uses to describe tag data.

§Supported Data Types

§Integer Types

  • SINT: 8-bit signed integer (-128 to 127)
  • INT: 16-bit signed integer (-32,768 to 32,767)
  • DINT: 32-bit signed integer (-2,147,483,648 to 2,147,483,647)
  • LINT: 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

§Unsigned Integer Types

  • USINT: 8-bit unsigned integer (0 to 255)
  • UINT: 16-bit unsigned integer (0 to 65,535)
  • UDINT: 32-bit unsigned integer (0 to 4,294,967,295)
  • ULINT: 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)

§Floating Point Types

  • REAL: 32-bit IEEE 754 float (±1.18 × 10^-38 to ±3.40 × 10^38)
  • LREAL: 64-bit IEEE 754 double (±2.23 × 10^-308 to ±1.80 × 10^308)

§Other Types

  • BOOL: Boolean value (true/false)
  • STRING: Variable-length string
  • UDT: User Defined Type (structured data)

Represents raw UDT (User Defined Type) data

This structure stores UDT data in a generic format that works for any UDT without requiring knowledge of member names. The symbol_id (template instance ID) is required for writing UDTs back to the PLC, and the raw bytes can be parsed later when the UDT definition is available.

§Usage

To write a UDT, you typically need to read it first to get the symbol_id. While it’s technically possible to calculate the symbol_id, it’s much safer to enforce a read of the UDT before writing to it.

Fields§

§symbol_id: i32

The template instance ID (symbol_id) from the PLC This is required for writing UDTs back to the PLC

§data: Vec<u8>

Raw UDT data bytes This can be parsed into member values when the UDT definition is known

Implementations§

Source§

impl UdtData

Source

pub fn parse( &self, definition: &UserDefinedType, ) -> Result<HashMap<String, PlcValue>>

Parses the raw UDT data into a HashMap of member values using the UDT definition

v0.6.0: This method converts the generic UdtData format into a structured HashMap of member names to values. This requires a UDT definition to know the structure of the data.

Use EipClient::get_udt_definition() to obtain the definition from the PLC first.

§Arguments
  • definition - The UDT definition containing member information (offsets, types, etc.)
§Returns

A HashMap mapping member names to their parsed PlcValue values

§Example
use rust_ethernet_ip::PlcValue;
let udt_value = client.read_tag("MyUDT").await?;
if let PlcValue::Udt(udt_data) = udt_value {
    let udt_def = client.get_udt_definition("MyUDT").await?;
    // Convert UdtDefinition to UserDefinedType
    let mut user_def = rust_ethernet_ip::udt::UserDefinedType::new(udt_def.name.clone());
    for member in &udt_def.members {
        user_def.add_member(member.clone());
    }
    let members = udt_data.parse(&user_def)?;
     
    if let Some(PlcValue::Dint(value)) = members.get("Member1") {
        println!("Member1 value: {}", value);
    }
}
Source

pub fn from_hash_map( members: &HashMap<String, PlcValue>, definition: &UserDefinedType, symbol_id: i32, ) -> Result<Self>

Creates UdtData from a HashMap of member values and a UDT definition

v0.6.0: This method serializes member values back into raw bytes according to the UDT definition. This is useful when you need to modify UDT members and write them back to the PLC.

§Arguments
  • members - HashMap of member names to PlcValue values
  • definition - The UDT definition containing member information (offsets, types, etc.)
  • symbol_id - The template instance ID (symbol_id) for this UDT. Typically obtained by reading the UDT first.
§Returns

UdtData containing the serialized bytes and symbol_id, ready to be written back

§Example
use rust_ethernet_ip::{PlcValue, UdtData};
// Read existing UDT to get symbol_id
let udt_value = client.read_tag("MyUDT").await?;
let udt_def = client.get_udt_definition("MyUDT").await?;

if let PlcValue::Udt(mut udt_data) = udt_value {
    // Convert UdtDefinition to UserDefinedType
    let mut user_def = rust_ethernet_ip::udt::UserDefinedType::new(udt_def.name.clone());
    for member in &udt_def.members {
        user_def.add_member(member.clone());
    }
    // Parse to modify members
    let mut members = udt_data.parse(&user_def)?;
    members.insert("Member1".to_string(), PlcValue::Dint(42));

    // Serialize back to UdtData
    let modified_udt = UdtData::from_hash_map(&members, &user_def, udt_data.symbol_id)?;
    client.write_tag("MyUDT", PlcValue::Udt(modified_udt)).await?;
}

Trait Implementations§

Source§

impl Clone for UdtData

Source§

fn clone(&self) -> UdtData

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UdtData

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for UdtData

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for UdtData

Source§

fn eq(&self, other: &UdtData) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for UdtData

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for UdtData

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,