Write a new Data Type Converter for a Krunch Pipeline Data Stream
- Last UpdatedApr 08, 2026
- 3 minute read
The KrunchData byte array, which is a member of the KrunchContext class, contains data provided by the protocol driver through Omnicomm. The maximum size of the array for most protocols is eight bytes for the Double data type.
Data Type Converters should truncate the data when too many bytes are provided and, conversely, flag the data as an instrumentation failure when too few bytes are provided.
Once the data is validated, the Data Type Converter needs to manipulate the endianness of the data to match the endianness of the system. The X86 and X64 CPUs are little endian (Least Significant Bit [LSB]). However, all of the default Data Type Converters expect the data from the field to be big endian (Most Significant Bit [MSB]). To perform the conversion, use the DataStreamByteOrder.ConvertFromMSB() method. If a new data type is added that expects little endian (LSB) data, use the DataStreamByteOrder.ConvertFromLSB() method instead. Once the data is in the correct endian, you can then decode it into a double representation of the number.
If the data is found to be invalid during the decoding process, set the instrumentation failure flag.
To write a new Data Type Converter for a Krunch Pipeline data stream
In this example, the step code is written to the MyDataConversionType.cs file.
- In the created project .csproj file used to implement the data converter code, add the reference to both the baseline data model dynamic-link library (DLL) and the extended RealTime data model DLL file used to store the extended data type.
- In the created project used to implement the data converter code, open or create the class file.
- Set the class to implement the IKrunchDataTypeConverter interface, and give the class the same name as the TypeName in the RealTime_KrunchDataTypeConverters.json file.
- Set the DataTypeName to the same as the class name.
- Add "using" statements for the namespace of both the extended RealTime data model and the baseline RealTime data model.
- Create the converter method, with the KrunchContext as the parameter, and returning a Double (return a Double from the method for all data types, even if the data type is an integer or something else).
- Check if the length of the supplied data has enough data for your data type.
- Conditionally return instrument failure if there is not enough data to perform the conversion and set the return value to 0.0.
- Either truncate the data array to the correct length if required, or use the data as is if the length is correct.
- Convert the data from MSB to the endian of the current CPU.
- Decode the data and return.
Example code
// In the created project used to implement the data converter code, open or create the class file.
using System;
// Add "using" statements for the namespace of both the extended RealTime data model and the baseline RealTime data model.
using OASySDNA.RealTime.Data.Model;
using OASySDNA.RealTime.Data.Model.Extensions;
using OASySDNA.RealTime.ExecutionPipelines.KrunchCommon.Context;
using OASySDNA.RealTime.ExecutionPipelines.KrunchDataStream.Context;
using OASySDNA.RealTime.ExecutionPipelines.KrunchDataStream.ConverterFramework;
namespace OASySDNA.RealTime.KrunchDataTypeConverters
{
// Set the class to implement the IKrunchDataTypeConverter interface.
// Name the class the same as the TypeName in the RealTime_KrunchDataTypeConverters.json file.
public sealed class MyDataConversionType : IKrunchDataTypeConverter
{
// Set the DataTypeName to the same as the class name.
public String DataTypeName { get; } = "MyDataConversionType";
// Create the converter method, with the KrunchContext as the parameter, and returning a Double.
public Double Convert(KrunchContext context)
{
// Check if the length of the supplied data has enough data for your data type.
if (((AnalogKrunchDataStreamParameters)context.TableParameters).KrunchData.Length < sizeof(Int16))
{
// Conditionally return instrument failure if there is not enough data to perform the conversion.
context.AdditionalParameters.Update("InstrumentFailFlag", Hilowstate.HILOW_INS_HIGH);
// Return 0.0.
return 0.0;
}
else
{
// Either truncate the data array to the correct length if required, or use the data as is if the length is correct.
var truncatedByteArray = new Byte[] { 0, 0 };
if (((AnalogKrunchDataStreamParameters)context.TableParameters).KrunchData.Length > sizeof(Int16))
{
Array.Copy(((AnalogKrunchDataStreamParameters)context.TableParameters).KrunchData, truncatedByteArray, truncatedByteArray.Length);
}
else
{
truncatedByteArray = ((AnalogKrunchDataStreamParameters)context.TableParameters).KrunchData;
}
// Convert the data from MSB to the endian of the current CPU.
var covertedData = DataStreamByteOrder.ConvertFromMSB(truncatedByteArray);
// Decode the data and return.
return BitConverter.ToInt16(covertedData, 0);
}
}
}
}