460 lines
14 KiB
C#
460 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace XYLEM.Base
|
|
{
|
|
public static class DataUnionTools
|
|
{
|
|
public static byte[] Reverse(this byte[] data)
|
|
{
|
|
Array.Reverse(data);
|
|
return data;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert DataObjClass.DataType to Byte
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public struct DataUnion
|
|
{
|
|
//----------------------------- Data String to Convert Rx to ----------------------
|
|
#region ---- Inset Data Bytes in union --------
|
|
public enum DataByteLayoutType
|
|
{
|
|
Normal,
|
|
SwapAll,
|
|
SwapWords // KellerType
|
|
}
|
|
|
|
internal UInt32 Conv32BitToLowAdrHighValue(UInt32 input)
|
|
{
|
|
return (input >> 16) | (input << 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert input byte data to union data
|
|
/// </summary>
|
|
/// <param name="u16Data"></param>
|
|
/// <returns></returns>
|
|
public DataUnion PutByteArray(Byte[] bInputArray, DataByteLayoutType byteType)
|
|
{
|
|
if (byteType == DataByteLayoutType.SwapAll)
|
|
{
|
|
Array.Reverse(bInputArray);
|
|
}
|
|
//dataRxText = "";
|
|
if ((bInputArray == null) || (bInputArray.Length == 0))
|
|
{
|
|
u64Data = 0;
|
|
return this;
|
|
}
|
|
bData = bInputArray[0];
|
|
if (bInputArray.Length == 1)
|
|
{
|
|
return this;
|
|
}
|
|
|
|
bData1 = bInputArray[1];
|
|
if (bInputArray.Length == 2)
|
|
{ // 16bit / 2 x 8bit
|
|
return this;
|
|
}
|
|
|
|
bData2 = bInputArray[2];
|
|
if (bInputArray.Length == 3)
|
|
{
|
|
if (byteType == DataByteLayoutType.SwapWords)
|
|
{
|
|
throw new Exception("SwapWords Not Supported!");
|
|
}
|
|
return this;
|
|
}
|
|
|
|
bData3 = bInputArray[3];
|
|
if (bInputArray.Length == 4)
|
|
{// 32bit / 4 x 8bit
|
|
if (byteType == DataByteLayoutType.SwapWords)
|
|
{
|
|
u32Data = Conv32BitToLowAdrHighValue(u32Data);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
bData4 = bInputArray[4];
|
|
if (bInputArray.Length == 5)
|
|
{
|
|
if (byteType == DataByteLayoutType.SwapWords)
|
|
{
|
|
throw new Exception("SwapWords Not Supported!");
|
|
}
|
|
return this;
|
|
}
|
|
|
|
bData5 = bInputArray[5];
|
|
if (bInputArray.Length == 6)
|
|
{
|
|
if (byteType == DataByteLayoutType.SwapWords)
|
|
{
|
|
throw new Exception("SwapWords Not Supported!");
|
|
}
|
|
return this;
|
|
}
|
|
|
|
bData6 = bInputArray[6];
|
|
if (bInputArray.Length == 7)
|
|
{
|
|
if (byteType == DataByteLayoutType.SwapWords)
|
|
{
|
|
throw new Exception("SwapWords Not Supported!");
|
|
}
|
|
return this;
|
|
}
|
|
|
|
bData7 = bInputArray[7];
|
|
if (byteType == DataByteLayoutType.SwapWords)
|
|
{ // 64bit / 8 x 8bit
|
|
throw new Exception("SwapWords Not Supported!");
|
|
}
|
|
return this;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ---- Put data as Value in to union -----
|
|
public void PutValue(UInt32 u32DataIn)
|
|
{
|
|
u32Data = u32DataIn;
|
|
}
|
|
|
|
public void PutValue(Int32 s32DataIn)
|
|
{
|
|
s32Data = s32DataIn;
|
|
}
|
|
|
|
public void PutValue(Single f32DataIn)
|
|
{
|
|
f32Data = f32DataIn;
|
|
}
|
|
|
|
public void PutValue(UInt64 u64DataIn)
|
|
{
|
|
u64Data = u64DataIn;
|
|
}
|
|
|
|
public void PutValue(Int64 s64DataIn)
|
|
{
|
|
s64Data = s64DataIn;
|
|
}
|
|
|
|
public void PutValue(double f64DataIn)
|
|
{
|
|
f64Data = f64DataIn;
|
|
}
|
|
#endregion
|
|
|
|
#region ---- Get ByteArray from input as a value ----
|
|
public byte[] GetByteArray()
|
|
{
|
|
return new byte[] { bData, bData1, bData2, bData3, bData4, bData5, bData6, bData7 };
|
|
}
|
|
|
|
|
|
void DoTxByteLayout(ref byte[] txData, DataByteLayoutType type)
|
|
{
|
|
try
|
|
{
|
|
if ((txData == null) || (txData.Length == 0))
|
|
{
|
|
throw new Exception("Has no txData");
|
|
}
|
|
switch (type)
|
|
{
|
|
case DataByteLayoutType.SwapAll: Array.Reverse(txData); break;
|
|
case DataByteLayoutType.SwapWords:
|
|
if (txData.Length == 4)
|
|
{ // 32bit / 2 x word
|
|
byte[] firstWord = new byte[2];
|
|
Array.Copy(txData, firstWord, 2);
|
|
Array.Copy(txData, 2, txData, 0, 2);
|
|
Array.Copy(firstWord, 0, txData, 2, 2);
|
|
}
|
|
else if (txData.Length == 8)
|
|
{ // 64bit / 2 x word
|
|
throw new Exception("SwapWords Not Supported!");
|
|
}
|
|
break;
|
|
case DataByteLayoutType.Normal: break; // Do nothing
|
|
default: throw new Exception("Unkown type = " + type.ToString());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Error in " + this + " in func. DoTxByteLayout()! message = " + ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to unsigned 16bit
|
|
/// </summary>
|
|
/// <param name="u16Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(UInt16 u16DataIn, DataByteLayoutType byteType)
|
|
{
|
|
byte[] bDataRet = new byte[2];
|
|
u16Data = u16DataIn;
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to signed 16bit
|
|
/// </summary>
|
|
/// <param name="s16Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(Int16 s16DataIn, DataByteLayoutType byteType)
|
|
{
|
|
byte[] bDataRet = new byte[2];
|
|
s16Data = s16DataIn;
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to unsigned 32bit
|
|
/// </summary>
|
|
/// <param name="u32Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(UInt32 u32DataIn, DataByteLayoutType byteType)
|
|
{
|
|
PutValue(u32DataIn);
|
|
byte[] bDataRet = new byte[4];
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
bDataRet[2] = bData2;
|
|
bDataRet[3] = bData3;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to sign 32bit
|
|
/// </summary>
|
|
/// <param name="s32Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(Int32 s32DataIn, DataByteLayoutType byteType)
|
|
{
|
|
PutValue(s32DataIn);
|
|
byte[] bDataRet = new byte[4];
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
bDataRet[2] = bData2;
|
|
bDataRet[3] = bData3;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to Single 32bit
|
|
/// </summary>
|
|
/// <param name="f32Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(Single f32DataIn, DataByteLayoutType byteType)
|
|
{
|
|
PutValue(f32DataIn);
|
|
byte[] bDataRet = new byte[4];
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
bDataRet[2] = bData2;
|
|
bDataRet[3] = bData3;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to unsigned 64bit
|
|
/// </summary>
|
|
/// <param name="U64Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(UInt64 u64DataIn, DataByteLayoutType byteType)
|
|
{
|
|
PutValue(u64DataIn);
|
|
byte[] bDataRet = new byte[8];
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
bDataRet[2] = bData2;
|
|
bDataRet[3] = bData3;
|
|
bDataRet[4] = bData4;
|
|
bDataRet[5] = bData5;
|
|
bDataRet[6] = bData6;
|
|
bDataRet[7] = bData7;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to double (F64bit)
|
|
/// </summary>
|
|
/// <param name="S64Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(Int64 s64DataIn, DataByteLayoutType byteType)
|
|
{
|
|
PutValue(s64DataIn);
|
|
byte[] bDataRet = new byte[8];
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
bDataRet[2] = bData2;
|
|
bDataRet[3] = bData3;
|
|
bDataRet[4] = bData4;
|
|
bDataRet[5] = bData5;
|
|
bDataRet[6] = bData6;
|
|
bDataRet[7] = bData7;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Tx data to unsigned 64bit
|
|
/// </summary>
|
|
/// <param name="U64Data"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetByteArray(double f64DataIn, DataByteLayoutType byteType)
|
|
{
|
|
PutValue(f64DataIn);
|
|
byte[] bDataRet = new byte[8];
|
|
bDataRet[0] = bData;
|
|
bDataRet[1] = bData1;
|
|
bDataRet[2] = bData2;
|
|
bDataRet[3] = bData3;
|
|
bDataRet[4] = bData4;
|
|
bDataRet[5] = bData5;
|
|
bDataRet[6] = bData6;
|
|
bDataRet[7] = bData7;
|
|
DoTxByteLayout(ref bDataRet, byteType);
|
|
return bDataRet;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ---- Return Union data as a value ----
|
|
public byte U8Data { get { return bData; } set { u16Data = bData; } }
|
|
public byte U8Data0 { get { return bData; } }
|
|
public byte U8Data1 { get { return bData1; } }
|
|
public byte U8Data2 { get { return bData2; } }
|
|
public byte U8Data3 { get { return bData3; } }
|
|
public byte U8Data4 { get { return bData4; } }
|
|
public byte U8Data5 { get { return bData5; } }
|
|
public byte U8Data6 { get { return bData6; } }
|
|
public byte U8Data7 { get { return bData7; } }
|
|
|
|
public UInt16 U16Data { get { return u16Data; } set { u16Data = value; } }
|
|
public UInt16 U16Data0 { get { return u16Data; } }
|
|
public UInt16 U16Data1 { get { return u16Data1; } }
|
|
public UInt16 U16Data2 { get { return u16Data2; } }
|
|
public UInt16 U16Data3 { get { return u16Data3; } }
|
|
|
|
public Int16 S16Data { get { return s16Data; } }
|
|
public Int16 S16Data0 { get { return s16Data; } }
|
|
public Int16 S16Data1 { get { return s16Data1; } }
|
|
public Int16 S16Data2 { get { return s16Data2; } }
|
|
public Int16 S16Data3 { get { return s16Data3; } }
|
|
|
|
public UInt32 U32Data { get { return u32Data; } set { u32Data = value; } }
|
|
public UInt32 U32Data0 { get { return u32Data; } }
|
|
public UInt32 U32Data1 { get { return u32Data1; } }
|
|
|
|
public Int32 S32Data { get { return s32Data; } set { s32Data = value; } }
|
|
public Int32 S32Data0 { get { return s32Data; } }
|
|
public Int32 S32Data1 { get { return s32Data1; } }
|
|
|
|
public Single F32Data { get { return f32Data; } set { f32Data = value; } }
|
|
|
|
public UInt64 U64Data { get { return u64Data; } }
|
|
public Int64 S64Data { get { return s64Data; } }
|
|
public double F64Data { get { return f64Data; } }
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Clear all data and return emptyunion
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public DataUnion ClearData()
|
|
{
|
|
u64Data = 0;
|
|
return this;
|
|
}
|
|
|
|
#region ---- Field Union Array ----
|
|
//----------------------------- Field Union array -----------------------------
|
|
[FieldOffset(0)]
|
|
private byte bData;
|
|
[FieldOffset(1)]
|
|
private byte bData1;
|
|
[FieldOffset(2)]
|
|
private byte bData2;
|
|
[FieldOffset(3)]
|
|
private byte bData3;
|
|
[FieldOffset(4)]
|
|
private byte bData4;
|
|
[FieldOffset(5)]
|
|
private byte bData5;
|
|
[FieldOffset(6)]
|
|
private byte bData6;
|
|
[FieldOffset(7)]
|
|
private byte bData7;
|
|
|
|
[FieldOffset(0)]
|
|
private UInt16 u16Data;
|
|
[FieldOffset(2)]
|
|
private UInt16 u16Data1;
|
|
[FieldOffset(4)]
|
|
private UInt16 u16Data2;
|
|
[FieldOffset(6)]
|
|
private UInt16 u16Data3;
|
|
|
|
[FieldOffset(0)]
|
|
private Int16 s16Data;
|
|
[FieldOffset(2)]
|
|
private Int16 s16Data1;
|
|
[FieldOffset(4)]
|
|
private Int16 s16Data2;
|
|
[FieldOffset(6)]
|
|
private Int16 s16Data3;
|
|
|
|
[FieldOffset(0)]
|
|
private UInt32 u32Data;
|
|
[FieldOffset(4)]
|
|
private UInt32 u32Data1;
|
|
|
|
[FieldOffset(0)]
|
|
private Int32 s32Data;
|
|
[FieldOffset(4)]
|
|
private Int32 s32Data1;
|
|
|
|
[FieldOffset(0)]
|
|
private Single f32Data;
|
|
[FieldOffset(4)]
|
|
private Single f32Data1;
|
|
|
|
[FieldOffset(0)]
|
|
private UInt64 u64Data;
|
|
|
|
[FieldOffset(0)]
|
|
private Int64 s64Data;
|
|
|
|
[FieldOffset(0)]
|
|
private double f64Data;
|
|
#endregion
|
|
|
|
};
|
|
}
|