AdvancedHMI Software
General Category => Tips & Tricks => Topic started by: Archie 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
-
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?
-
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.
-
This works very well, indeed! C# example follows:
// C# example
var workOrder = new WO();
// Read one complete WorkOrder from the PLC
workOrder = ReadWO("WorkOrderTag")
// 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;
}
// 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;
}