Author Topic: EthernetIPforCLXCom - Reading Complete UDT  (Read 7292 times)

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
EthernetIPforCLXCom - Reading Complete UDT
« on: September 15, 2017, 08:04:35 AM »
It is frequently asked about reading complete UDTs since reading each element individually is slow and inefficient. A page has been added to the documentation wiki that will help shed some light on this.

http://advancedhmi.com/documentation/index.php?title=Reading_Complete_UDT_with_ControlLogix_Driver

dmroeder

  • Administrator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: EthernetIPforCLXCom - Reading Complete UDT
« Reply #1 on: September 16, 2017, 12:51:23 AM »
It is frequently asked about reading complete UDTs since reading each element individually is slow and inefficient. A page has been added to the documentation wiki that will help shed some light on this.

https://advancedhmi.com/documentation/index.php?title=Reading_Complete_UDT_with_ControlLogix_Driver

That's a nice addition!  Will it currently handle UDT's that require more than 1 packet to get all the data?
« Last Edit: March 11, 2018, 08:07:56 PM by Archie »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: EthernetIPforCLXCom - Reading Complete UDT
« Reply #2 on: September 16, 2017, 09:02:43 AM »
That's a nice addition!  Will it currently handle UDT's that require more than 1 packet to get all the data?
Yes it will. It was created as a pre-cursor to the ReadUDT which will do the parsing of the UDT data for you into a Structure or Class. The only thing that kept it from being part of the previous release was the way .NET handles arrays in structures. When an array is defined in a structure, it only creates a pointer to any length array, therefore making it not possible to correctly parse the data without knowing the length of the array in a UDT element.

Larry Griffin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: EthernetIPforCLXCom - Reading Complete UDT
« Reply #3 on: September 24, 2017, 12:03:47 PM »
This works very well, indeed!  C# example follows:

Code: [Select]
        // C# example

        var workOrder = new WO();

        // Read one complete WorkOrder from the PLC
        workOrder = ReadWO("WorkOrderTag")

Code: [Select]
    // Work Order Structure
    public class WO
    {
        public int Sequence;
        public string Description;
        public string ProductID;
        public string CustWO;
        publice string ThisWO;
        public float CasingLen;
        public float TubingLen;
        public int Qty;
        public int Priority;
     }

Code: [Select]
        // Method to read Work Order from PLC
        private WO ReadWO(string tag)
        {
            Byte[] bytes = ethernetIPforCLXCom1.ReadRaw(tag);
            WO wO = new WO
            {
                Sequence = BitConverter.ToInt32(bytes, 0),
                Description = System.Text.Encoding.Default.GetString(bytes, 8, BitConverter.ToInt32(bytes, 4)),
                ProductID = System.Text.Encoding.Default.GetString(bytes, 96, BitConverter.ToInt32(bytes, 92)),
                CustWO = System.Text.Encoding.Default.GetString(bytes, 116, BitConverter.ToInt32(bytes, 112)),
                ThisWO = System.Text.Encoding.Default.GetString(bytes, 136, BitConverter.ToInt32(bytes, 132)),
                CasingLen = BitConverter.ToSingle(bytes, 152),
                TubingLen = BitConverter.ToSingle(bytes, 156),
                Qty = BitConverter.ToInt32(bytes, 160),
                Priority = BitConverter.ToInt32(bytes, 164)
            };
            return wO;
        }