Demo entry 6756894

UnityStuff

   

Submitted by anonymous on Aug 04, 2018 at 23:24
Language: C#. Code size: 17.4 kB.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace UnityStuff
{
    #region C++ Struct

    public enum EPrimitiveDataType
    {
        SRPDT_UNSPECIFIC = 0,
        SRPDT_STRUCT = 1,
        SRPDT_BOOL = 2,
        SRPDT_INT8 = 3,
        SRPDT_INT16 = 4,
        SRPDT_INT32 = 5,
        SRPDT_INT64 = 6,
        SRPDT_UINT8 = 7,
        SRPDT_UINT16 = 8,
        SRPDT_UINT32 = 9,
        SRPDT_UINT64 = 10,
        SRPDT_FLOAT = 11,
        SRPDT_DOUBLE = 12,
        SRPDT_CHAR = 13,
        SRPDT_WCHAR = 14
    };

    //public enum ERuntimeErrorCode
    //{
    //    SREC_OK = 0,
    //    SREC_INVALID_ERROR_CODE = -1,
    //    SREC_NOT_IMPLEMENTED = -2,
    //    SREC_INDEX_OUT_OF_RANGE = -3,
    //    SREC_DOES_NOT_EXIST = -4,
    //    SREC_ALREADY_EXISTS = -5,
    //    SREC_UNKNOWN_MESSAGE_TYPE = -6,
    //    SREC_INVALID_MESSAGE_ID = -7,
    //    SREC_WRONG_ARGUMENT = -8,
    //    SREC_WRONG_PIPE = -9,
    //    SREC_CONNECTION_ERROR = -10,
    //    SREC_TIMEOUT = -11,
    //    SREC_MESSAGE_CORRUPT = -12,
    //    SREC_WRONG_VERSION = -13,
    //    SREC_INSTANCE_NOT_RUNNING = -14,
    //    SREC_INTERFACE_REMOVED = -15,
    //    SREC_SHARED_MEMORY_NOT_INITIALIZED = -16,
    //    SREC_API_NOT_INITIALIZED = -17,
    //    SREC_WARNING_ALREADY_EXISTS = 18,
    //    SREC_NOT_SUPPORTED = -19,
    //    SREC_WARNING_INVALID_CALL = 20,
    //    SREC_ERROR_LOADING_DLL = -21,
    //    SREC_SIGNAL_NAME_DOES_NOT_EXIST = -22,
    //    SREC_SIGNAL_TYPE_MISMATCH = -23,
    //    SREC_SIGNAL_CONFIGURATION_ERROR = -24,
    //    SREC_NO_SIGNAL_CONFIGURATION_LOADED = -25,
    //    SREC_CONFIGURED_CONNECTION_NOT_FOUND = -26,
    //    SREC_CONFIGURED_DEVICE_NOT_FOUND = -27,
    //    SREC_INVALID_CONFIGURATION = -28,
    //    SREC_TYPE_MISMATCH = -29,
    //    SREC_LICENSE_NOT_FOUND = -30,
    //    SREC_NO_LICENSE_AVAILABLE = -31,
    //    SREC_WRONG_COMMUNICATION_INTERFACE = -32,
    //    SREC_LIMIT_REACHED = -33,
    //    SREC_NO_STORAGE_PATH_SET = -34,
    //    SREC_STORAGE_PATH_ALREADY_IN_USE = -35,
    //    SREC_MESSAGE_INCOMPLETE = -36,
    //    SREC_ARCHIVE_STORAGE_FAILED = -37,
    //    SREC_RETRIEVE_STORAGE_FAILURE = -38,
    //    SREC_INVALID_OPERATING_STATE = -39,
    //    SREC_INVALID_ARCHIVE_PATH = -40,
    //    SREC_DELETE_EXISTING_STORAGE_FAILED = -41,
    //    SREC_CREATE_DIRECTORIES_FAILED = -42,
    //    SREC_NOT_ENOUGH_MEMORY = -43,
    //    SREC_WARNING_TRIAL_MODE_ACTIVE = 44,
    //    SREC_NOT_RUNNING = -45,
    //    SREC_NOT_EMPTY = -46,
    //    SREC_NOT_UP_TO_DATE = -47,
    //    SREC_COMMUNICATION_INTERFACE_NOT_AVAILABLE = -48,
    //    SREC_WARNING_NOT_COMPLETE = 49,
    //    SREC_VIRTUAL_SWITCH_MISCONFIGURED = -50,
    //    SREC_RUNTIME_NOT_AVAILABLE = -51,
    //    SREC_IS_EMPTY = -52,
    //    SREC_WRONG_MODULE_STATE = -53,
    //    SREC_WRONG_MODULE_TYPE = -54,
    //    SREC_NOT_SUPPORTED_BY_MODULE = -55,
    //    SREC_INTERNAL_ERROR = -56,
    //    SREC_STORAGE_TRANSFER_ERROR = -57,
    //    SREC_ANOTHER_VARIANT_OF_PLCSIM_RUNNING = -58,
    //    SREC_ACCESS_DENIED = -59,
    //    SREC_NOT_ALLOWED_DURING_DOWNLOAD = -60
    //};

    //public struct SDVBNI
    //{
    //    SILS ILS;
    //    SILD ILD;
    //};

    //public struct SILS
    //{
    //    UInt16 UDS;
    //    UInt32 IDX;
    //};

    //public struct SILD
    //{
    //    UInt32 FI;
    //    UInt32 LIP1;
    //    UInt32 FM;
    //    UInt32 LMP1;
    //    UInt32 FQ;
    //    UInt32 LQP1;
    //    UInt32 TCNT;
    //    UInt32 SCNT;
    //    UInt32 CRC;
    //};

    [Serializable]
    public struct UDataValue
    {
        public bool Bool;
        public SByte Int8;
        public Int16 Int16;
        public Int32 Int32;
        public Int64 Int64;
        public Byte UInt8;
        public UInt16 UInt16;
        public UInt32 UInt32;
        public UInt64 UInt64;
        public float Float;
        public double Double;
        public byte Char;
        public Char WChar;
    }

    [Serializable]
    public struct SDataValue
    {
        public UDataValue Value;
        public EPrimitiveDataType Type;
    };

    [Serializable]
    public class Tag
    {
        public string Name;
        public SDataValue Data;
        public bool isWrite;
    }

    public struct Tags
    {
        [MarshalAs(UnmanagedType.LPWStr, )]
        string name)
    }
    
    //[Serializable]
    //public struct SDataValueByName
    //{
    //    public string Name;
    //    public SDataValue Data;
    //    ERuntimeErrorCode ErrorCode;
    //    public SDVBNI Internal;
    //}

    #endregion C++ Struct
    
    public class PLCSimConnection : MonoBehaviour
    {

        #region Mashall
        [DllImport("NativeLibrary")]
        public static extern int CreateInterface([MarshalAs(UnmanagedType.LPWStr)]string name);

        [DllImport("NativeLibrary")]
        public static extern void ReadSignals(SDataValueByName readtags, UInt32 readSignalCount);
        [DllImport("NativeLibrary")]
        public static extern void WriteSignals(SDataValueByName writetags, UInt32 writeSignalCount);

        [DllImport("NativeLibrary")]
        public static extern UInt64 GetWindowsTime();

        [DllImport("NativeLibrary")]
        public static extern UInt64 GetPLCSystemTime();


        //[DllImport("NativeLibrary")]
        //public static extern bool ReadBool([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern SByte ReadInt8([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern Int16 ReadInt16([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern Int32 ReadInt32([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern Int64 ReadInt64([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern Byte ReadUInt8([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern UInt16 ReadUInt16([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern UInt32 ReadUInt32([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern UInt64 ReadUInt64([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern float ReadFloat([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern double ReadDouble([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern byte ReadChar([MarshalAs(UnmanagedType.LPWStr)]string name);
        //[DllImport("NativeLibrary")]
        //public static extern char ReadWChar([MarshalAs(UnmanagedType.LPWStr)]string name);


        //[DllImport("NativeLibrary")]
        //public static extern void WriteBool([MarshalAs(UnmanagedType.LPWStr)]string name, bool value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteInt8([MarshalAs(UnmanagedType.LPWStr)]string name, SByte value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteInt16([MarshalAs(UnmanagedType.LPWStr)]string name, Int16 value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteInt32([MarshalAs(UnmanagedType.LPWStr)]string name, Int32 value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteInt64([MarshalAs(UnmanagedType.LPWStr)]string name, Int64 value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteUInt8([MarshalAs(UnmanagedType.LPWStr)]string name, Byte value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteUInt16([MarshalAs(UnmanagedType.LPWStr)]string name, UInt16 value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteUInt32([MarshalAs(UnmanagedType.LPWStr)]string name, UInt32 value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteUInt64([MarshalAs(UnmanagedType.LPWStr)]string name, UInt64 value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteFloat([MarshalAs(UnmanagedType.LPWStr)]string name, float value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteDouble([MarshalAs(UnmanagedType.LPWStr)]string name, double value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteChar([MarshalAs(UnmanagedType.LPWStr)]string name, byte value);
        //[DllImport("NativeLibrary")]
        //public static extern void WriteWChar([MarshalAs(UnmanagedType.LPWStr)]string name, Char value);

        #endregion Mashall

        #region Properties
        public string InstanceName;

        public ObservableCollection<SDataValueByName> readtags = new ObservableCollection<SDataValueByName>();
        public ObservableCollection<SDataValueByName> writetags = new ObservableCollection<SDataValueByName>();
        
        //public ObservableCollection<Tag> readtags = new ObservableCollection<Tag>();
        //public ObservableCollection<Tag> writetags = new ObservableCollection<Tag>();

        #endregion Properties

        #region Public Methods

        public void RegisterReadTag(ref SDataValueByName tag)
        {
            readtags.Add(tag);
        }
        //public void RegisterReadTag(ref Tag tag)
        //{
        //    readtags.Add(tag);
        //}
        public void RegisterWriteTag(ref SDataValueByName tag)
        {
            writetags.Add(tag);
        }
        //public void RegisterWriteTag(ref Tag tag)
        //{
        //    writetags.Add(tag);
        //}

        public UInt64 GetWindowsTime00()
        {
            return GetWindowsTime();
        }

        public UInt64 GetPLCSystemTime00()
        {
            return GetPLCSystemTime();
        }

        #endregion Public Methods

        #region C'tor
        // At the start of the game..
        void Start()
        {
            CreateInterface(InstanceName);
        }

        void Update()
        {            
            // Read signal data according to the data type
            uint readSignalCount = (UInt32)(readtags.Count);
            ReadSignals(readtags, readSignalCount);

            uint writeSignalCount = (UInt32)(writetags.Count);
            WriteSignals(writetags,writeSignalCount);

            //for (int i = 0; i < readtags.Count; i++)
            //{
            //switch (readtags[i].Data.Type)
            //{
            //    case EPrimitiveDataType.SRPDT_UNSPECIFIC:
            //        break;
            //    case EPrimitiveDataType.SRPDT_STRUCT:
            //        break;
            //    case EPrimitiveDataType.SRPDT_BOOL:
            //        readtags[i].Data.Value.Bool = ReadBool(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_INT8:
            //        readtags[i].Data.Value.Int8 = ReadInt8(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_INT16:
            //        readtags[i].Data.Value.Int16 = ReadInt16(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_INT32:
            //        readtags[i].Data.Value.Int32 = ReadInt32(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_INT64:
            //        readtags[i].Data.Value.Int64 = ReadInt64(readtags[i].Name);        
            //        break;
            //    case EPrimitiveDataType.SRPDT_UINT8:
            //        readtags[i].Data.Value.UInt8 = ReadUInt8(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_UINT16:
            //        readtags[i].Data.Value.UInt16 = ReadUInt16(readtags[i].Name);    
            //        break;
            //    case EPrimitiveDataType.SRPDT_UINT32:
            //        readtags[i].Data.Value.UInt32 = ReadUInt32(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_UINT64:
            //        readtags[i].Data.Value.UInt64 = ReadUInt64(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_FLOAT:
            //        readtags[i].Data.Value.Float = ReadFloat(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_DOUBLE:
            //        readtags[i].Data.Value.Double = ReadDouble(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_CHAR:
            //        readtags[i].Data.Value.Char = ReadChar(readtags[i].Name);
            //        break;
            //    case EPrimitiveDataType.SRPDT_WCHAR:
            //        readtags[i].Data.Value.WChar = ReadWChar(readtags[i].Name);
            //        break;
            //    default:
            //        break;
            //}
            //}

            // Write signal data according to the data type

            //for (int i = 0; i < writetags.Count; i++)
            //{
            //    //counter2++;
            //    switch (writetags[i].Data.Type)
            //    {
            //        case EPrimitiveDataType.SRPDT_UNSPECIFIC:
            //            break;
            //        case EPrimitiveDataType.SRPDT_STRUCT:
            //            break;
            //        case EPrimitiveDataType.SRPDT_BOOL:
            //            WriteBool(writetags[i].Name, writetags[i].Data.Value.Bool);
            //            break;
            //        case EPrimitiveDataType.SRPDT_INT8:
            //            WriteInt8(writetags[i].Name, writetags[i].Data.Value.Int8);                  
            //            break;
            //        case EPrimitiveDataType.SRPDT_INT16:
            //            WriteInt16(writetags[i].Name, writetags[i].Data.Value.Int16);
            //            break;
            //        case EPrimitiveDataType.SRPDT_INT32:
            //            WriteInt32(writetags[i].Name, writetags[i].Data.Value.Int32);
            //            break;
            //        case EPrimitiveDataType.SRPDT_INT64:
            //            WriteInt64(writetags[i].Name, writetags[i].Data.Value.Int64);
            //            break;
            //        case EPrimitiveDataType.SRPDT_UINT8:
            //            WriteUInt8(writetags[i].Name, writetags[i].Data.Value.UInt8);
            //            break;
            //        case EPrimitiveDataType.SRPDT_UINT16:
            //            WriteUInt16(writetags[i].Name, writetags[i].Data.Value.UInt16);
            //            break;
            //        case EPrimitiveDataType.SRPDT_UINT32:
            //            WriteUInt32(writetags[i].Name, writetags[i].Data.Value.UInt32);               
            //            break;
            //        case EPrimitiveDataType.SRPDT_UINT64:
            //            WriteUInt64(writetags[i].Name, writetags[i].Data.Value.UInt64);
            //            break;
            //        case EPrimitiveDataType.SRPDT_FLOAT:
            //            WriteFloat(writetags[i].Name, writetags[i].Data.Value.Float);
            //            break;
            //        case EPrimitiveDataType.SRPDT_DOUBLE:
            //            WriteDouble(writetags[i].Name, writetags[i].Data.Value.Double);
            //            break;
            //        case EPrimitiveDataType.SRPDT_CHAR:
            //            WriteChar(writetags[i].Name, writetags[i].Data.Value.Char);
            //            break;
            //        case EPrimitiveDataType.SRPDT_WCHAR:
            //            WriteWChar(writetags[i].Name, writetags[i].Data.Value.WChar);
            //            break;
            //        default:
            //            break;
            //    }
            //}
        }
        #endregion C'tor

    }

    #region GUI
    //public class GUISiemens : EditorWindow
    //{

    //    static bool avail = false;


    //    [MenuItem("SIEMENS/PLCSIM Advanced V2")]
    //    public static void ShowWindow()
    //    {
    //        //UnityEditor.Compilation.Assembly test = new UnityEditor.Compilation.Assembly("name","/",);

    //        GetWindow(typeof(GUISiemens));
    //        //avail = JustAValue();
    //    }

    //    void OnGUI()
    //    {
    //        GUILayout.Label("Base Settings", EditorStyles.boldLabel);
    //        EditorGUILayout.Toggle("RTM avail", avail);
    //    }

    //}
    #endregion GUI
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).