diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 00000000..8c1d5a32 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,7 @@ + + + + latest + + + \ No newline at end of file diff --git a/src/.editorconfig b/src/.editorconfig index 9b4acce9..cdf9366c 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -110,7 +110,7 @@ csharp_preferred_modifier_order = public,private,protected,internal,static,exter # Code-block preferences csharp_prefer_braces = true csharp_prefer_simple_using_statement = true -csharp_style_namespace_declarations = block_scoped +csharp_style_namespace_declarations = file_scoped csharp_style_prefer_method_group_conversion = true csharp_style_prefer_top_level_statements = true diff --git a/src/libplctag.NativeImport/CONN_STATUS.cs b/src/libplctag.NativeImport/CONN_STATUS.cs index bdd43efd..9f8caab2 100644 --- a/src/libplctag.NativeImport/CONN_STATUS.cs +++ b/src/libplctag.NativeImport/CONN_STATUS.cs @@ -5,50 +5,49 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +/// +/// You can query the connection status of a tag using the connection_status attribute +/// with : +/// +/// int status = plc_tag_get_int_attribute(tag, "connection_status", PLCTAG_CONN_STATUS_DOWN); +/// +/// +/// This attribute is read-only and can be queried at any time after tag creation. +/// It is useful for determining the current state of the connection to the PLC +/// and can be used in conjunction with callbacks to monitor connection lifecycle events. +/// +/// +public enum CONN_STATUS { /// - /// You can query the connection status of a tag using the connection_status attribute - /// with : - /// - /// int status = plc_tag_get_int_attribute(tag, "connection_status", PLCTAG_CONN_STATUS_DOWN); - /// - /// - /// This attribute is read-only and can be queried at any time after tag creation. - /// It is useful for determining the current state of the connection to the PLC - /// and can be used in conjunction with callbacks to monitor connection lifecycle events. - /// + /// Connected and ready for communication. /// - public enum CONN_STATUS - { - /// - /// Connected and ready for communication. - /// - PLCTAG_CONN_STATUS_UP = 0, + PLCTAG_CONN_STATUS_UP = 0, - /// - /// Not connected. - /// - PLCTAG_CONN_STATUS_DOWN = 1, + /// + /// Not connected. + /// + PLCTAG_CONN_STATUS_DOWN = 1, - /// - /// Disconnection in progress. - /// - PLCTAG_CONN_STATUS_DISCONNECTING = 2, + /// + /// Disconnection in progress. + /// + PLCTAG_CONN_STATUS_DISCONNECTING = 2, - /// - /// Connection in progress. - /// - PLCTAG_CONN_STATUS_CONNECTING = 3, + /// + /// Connection in progress. + /// + PLCTAG_CONN_STATUS_CONNECTING = 3, - /// - /// Waiting to reconnect after disconnect. - /// - PLCTAG_CONN_STATUS_IDLE_WAIT = 4, + /// + /// Waiting to reconnect after disconnect. + /// + PLCTAG_CONN_STATUS_IDLE_WAIT = 4, - /// - /// Waiting to retry after error. - /// - PLCTAG_CONN_STATUS_ERR_WAIT = 5 - } -} + /// + /// Waiting to retry after error. + /// + PLCTAG_CONN_STATUS_ERR_WAIT = 5 +} \ No newline at end of file diff --git a/src/libplctag.NativeImport/DEBUG_LEVEL.cs b/src/libplctag.NativeImport/DEBUG_LEVEL.cs index 4bb28e6f..86b246a4 100644 --- a/src/libplctag.NativeImport/DEBUG_LEVEL.cs +++ b/src/libplctag.NativeImport/DEBUG_LEVEL.cs @@ -5,52 +5,51 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +/// +/// The library provides debugging output when enabled. +/// There are three ways to set the debug level (for historical reasons): +/// +/// Adding a debug attribute to the attribute string when creating a tag: "protocol=XXX&...&debug=4". +/// Using the function to set the debug attribute. +/// Using the function. +/// +/// The preferred method in code is the last one. +/// +public enum DEBUG_LEVEL { /// - /// The library provides debugging output when enabled. - /// There are three ways to set the debug level (for historical reasons): - /// - /// Adding a debug attribute to the attribute string when creating a tag: "protocol=XXX&...&debug=4". - /// Using the function to set the debug attribute. - /// Using the function. - /// - /// The preferred method in code is the last one. + /// Disables debugging output. /// - public enum DEBUG_LEVEL - { - /// - /// Disables debugging output. - /// - PLCTAG_DEBUG_NONE = 0, + PLCTAG_DEBUG_NONE = 0, - /// - /// Only output errors. Generally these are fatal to the functioning of the library. - /// - PLCTAG_DEBUG_ERROR = 1, + /// + /// Only output errors. Generally these are fatal to the functioning of the library. + /// + PLCTAG_DEBUG_ERROR = 1, - /// - /// Outputs warnings such as error found when checking a malformed tag attribute string or when unexpected problems are reported from the PLC. - /// - PLCTAG_DEBUG_WARN = 2, + /// + /// Outputs warnings such as error found when checking a malformed tag attribute string or when unexpected problems are reported from the PLC. + /// + PLCTAG_DEBUG_WARN = 2, - /// - /// Outputs diagnostic information about the internal calls within the library. - /// Includes some packet dumps. - /// - PLCTAG_DEBUG_INFO = 3, + /// + /// Outputs diagnostic information about the internal calls within the library. + /// Includes some packet dumps. + /// + PLCTAG_DEBUG_INFO = 3, - /// - /// Outputs detailed diagnostic information about the code executing within the library including packet dumps. - /// - PLCTAG_DEBUG_DETAIL = 4, + /// + /// Outputs detailed diagnostic information about the code executing within the library including packet dumps. + /// + PLCTAG_DEBUG_DETAIL = 4, - /// - /// Outputs extremely detailed information. - /// Do not use this unless you are trying to debug detailed information about every mutex lock and release. - /// Will output many lines of output per millisecond. - /// You have been warned! - /// - PLCTAG_DEBUG_SPEW = 5 - } -} + /// + /// Outputs extremely detailed information. + /// Do not use this unless you are trying to debug detailed information about every mutex lock and release. + /// Will output many lines of output per millisecond. + /// You have been warned! + /// + PLCTAG_DEBUG_SPEW = 5 +} \ No newline at end of file diff --git a/src/libplctag.NativeImport/DEBUG_MODULE.cs b/src/libplctag.NativeImport/DEBUG_MODULE.cs index 1f2fd51c..a769ae3e 100644 --- a/src/libplctag.NativeImport/DEBUG_MODULE.cs +++ b/src/libplctag.NativeImport/DEBUG_MODULE.cs @@ -5,41 +5,40 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +/// +/// The library supports setting debug levels for individual modules. +/// This allows for fine-grained control over which subsystems produce debug output. +/// Used with and . +/// +public enum DEBUG_MODULE { - /// - /// The library supports setting debug levels for individual modules. - /// This allows for fine-grained control over which subsystems produce debug output. - /// Used with and . - /// - public enum DEBUG_MODULE - { - PLCTAG_MODULE_LIB = 0, - PLCTAG_MODULE_INIT = 1, - PLCTAG_MODULE_VERSION = 2, - PLCTAG_MODULE_UTILS = 3, - PLCTAG_MODULE_AB_SESSION = 4, - PLCTAG_MODULE_AB_PCCC = 5, - PLCTAG_MODULE_AB_CIP = 6, - PLCTAG_MODULE_AB_COMMON = 7, - PLCTAG_MODULE_AB_EIP_CIP = 8, - PLCTAG_MODULE_AB_EIP_CIP_SPECIAL = 9, - PLCTAG_MODULE_AB_EIP_LGX_PCCC = 10, - PLCTAG_MODULE_AB_EIP_PLC5_PCCC = 11, - PLCTAG_MODULE_AB_EIP_PLC5_DHP = 12, - PLCTAG_MODULE_AB_EIP_SLC_PCCC = 13, - PLCTAG_MODULE_AB_EIP_SLC_DHP = 14, - PLCTAG_MODULE_AB_ERROR = 15, - PLCTAG_MODULE_OMRON_CONN = 16, - PLCTAG_MODULE_OMRON_CIP = 17, - PLCTAG_MODULE_OMRON_COMMON = 18, - PLCTAG_MODULE_OMRON_STANDARD_TAG = 19, - PLCTAG_MODULE_OMRON_RAW_TAG = 20, - PLCTAG_MODULE_MODBUS = 21, - PLCTAG_MODULE_SYSTEM = 22, - PLCTAG_MODULE_PLATFORM = 23, - PLCTAG_MODULE_AB_CONNECTION = 24, - PLCTAG_MODULE_MB_CONNECTION = 25, - PLCTAG_MODULE_OMRON_CONNECTION = 26, - } -} + PLCTAG_MODULE_LIB = 0, + PLCTAG_MODULE_INIT = 1, + PLCTAG_MODULE_VERSION = 2, + PLCTAG_MODULE_UTILS = 3, + PLCTAG_MODULE_AB_SESSION = 4, + PLCTAG_MODULE_AB_PCCC = 5, + PLCTAG_MODULE_AB_CIP = 6, + PLCTAG_MODULE_AB_COMMON = 7, + PLCTAG_MODULE_AB_EIP_CIP = 8, + PLCTAG_MODULE_AB_EIP_CIP_SPECIAL = 9, + PLCTAG_MODULE_AB_EIP_LGX_PCCC = 10, + PLCTAG_MODULE_AB_EIP_PLC5_PCCC = 11, + PLCTAG_MODULE_AB_EIP_PLC5_DHP = 12, + PLCTAG_MODULE_AB_EIP_SLC_PCCC = 13, + PLCTAG_MODULE_AB_EIP_SLC_DHP = 14, + PLCTAG_MODULE_AB_ERROR = 15, + PLCTAG_MODULE_OMRON_CONN = 16, + PLCTAG_MODULE_OMRON_CIP = 17, + PLCTAG_MODULE_OMRON_COMMON = 18, + PLCTAG_MODULE_OMRON_STANDARD_TAG = 19, + PLCTAG_MODULE_OMRON_RAW_TAG = 20, + PLCTAG_MODULE_MODBUS = 21, + PLCTAG_MODULE_SYSTEM = 22, + PLCTAG_MODULE_PLATFORM = 23, + PLCTAG_MODULE_AB_CONNECTION = 24, + PLCTAG_MODULE_MB_CONNECTION = 25, + PLCTAG_MODULE_OMRON_CONNECTION = 26, +} \ No newline at end of file diff --git a/src/libplctag.NativeImport/EVENT.cs b/src/libplctag.NativeImport/EVENT.cs index 703d693f..58a1bea2 100644 --- a/src/libplctag.NativeImport/EVENT.cs +++ b/src/libplctag.NativeImport/EVENT.cs @@ -5,85 +5,84 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +/// +/// The library provides functions to register a callback on a tag. +/// These events generate a call to that callback. +/// +public enum EVENT { /// - /// The library provides functions to register a callback on a tag. - /// These events generate a call to that callback. + /// A read of the tag has been requested. + /// The callback is called immediately before the underlying protocol implementation is called. /// - public enum EVENT - { - /// - /// A read of the tag has been requested. - /// The callback is called immediately before the underlying protocol implementation is called. - /// - PLCTAG_EVENT_READ_STARTED = 1, + PLCTAG_EVENT_READ_STARTED = 1, - /// - /// The callback is called after a read completes. - /// The final status of the read is passed to the callback as well. - /// - PLCTAG_EVENT_READ_COMPLETED = 2, + /// + /// The callback is called after a read completes. + /// The final status of the read is passed to the callback as well. + /// + PLCTAG_EVENT_READ_COMPLETED = 2, - /// - /// As with reads, the callback is called when a write is requested. - /// The callback can change the data in the tag and the changes will be sent to the PLC. - /// - PLCTAG_EVENT_WRITE_STARTED = 3, + /// + /// As with reads, the callback is called when a write is requested. + /// The callback can change the data in the tag and the changes will be sent to the PLC. + /// + PLCTAG_EVENT_WRITE_STARTED = 3, - /// - /// The callback is called when the PLC indicates that the write has completed. - /// The status of the write is passed to the callback. - /// - PLCTAG_EVENT_WRITE_COMPLETED = 4, + /// + /// The callback is called when the PLC indicates that the write has completed. + /// The status of the write is passed to the callback. + /// + PLCTAG_EVENT_WRITE_COMPLETED = 4, - /// - /// The callback function is called when something calls on the tag. - /// - PLCTAG_EVENT_ABORTED = 5, + /// + /// The callback function is called when something calls on the tag. + /// + PLCTAG_EVENT_ABORTED = 5, - /// - /// The callback function is called when the tag is being destroyed. - /// It is not safe to call any API functions on the tag at this time. - /// This is purely for the callback to manage any application state. - /// - PLCTAG_EVENT_DESTROYED = 6, + /// + /// The callback function is called when the tag is being destroyed. + /// It is not safe to call any API functions on the tag at this time. + /// This is purely for the callback to manage any application state. + /// + PLCTAG_EVENT_DESTROYED = 6, - /// - /// The callback is called after a tag creation completes. - /// The final status of the creation is passed to the callback as well. - /// This is not as well supported in some cases, so only depend on this for normal tags and not tags like @tags. - /// - PLCTAG_EVENT_CREATED = 7, + /// + /// The callback is called after a tag creation completes. + /// The final status of the creation is passed to the callback as well. + /// This is not as well supported in some cases, so only depend on this for normal tags and not tags like @tags. + /// + PLCTAG_EVENT_CREATED = 7, - /// - /// The underlying connection is established and ready for operations. - /// - PLCTAG_EVENT_CONN_STATUS_UP = 100, + /// + /// The underlying connection is established and ready for operations. + /// + PLCTAG_EVENT_CONN_STATUS_UP = 100, - /// - /// The connection is not established. - /// - PLCTAG_EVENT_CONN_STATUS_DOWN = 101, + /// + /// The connection is not established. + /// + PLCTAG_EVENT_CONN_STATUS_DOWN = 101, - /// - /// The connection is in the process of disconnecting. - /// - PLCTAG_EVENT_CONN_STATUS_DISCONNECTING = 102, + /// + /// The connection is in the process of disconnecting. + /// + PLCTAG_EVENT_CONN_STATUS_DISCONNECTING = 102, - /// - /// The connection is in the process of being established. - /// - PLCTAG_EVENT_CONN_STATUS_CONNECTING = 103, + /// + /// The connection is in the process of being established. + /// + PLCTAG_EVENT_CONN_STATUS_CONNECTING = 103, - /// - /// Waiting to reconnect after an idle disconnect. - /// - PLCTAG_EVENT_CONN_STATUS_IDLE_WAIT = 104, + /// + /// Waiting to reconnect after an idle disconnect. + /// + PLCTAG_EVENT_CONN_STATUS_IDLE_WAIT = 104, - /// - /// Waiting to reconnect after an error. - /// - PLCTAG_EVENT_CONN_STATUS_ERR_WAIT = 105, - } -} + /// + /// Waiting to reconnect after an error. + /// + PLCTAG_EVENT_CONN_STATUS_ERR_WAIT = 105, +} \ No newline at end of file diff --git a/src/libplctag.NativeImport/NativeMethods.cs b/src/libplctag.NativeImport/NativeMethods.cs index 00958089..f0b1c9ff 100644 --- a/src/libplctag.NativeImport/NativeMethods.cs +++ b/src/libplctag.NativeImport/NativeMethods.cs @@ -10,258 +10,257 @@ using System.Text; using static libplctag.NativeImport.plctag; -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +static class NativeMethods { - static class NativeMethods - { - const string DLL_NAME = "plctag"; + const string DLL_NAME = "plctag"; - static NativeMethods() - { + static NativeMethods() + { #if NET472_OR_GREATER - // Assume we're running on Windows because .NET Framework is not supported on other platforms - string executingDirectory = System.IO.Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location); - string system = Environment.Is64BitProcess ? "X64" : "X86"; - string libDirectory = System.IO.Path.Combine (executingDirectory, system); - if (System.IO.Directory.Exists(libDirectory)) - { - SetDllDirectory(libDirectory); - } - else - { - SetDllDirectory(executingDirectory); - } -#endif + // Assume we're running on Windows because .NET Framework is not supported on other platforms + string executingDirectory = System.IO.Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location); + string system = Environment.Is64BitProcess ? "X64" : "X86"; + string libDirectory = System.IO.Path.Combine (executingDirectory, system); + if (System.IO.Directory.Exists(libDirectory)) + { + SetDllDirectory(libDirectory); } + else + { + SetDllDirectory(executingDirectory); + } +#endif + } - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool SetDllDirectory(string lpPathName); + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool SetDllDirectory(string lpPathName); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_check_lib_version), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_check_lib_version), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern Int32 plc_tag_create([MarshalAs(UnmanagedType.LPStr)] string lpString, int timeout); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern Int32 plc_tag_create([MarshalAs(UnmanagedType.LPStr)] string lpString, int timeout); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create_ex), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern Int32 plc_tag_create_ex([MarshalAs(UnmanagedType.LPStr)] string lpString, callback_func_ex func, IntPtr userdata, int timeout); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create_ex), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern Int32 plc_tag_create_ex([MarshalAs(UnmanagedType.LPStr)] string lpString, callback_func_ex func, IntPtr userdata, int timeout); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create_from_tag), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern Int32 plc_tag_create_from_tag(Int32 src_tag_id, [MarshalAs(UnmanagedType.LPStr)] string attrib_str, callback_func_ex func, IntPtr userdata, int timeout); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create_from_tag), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern Int32 plc_tag_create_from_tag(Int32 src_tag_id, [MarshalAs(UnmanagedType.LPStr)] string attrib_str, callback_func_ex func, IntPtr userdata, int timeout); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_destroy), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_destroy(Int32 tag); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_destroy), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_destroy(Int32 tag); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_shutdown), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern void plc_tag_shutdown(); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_shutdown), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void plc_tag_shutdown(); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_callback), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_register_callback(Int32 tag_id, callback_func func); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_callback), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_register_callback(Int32 tag_id, callback_func func); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_callback_ex), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_register_callback_ex(Int32 tag_id, callback_func_ex func, IntPtr userdata); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_callback_ex), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_register_callback_ex(Int32 tag_id, callback_func_ex func, IntPtr userdata); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unregister_callback), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_unregister_callback(Int32 tag_id); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unregister_callback), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_unregister_callback(Int32 tag_id); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_logger), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_register_logger(log_callback_func func); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_logger), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_register_logger(log_callback_func func); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unregister_logger), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_unregister_logger(); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unregister_logger), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_unregister_logger(); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_lock), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_lock(Int32 tag); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_lock), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_lock(Int32 tag); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unlock), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_unlock(Int32 tag); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unlock), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_unlock(Int32 tag); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_status), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_status(Int32 tag); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_status), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_status(Int32 tag); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_decode_error), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern IntPtr plc_tag_decode_error(int err); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_decode_error), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern IntPtr plc_tag_decode_error(int err); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_read), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_read(Int32 tag, int timeout); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_read), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_read(Int32 tag, int timeout); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_write), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_write(Int32 tag, int timeout); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_write), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_write(Int32 tag, int timeout); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_size), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_size(Int32 tag); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_size), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_size(Int32 tag); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_size), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_size(Int32 tag, int new_size); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_size), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_size(Int32 tag, int new_size); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_abort), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_abort(Int32 tag); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_abort), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_abort(Int32 tag); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern int plc_tag_get_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int default_value); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern int plc_tag_get_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int default_value); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern int plc_tag_set_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int new_value); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern int plc_tag_set_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int new_value); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_byte_array_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern int plc_tag_get_byte_array_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, [In] byte[] buffer, int buffer_length); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_byte_array_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern int plc_tag_get_byte_array_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, [In] byte[] buffer, int buffer_length); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern UInt64 plc_tag_get_uint64(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern UInt64 plc_tag_get_uint64(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern Int64 plc_tag_get_int64(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Int64 plc_tag_get_int64(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_int64(Int32 tag, int offset, Int64 val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_int64(Int32 tag, int offset, Int64 val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_float64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern double plc_tag_get_float64(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_float64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern double plc_tag_get_float64(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_float64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_float64(Int32 tag, int offset, double val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_float64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_float64(Int32 tag, int offset, double val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern UInt32 plc_tag_get_uint32(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern UInt32 plc_tag_get_uint32(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern Int32 plc_tag_get_int32(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Int32 plc_tag_get_int32(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_int32(Int32 tag, int offset, Int32 val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_int32(Int32 tag, int offset, Int32 val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_float32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern float plc_tag_get_float32(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_float32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern float plc_tag_get_float32(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_float32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_float32(Int32 tag, int offset, float val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_float32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_float32(Int32 tag, int offset, float val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern UInt16 plc_tag_get_uint16(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern UInt16 plc_tag_get_uint16(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern Int16 plc_tag_get_int16(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern Int16 plc_tag_get_int16(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_int16(Int32 tag, int offset, Int16 val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_int16(Int32 tag, int offset, Int16 val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern byte plc_tag_get_uint8(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern byte plc_tag_get_uint8(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern sbyte plc_tag_get_int8(Int32 tag, int offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern sbyte plc_tag_get_int8(Int32 tag, int offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_uint8(Int32 tag, int offset, byte val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_uint8(Int32 tag, int offset, byte val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_int8(Int32 tag, int offset, sbyte val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_int8(Int32 tag, int offset, sbyte val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_bit), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_bit(Int32 tag, int offset_bit); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_bit), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_bit(Int32 tag, int offset_bit); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_bit), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_bit(Int32 tag, int offset_bit, int val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_bit), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_bit(Int32 tag, int offset_bit, int val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_debug_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern void plc_tag_set_debug_level(int debug_level); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_debug_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void plc_tag_set_debug_level(int debug_level); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_debug_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_debug_level(); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_debug_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_debug_level(); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_debug_module_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_set_debug_module_level(int module, int debug_level); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_debug_module_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_set_debug_module_level(int module, int debug_level); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_debug_module_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_debug_module_level(int module); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_debug_module_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_debug_module_level(int module); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern int plc_tag_get_string(Int32 tag_id, int string_start_offset, StringBuilder buffer, int buffer_length); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern int plc_tag_get_string(Int32 tag_id, int string_start_offset, StringBuilder buffer, int buffer_length); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_string), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] - public static extern int plc_tag_set_string(Int32 tag_id, int string_start_offset, [MarshalAs(UnmanagedType.LPStr)] string string_val); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_string), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] + public static extern int plc_tag_set_string(Int32 tag_id, int string_start_offset, [MarshalAs(UnmanagedType.LPStr)] string string_val); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_length), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_string_length(Int32 tag_id, int string_start_offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_length), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_string_length(Int32 tag_id, int string_start_offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_capacity), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_string_capacity(Int32 tag_id, int string_start_offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_capacity), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_string_capacity(Int32 tag_id, int string_start_offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_total_length), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int plc_tag_get_string_total_length(Int32 tag_id, int string_start_offset); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_total_length), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int plc_tag_get_string_total_length(Int32 tag_id, int string_start_offset); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public unsafe static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length); + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public unsafe static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length); - [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public unsafe static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length); - } -} + [DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public unsafe static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length); +} \ No newline at end of file diff --git a/src/libplctag.NativeImport/STATUS.cs b/src/libplctag.NativeImport/STATUS.cs index 0abdcc8d..c6f18bc5 100644 --- a/src/libplctag.NativeImport/STATUS.cs +++ b/src/libplctag.NativeImport/STATUS.cs @@ -5,218 +5,217 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +/// +/// Most functions return a status code. +/// It will be one of these. +/// +public enum STATUS { /// - /// Most functions return a status code. - /// It will be one of these. - /// - public enum STATUS - { - /// - /// Operation in progress. Not an error. - /// - PLCTAG_STATUS_PENDING = 1, - - /// - /// No error. - /// - PLCTAG_STATUS_OK = 0, - - /// - /// The operation was aborted. - /// - PLCTAG_ERR_ABORT = -1, - - /// - /// The operation failed due to incorrect configuration. Usually returned from a remote system. - /// - PLCTAG_ERR_BAD_CONFIG = -2, - - /// - /// The connection failed for some reason. This can mean that the remote PLC was power cycled, for instance. - /// - PLCTAG_ERR_BAD_CONNECTION = -3, - - /// - /// The data received from the remote PLC was undecipherable or otherwise not able to be processed. - /// Can also be returned from a remote system that cannot process the data sent to it. - /// - PLCTAG_ERR_BAD_DATA = -4, - - /// - /// Usually returned from a remote system when something addressed does not exist. - /// - PLCTAG_ERR_BAD_DEVICE = -5, - - /// - /// Usually returned when the library is unable to connect to a remote system. - /// - PLCTAG_ERR_BAD_GATEWAY = -6, - - /// - /// A common error return when something is not correct with the tag creation attribute string. - /// - PLCTAG_ERR_BAD_PARAM = -7, - - /// - /// Usually returned when the remote system returned an unexpected response. - /// - PLCTAG_ERR_BAD_REPLY = -8, - - /// - /// Usually returned by a remote system when something is not in a good state. - /// - PLCTAG_ERR_BAD_STATUS = -9, - - /// - /// An error occurred trying to close some resource. - /// - PLCTAG_ERR_CLOSE = -10, - - /// - /// An error occurred trying to create some internal resource. - /// - PLCTAG_ERR_CREATE = -11, - - /// - /// An error returned by a remote system when something is incorrectly duplicated (i.e. a duplicate connection ID). - /// - PLCTAG_ERR_DUPLICATE = -12, - - /// - /// An error was returned when trying to encode some data such as a tag name. - /// - PLCTAG_ERR_ENCODE = -13, - - /// - /// An internal library error. It would be very unusual to see this. - /// - PLCTAG_ERR_MUTEX_DESTROY = -14, - - /// - /// An internal library error. It would be very unusual to see this. - /// - PLCTAG_ERR_MUTEX_INIT = -15, - - /// - /// An internal library error. It would be very unusual to see this. - /// - PLCTAG_ERR_MUTEX_LOCK = -16, - - /// - /// An internal library error. It would be very unusual to see this. - /// - PLCTAG_ERR_MUTEX_UNLOCK = -17, - - /// - /// Often returned from the remote system when an operation is not permitted. - /// - PLCTAG_ERR_NOT_ALLOWED = -18, - - /// - /// Often returned from the remote system when something is not found. - /// - PLCTAG_ERR_NOT_FOUND = -19, - - /// - /// returned when a valid operation is not implemented. - /// - PLCTAG_ERR_NOT_IMPLEMENTED = -20, - - /// - /// Returned when expected data is not present. - /// - PLCTAG_ERR_NO_DATA = -21, - - /// - /// Similar to - /// - PLCTAG_ERR_NO_MATCH = -22, - - /// - /// Returned by the library when memory allocation fails. - /// - PLCTAG_ERR_NO_MEM = -23, - - /// - /// Returned by the remote system when some resource allocation fails. - /// - PLCTAG_ERR_NO_RESOURCES = -24, - - /// - /// Usually an internal error, but can be returned when an invalid handle is used with an API call. - /// - PLCTAG_ERR_NULL_PTR = -25, - - /// - /// Returned when an error occurs opening a resource such as a socket. - /// - PLCTAG_ERR_OPEN = -26, - - /// - /// Usually returned when trying to write a value into a tag outside of the tag data bounds. - /// - PLCTAG_ERR_OUT_OF_BOUNDS = -27, - - /// - /// Returned when an error occurs during a read operation. Usually related to socket problems. - /// - PLCTAG_ERR_READ = -28, - - /// - /// An unspecified or untranslatable remote error causes this. - /// - PLCTAG_ERR_REMOTE_ERR = -29, - - /// - /// An internal library error. If you see this, it is likely that everything is about to crash. - /// - PLCTAG_ERR_THREAD_CREATE = -30, - - /// - /// Another internal library error. It is very unlikely that you will see this. - /// - PLCTAG_ERR_THREAD_JOIN = -31, - - /// - /// An operation took too long and timed out. - /// - PLCTAG_ERR_TIMEOUT = -32, - - /// - /// More data was returned than was expected. - /// - PLCTAG_ERR_TOO_LARGE = -33, - - /// - /// Insufficient data was returned from the remote system. - /// - PLCTAG_ERR_TOO_SMALL = -34, - - /// - /// The operation is not supported on the remote system. - /// - PLCTAG_ERR_UNSUPPORTED = -35, - - /// - /// A Winsock-specific error occurred (only on Windows). - /// - PLCTAG_ERR_WINSOCK = -36, - - /// - /// An error occurred trying to write, usually to a socket. - /// - PLCTAG_ERR_WRITE = -37, - - /// - /// Partial data was received or something was unexpectedly incomplete. - /// - PLCTAG_ERR_PARTIAL = -38, - - /// - /// The operation cannot be performed as some other operation is taking place. - /// - PLCTAG_ERR_BUSY = -39 - } -} + /// Operation in progress. Not an error. + /// + PLCTAG_STATUS_PENDING = 1, + + /// + /// No error. + /// + PLCTAG_STATUS_OK = 0, + + /// + /// The operation was aborted. + /// + PLCTAG_ERR_ABORT = -1, + + /// + /// The operation failed due to incorrect configuration. Usually returned from a remote system. + /// + PLCTAG_ERR_BAD_CONFIG = -2, + + /// + /// The connection failed for some reason. This can mean that the remote PLC was power cycled, for instance. + /// + PLCTAG_ERR_BAD_CONNECTION = -3, + + /// + /// The data received from the remote PLC was undecipherable or otherwise not able to be processed. + /// Can also be returned from a remote system that cannot process the data sent to it. + /// + PLCTAG_ERR_BAD_DATA = -4, + + /// + /// Usually returned from a remote system when something addressed does not exist. + /// + PLCTAG_ERR_BAD_DEVICE = -5, + + /// + /// Usually returned when the library is unable to connect to a remote system. + /// + PLCTAG_ERR_BAD_GATEWAY = -6, + + /// + /// A common error return when something is not correct with the tag creation attribute string. + /// + PLCTAG_ERR_BAD_PARAM = -7, + + /// + /// Usually returned when the remote system returned an unexpected response. + /// + PLCTAG_ERR_BAD_REPLY = -8, + + /// + /// Usually returned by a remote system when something is not in a good state. + /// + PLCTAG_ERR_BAD_STATUS = -9, + + /// + /// An error occurred trying to close some resource. + /// + PLCTAG_ERR_CLOSE = -10, + + /// + /// An error occurred trying to create some internal resource. + /// + PLCTAG_ERR_CREATE = -11, + + /// + /// An error returned by a remote system when something is incorrectly duplicated (i.e. a duplicate connection ID). + /// + PLCTAG_ERR_DUPLICATE = -12, + + /// + /// An error was returned when trying to encode some data such as a tag name. + /// + PLCTAG_ERR_ENCODE = -13, + + /// + /// An internal library error. It would be very unusual to see this. + /// + PLCTAG_ERR_MUTEX_DESTROY = -14, + + /// + /// An internal library error. It would be very unusual to see this. + /// + PLCTAG_ERR_MUTEX_INIT = -15, + + /// + /// An internal library error. It would be very unusual to see this. + /// + PLCTAG_ERR_MUTEX_LOCK = -16, + + /// + /// An internal library error. It would be very unusual to see this. + /// + PLCTAG_ERR_MUTEX_UNLOCK = -17, + + /// + /// Often returned from the remote system when an operation is not permitted. + /// + PLCTAG_ERR_NOT_ALLOWED = -18, + + /// + /// Often returned from the remote system when something is not found. + /// + PLCTAG_ERR_NOT_FOUND = -19, + + /// + /// returned when a valid operation is not implemented. + /// + PLCTAG_ERR_NOT_IMPLEMENTED = -20, + + /// + /// Returned when expected data is not present. + /// + PLCTAG_ERR_NO_DATA = -21, + + /// + /// Similar to + /// + PLCTAG_ERR_NO_MATCH = -22, + + /// + /// Returned by the library when memory allocation fails. + /// + PLCTAG_ERR_NO_MEM = -23, + + /// + /// Returned by the remote system when some resource allocation fails. + /// + PLCTAG_ERR_NO_RESOURCES = -24, + + /// + /// Usually an internal error, but can be returned when an invalid handle is used with an API call. + /// + PLCTAG_ERR_NULL_PTR = -25, + + /// + /// Returned when an error occurs opening a resource such as a socket. + /// + PLCTAG_ERR_OPEN = -26, + + /// + /// Usually returned when trying to write a value into a tag outside of the tag data bounds. + /// + PLCTAG_ERR_OUT_OF_BOUNDS = -27, + + /// + /// Returned when an error occurs during a read operation. Usually related to socket problems. + /// + PLCTAG_ERR_READ = -28, + + /// + /// An unspecified or untranslatable remote error causes this. + /// + PLCTAG_ERR_REMOTE_ERR = -29, + + /// + /// An internal library error. If you see this, it is likely that everything is about to crash. + /// + PLCTAG_ERR_THREAD_CREATE = -30, + + /// + /// Another internal library error. It is very unlikely that you will see this. + /// + PLCTAG_ERR_THREAD_JOIN = -31, + + /// + /// An operation took too long and timed out. + /// + PLCTAG_ERR_TIMEOUT = -32, + + /// + /// More data was returned than was expected. + /// + PLCTAG_ERR_TOO_LARGE = -33, + + /// + /// Insufficient data was returned from the remote system. + /// + PLCTAG_ERR_TOO_SMALL = -34, + + /// + /// The operation is not supported on the remote system. + /// + PLCTAG_ERR_UNSUPPORTED = -35, + + /// + /// A Winsock-specific error occurred (only on Windows). + /// + PLCTAG_ERR_WINSOCK = -36, + + /// + /// An error occurred trying to write, usually to a socket. + /// + PLCTAG_ERR_WRITE = -37, + + /// + /// Partial data was received or something was unexpectedly incomplete. + /// + PLCTAG_ERR_PARTIAL = -38, + + /// + /// The operation cannot be performed as some other operation is taking place. + /// + PLCTAG_ERR_BUSY = -39 +} \ No newline at end of file diff --git a/src/libplctag.NativeImport/plctag.cs b/src/libplctag.NativeImport/plctag.cs index 1afbdd09..7a7fde98 100644 --- a/src/libplctag.NativeImport/plctag.cs +++ b/src/libplctag.NativeImport/plctag.cs @@ -9,327 +9,326 @@ using System.Runtime.InteropServices; using System.Text; -namespace libplctag.NativeImport +namespace libplctag.NativeImport; + +/// +/// This class provides low-level (raw) access to the native libplctag library (which is written in C). +/// The purpose of this package is to expose the API for this native library, and handle platform and configuration issues. +/// +/// See for documentation. +/// +public static class plctag { - /// - /// This class provides low-level (raw) access to the native libplctag library (which is written in C). - /// The purpose of this package is to expose the API for this native library, and handle platform and configuration issues. - /// - /// See for documentation. - /// - public static class plctag - { - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void callback_func(Int32 tag_id, Int32 event_id, Int32 status); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void callback_func(Int32 tag_id, Int32 event_id, Int32 status); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void callback_func_ex(Int32 tag_id, Int32 event_id, Int32 status, IntPtr userdata); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void callback_func_ex(Int32 tag_id, Int32 event_id, Int32 status, IntPtr userdata); - [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public delegate void log_callback_func(Int32 tag_id, int debug_level, [MarshalAs(UnmanagedType.LPStr)] string message); + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public delegate void log_callback_func(Int32 tag_id, int debug_level, [MarshalAs(UnmanagedType.LPStr)] string message); - public static int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch) - { - return NativeMethods.plc_tag_check_lib_version(req_major, req_minor, req_patch); - } + public static int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch) + { + return NativeMethods.plc_tag_check_lib_version(req_major, req_minor, req_patch); + } - public static Int32 plc_tag_create(string lpString, int timeout) - { - return NativeMethods.plc_tag_create(lpString, timeout); - } + public static Int32 plc_tag_create(string lpString, int timeout) + { + return NativeMethods.plc_tag_create(lpString, timeout); + } - public static Int32 plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout) - { - return NativeMethods.plc_tag_create_ex(lpString, func, userdata, timeout); - } + public static Int32 plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout) + { + return NativeMethods.plc_tag_create_ex(lpString, func, userdata, timeout); + } - public static Int32 plc_tag_create_from_tag(Int32 src_tag_id, string attrib_str, callback_func_ex func, IntPtr userdata, int timeout) - { - return NativeMethods.plc_tag_create_from_tag(src_tag_id, attrib_str, func, userdata, timeout); - } + public static Int32 plc_tag_create_from_tag(Int32 src_tag_id, string attrib_str, callback_func_ex func, IntPtr userdata, int timeout) + { + return NativeMethods.plc_tag_create_from_tag(src_tag_id, attrib_str, func, userdata, timeout); + } - public static int plc_tag_destroy(Int32 tag) - { - return NativeMethods.plc_tag_destroy(tag); - } + public static int plc_tag_destroy(Int32 tag) + { + return NativeMethods.plc_tag_destroy(tag); + } - public static void plc_tag_shutdown() - { - NativeMethods.plc_tag_shutdown(); - } + public static void plc_tag_shutdown() + { + NativeMethods.plc_tag_shutdown(); + } - public static int plc_tag_register_callback(Int32 tag_id, callback_func func) - { - return NativeMethods.plc_tag_register_callback(tag_id, func); - } + public static int plc_tag_register_callback(Int32 tag_id, callback_func func) + { + return NativeMethods.plc_tag_register_callback(tag_id, func); + } - public static int plc_tag_register_callback_ex(Int32 tag_id, callback_func_ex func, IntPtr userdata) - { - return NativeMethods.plc_tag_register_callback_ex(tag_id, func, userdata); - } + public static int plc_tag_register_callback_ex(Int32 tag_id, callback_func_ex func, IntPtr userdata) + { + return NativeMethods.plc_tag_register_callback_ex(tag_id, func, userdata); + } - public static int plc_tag_unregister_callback(Int32 tag_id) - { - return NativeMethods.plc_tag_unregister_callback(tag_id); - } + public static int plc_tag_unregister_callback(Int32 tag_id) + { + return NativeMethods.plc_tag_unregister_callback(tag_id); + } - public static int plc_tag_register_logger(log_callback_func func) - { - return NativeMethods.plc_tag_register_logger(func); - } - public static int plc_tag_unregister_logger() - { - return NativeMethods.plc_tag_unregister_logger(); - } + public static int plc_tag_register_logger(log_callback_func func) + { + return NativeMethods.plc_tag_register_logger(func); + } + public static int plc_tag_unregister_logger() + { + return NativeMethods.plc_tag_unregister_logger(); + } - public static int plc_tag_lock(Int32 tag) - { - return NativeMethods.plc_tag_lock(tag); - } + public static int plc_tag_lock(Int32 tag) + { + return NativeMethods.plc_tag_lock(tag); + } - public static int plc_tag_unlock(Int32 tag) - { - return NativeMethods.plc_tag_unlock(tag); - } - public static int plc_tag_status(Int32 tag) - { - return NativeMethods.plc_tag_status(tag); - } + public static int plc_tag_unlock(Int32 tag) + { + return NativeMethods.plc_tag_unlock(tag); + } + public static int plc_tag_status(Int32 tag) + { + return NativeMethods.plc_tag_status(tag); + } - public static string plc_tag_decode_error(int err) - { - return Marshal.PtrToStringAnsi(NativeMethods.plc_tag_decode_error(err)); - } + public static string plc_tag_decode_error(int err) + { + return Marshal.PtrToStringAnsi(NativeMethods.plc_tag_decode_error(err)); + } - public static int plc_tag_read(Int32 tag, int timeout) - { - return NativeMethods.plc_tag_read(tag, timeout); - } + public static int plc_tag_read(Int32 tag, int timeout) + { + return NativeMethods.plc_tag_read(tag, timeout); + } - public static int plc_tag_write(Int32 tag, int timeout) - { - return NativeMethods.plc_tag_write(tag, timeout); - } + public static int plc_tag_write(Int32 tag, int timeout) + { + return NativeMethods.plc_tag_write(tag, timeout); + } - public static int plc_tag_get_size(Int32 tag) - { - return NativeMethods.plc_tag_get_size(tag); - } + public static int plc_tag_get_size(Int32 tag) + { + return NativeMethods.plc_tag_get_size(tag); + } - public static int plc_tag_set_size(Int32 tag, int new_size) - { - return NativeMethods.plc_tag_set_size(tag, new_size); - } + public static int plc_tag_set_size(Int32 tag, int new_size) + { + return NativeMethods.plc_tag_set_size(tag, new_size); + } - public static int plc_tag_abort(Int32 tag) - { - return NativeMethods.plc_tag_abort(tag); - } + public static int plc_tag_abort(Int32 tag) + { + return NativeMethods.plc_tag_abort(tag); + } - public static int plc_tag_get_int_attribute(Int32 tag, string attrib_name, int default_value) - { - return NativeMethods.plc_tag_get_int_attribute(tag, attrib_name, default_value); - } + public static int plc_tag_get_int_attribute(Int32 tag, string attrib_name, int default_value) + { + return NativeMethods.plc_tag_get_int_attribute(tag, attrib_name, default_value); + } - public static int plc_tag_set_int_attribute(Int32 tag, string attrib_name, int new_value) - { - return NativeMethods.plc_tag_set_int_attribute(tag, attrib_name, new_value); - } + public static int plc_tag_set_int_attribute(Int32 tag, string attrib_name, int new_value) + { + return NativeMethods.plc_tag_set_int_attribute(tag, attrib_name, new_value); + } - public static int plc_tag_get_byte_array_attribute(Int32 tag, string attrib_name, byte[] buffer, int buffer_length) - { - return NativeMethods.plc_tag_get_byte_array_attribute(tag, attrib_name, buffer, buffer_length); - } + public static int plc_tag_get_byte_array_attribute(Int32 tag, string attrib_name, byte[] buffer, int buffer_length) + { + return NativeMethods.plc_tag_get_byte_array_attribute(tag, attrib_name, buffer, buffer_length); + } - public static UInt64 plc_tag_get_uint64(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_uint64(tag, offset); - } + public static UInt64 plc_tag_get_uint64(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_uint64(tag, offset); + } - public static Int64 plc_tag_get_int64(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_int64(tag, offset); - } + public static Int64 plc_tag_get_int64(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_int64(tag, offset); + } - public static int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val) - { - return NativeMethods.plc_tag_set_uint64(tag, offset, val); - } + public static int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val) + { + return NativeMethods.plc_tag_set_uint64(tag, offset, val); + } - public static int plc_tag_set_int64(Int32 tag, int offset, Int64 val) - { - return NativeMethods.plc_tag_set_int64(tag, offset, val); - } + public static int plc_tag_set_int64(Int32 tag, int offset, Int64 val) + { + return NativeMethods.plc_tag_set_int64(tag, offset, val); + } - public static double plc_tag_get_float64(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_float64(tag, offset); - } + public static double plc_tag_get_float64(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_float64(tag, offset); + } - public static int plc_tag_set_float64(Int32 tag, int offset, double val) - { - return NativeMethods.plc_tag_set_float64(tag, offset, val); - } - public static UInt32 plc_tag_get_uint32(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_uint32(tag, offset); - } + public static int plc_tag_set_float64(Int32 tag, int offset, double val) + { + return NativeMethods.plc_tag_set_float64(tag, offset, val); + } + public static UInt32 plc_tag_get_uint32(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_uint32(tag, offset); + } - public static Int32 plc_tag_get_int32(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_int32(tag, offset); - } + public static Int32 plc_tag_get_int32(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_int32(tag, offset); + } - public static int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val) - { - return NativeMethods.plc_tag_set_uint32(tag, offset, val); - } + public static int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val) + { + return NativeMethods.plc_tag_set_uint32(tag, offset, val); + } - public static int plc_tag_set_int32(Int32 tag, int offset, Int32 val) - { - return NativeMethods.plc_tag_set_int32(tag, offset, val); - } + public static int plc_tag_set_int32(Int32 tag, int offset, Int32 val) + { + return NativeMethods.plc_tag_set_int32(tag, offset, val); + } - public static float plc_tag_get_float32(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_float32(tag, offset); - } + public static float plc_tag_get_float32(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_float32(tag, offset); + } - public static int plc_tag_set_float32(Int32 tag, int offset, float val) - { - return NativeMethods.plc_tag_set_float32(tag, offset, val); - } + public static int plc_tag_set_float32(Int32 tag, int offset, float val) + { + return NativeMethods.plc_tag_set_float32(tag, offset, val); + } - public static UInt16 plc_tag_get_uint16(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_uint16(tag, offset); - } + public static UInt16 plc_tag_get_uint16(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_uint16(tag, offset); + } - public static Int16 plc_tag_get_int16(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_int16(tag, offset); - } + public static Int16 plc_tag_get_int16(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_int16(tag, offset); + } - public static int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val) - { - return NativeMethods.plc_tag_set_uint16(tag, offset, val); - } + public static int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val) + { + return NativeMethods.plc_tag_set_uint16(tag, offset, val); + } - public static int plc_tag_set_int16(Int32 tag, int offset, Int16 val) - { - return NativeMethods.plc_tag_set_int16(tag, offset, val); - } + public static int plc_tag_set_int16(Int32 tag, int offset, Int16 val) + { + return NativeMethods.plc_tag_set_int16(tag, offset, val); + } - public static byte plc_tag_get_uint8(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_uint8(tag, offset); - } + public static byte plc_tag_get_uint8(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_uint8(tag, offset); + } - public static sbyte plc_tag_get_int8(Int32 tag, int offset) - { - return NativeMethods.plc_tag_get_int8(tag, offset); - } + public static sbyte plc_tag_get_int8(Int32 tag, int offset) + { + return NativeMethods.plc_tag_get_int8(tag, offset); + } - public static int plc_tag_set_uint8(Int32 tag, int offset, byte val) - { - return NativeMethods.plc_tag_set_uint8(tag, offset, val); - } + public static int plc_tag_set_uint8(Int32 tag, int offset, byte val) + { + return NativeMethods.plc_tag_set_uint8(tag, offset, val); + } - public static int plc_tag_set_int8(Int32 tag, int offset, sbyte val) - { - return NativeMethods.plc_tag_set_int8(tag, offset, val); - } + public static int plc_tag_set_int8(Int32 tag, int offset, sbyte val) + { + return NativeMethods.plc_tag_set_int8(tag, offset, val); + } - public static int plc_tag_get_bit(Int32 tag, int offset_bit) - { - return NativeMethods.plc_tag_get_bit(tag, offset_bit); - } + public static int plc_tag_get_bit(Int32 tag, int offset_bit) + { + return NativeMethods.plc_tag_get_bit(tag, offset_bit); + } - public static int plc_tag_set_bit(Int32 tag, int offset_bit, int val) - { - return NativeMethods.plc_tag_set_bit(tag, offset_bit, val); - } - public static void plc_tag_set_debug_level(int debug_level) - { - NativeMethods.plc_tag_set_debug_level(debug_level); - } + public static int plc_tag_set_bit(Int32 tag, int offset_bit, int val) + { + return NativeMethods.plc_tag_set_bit(tag, offset_bit, val); + } + public static void plc_tag_set_debug_level(int debug_level) + { + NativeMethods.plc_tag_set_debug_level(debug_level); + } - public static int plc_tag_get_debug_level() - { - return NativeMethods.plc_tag_get_debug_level(); - } + public static int plc_tag_get_debug_level() + { + return NativeMethods.plc_tag_get_debug_level(); + } - public static int plc_tag_set_debug_module_level(int module, int debug_level) - { - return NativeMethods.plc_tag_set_debug_module_level(module, debug_level); - } + public static int plc_tag_set_debug_module_level(int module, int debug_level) + { + return NativeMethods.plc_tag_set_debug_module_level(module, debug_level); + } - public static int plc_tag_get_debug_module_level(int module) - { - return NativeMethods.plc_tag_get_debug_module_level(module); - } + public static int plc_tag_get_debug_module_level(int module) + { + return NativeMethods.plc_tag_get_debug_module_level(module); + } - public static int plc_tag_get_string(Int32 tag_id, int string_start_offset, StringBuilder buffer, int buffer_length) - { - return NativeMethods.plc_tag_get_string(tag_id, string_start_offset, buffer, buffer_length); - } + public static int plc_tag_get_string(Int32 tag_id, int string_start_offset, StringBuilder buffer, int buffer_length) + { + return NativeMethods.plc_tag_get_string(tag_id, string_start_offset, buffer, buffer_length); + } - public static int plc_tag_set_string(Int32 tag_id, int string_start_offset, string string_val) - { - return NativeMethods.plc_tag_set_string(tag_id, string_start_offset, string_val); - } + public static int plc_tag_set_string(Int32 tag_id, int string_start_offset, string string_val) + { + return NativeMethods.plc_tag_set_string(tag_id, string_start_offset, string_val); + } - public static int plc_tag_get_string_length(Int32 tag_id, int string_start_offset) - { - return NativeMethods.plc_tag_get_string_length(tag_id, string_start_offset); - } + public static int plc_tag_get_string_length(Int32 tag_id, int string_start_offset) + { + return NativeMethods.plc_tag_get_string_length(tag_id, string_start_offset); + } - public static int plc_tag_get_string_capacity(Int32 tag_id, int string_start_offset) - { - return NativeMethods.plc_tag_get_string_capacity(tag_id, string_start_offset); - } + public static int plc_tag_get_string_capacity(Int32 tag_id, int string_start_offset) + { + return NativeMethods.plc_tag_get_string_capacity(tag_id, string_start_offset); + } - public static int plc_tag_get_string_total_length(Int32 tag_id, int string_start_offset) - { - return NativeMethods.plc_tag_get_string_total_length(tag_id, string_start_offset); - } + public static int plc_tag_get_string_total_length(Int32 tag_id, int string_start_offset) + { + return NativeMethods.plc_tag_get_string_total_length(tag_id, string_start_offset); + } - public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length) - { - return plc_tag_get_raw_bytes(tag_id, start_offset, buffer.AsSpan(0, buffer_length)); - } + public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length) + { + return plc_tag_get_raw_bytes(tag_id, start_offset, buffer.AsSpan(0, buffer_length)); + } - public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, Span buffer) + public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, Span buffer) + { + unsafe { - unsafe + fixed (byte* ptr = buffer) { - fixed (byte* ptr = buffer) - { - return NativeMethods.plc_tag_get_raw_bytes(tag_id, start_offset, ptr, buffer.Length); - } + return NativeMethods.plc_tag_get_raw_bytes(tag_id, start_offset, ptr, buffer.Length); } } + } - public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length) - { - return plc_tag_set_raw_bytes(tag_id, start_offset, new ReadOnlySpan(buffer, 0, buffer_length)); - } + public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length) + { + return plc_tag_set_raw_bytes(tag_id, start_offset, new ReadOnlySpan(buffer, 0, buffer_length)); + } - public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, ReadOnlySpan buffer) + public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, ReadOnlySpan buffer) + { + unsafe { - unsafe + fixed (byte* ptr = buffer) { - fixed (byte* ptr = buffer) - { - return NativeMethods.plc_tag_set_raw_bytes(tag_id, start_offset, ptr, buffer.Length); - } + return NativeMethods.plc_tag_set_raw_bytes(tag_id, start_offset, ptr, buffer.Length); } } + } - } -} +} \ No newline at end of file diff --git a/src/libplctag.Tests/AsyncTests.cs b/src/libplctag.Tests/AsyncTests.cs index 028f6a15..8fe0e19f 100644 --- a/src/libplctag.Tests/AsyncTests.cs +++ b/src/libplctag.Tests/AsyncTests.cs @@ -11,150 +11,149 @@ using System.Threading; using System.Threading.Tasks; -namespace libplctag.Tests +namespace libplctag.Tests; + +public class AsyncTests { - public class AsyncTests + + readonly TimeSpan REALISTIC_LATENCY_FOR_CREATE = TimeSpan.FromMilliseconds(50); + readonly TimeSpan REALISTIC_LATENCY_FOR_READ = TimeSpan.FromMilliseconds(50); + readonly TimeSpan REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS = TimeSpan.FromMilliseconds(1000); + + [Fact] + public async Task Cancelled_cancellation_token_throws_a_TaskCanceledException() { + // Arrange + var nativeTag = new Mock(); - readonly TimeSpan REALISTIC_LATENCY_FOR_CREATE = TimeSpan.FromMilliseconds(50); - readonly TimeSpan REALISTIC_LATENCY_FOR_READ = TimeSpan.FromMilliseconds(50); - readonly TimeSpan REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS = TimeSpan.FromMilliseconds(1000); + nativeTag // The initial creation of the tag object returns a status, so we return pending + .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) + .Returns((int)Status.Pending); - [Fact] - public async Task Cancelled_cancellation_token_throws_a_TaskCanceledException() - { - // Arrange - var nativeTag = new Mock(); + nativeTag // Subsequent calls to determine the tag status should still return pending + .Setup(m => m.plc_tag_status(It.IsAny())) + .Returns((int)Status.Pending); - nativeTag // The initial creation of the tag object returns a status, so we return pending - .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) - .Returns((int)Status.Pending); + var tag = new Tag(nativeTag.Object); + var cts = new CancellationTokenSource(); - nativeTag // Subsequent calls to determine the tag status should still return pending - .Setup(m => m.plc_tag_status(It.IsAny())) - .Returns((int)Status.Pending); + // Act, Assert + cts.CancelAfter(REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS); + await Assert.ThrowsAsync(async () => { + await tag.InitializeAsync(cts.Token); + }); + } - var tag = new Tag(nativeTag.Object); - var cts = new CancellationTokenSource(); - // Act, Assert - cts.CancelAfter(REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS); - await Assert.ThrowsAsync(async () => { - await tag.InitializeAsync(cts.Token); - }); - } + [Fact] + public async Task Timeout_throws_a_LibPlcTagException() + { + // Arrange + var nativeTag = new Mock(); + + nativeTag // The initial creation of the tag object returns a status, so we return pending + .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) + .Returns((int)Status.Pending); + nativeTag // Subsequent calls to determine the tag status should still return pending + .Setup(m => m.plc_tag_status(It.IsAny())) + .Returns((int)Status.Pending); - [Fact] - public async Task Timeout_throws_a_LibPlcTagException() + var tag = new Tag(nativeTag.Object) { - // Arrange - var nativeTag = new Mock(); + Timeout = REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS + }; - nativeTag // The initial creation of the tag object returns a status, so we return pending - .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) - .Returns((int)Status.Pending); + // Act + var ex = await Assert.ThrowsAsync(async () => { + await tag.InitializeAsync(); + }); - nativeTag // Subsequent calls to determine the tag status should still return pending - .Setup(m => m.plc_tag_status(It.IsAny())) - .Returns((int)Status.Pending); + // Assert + Assert.Equal(Status.ErrorTimeout, ex.Status); + } - var tag = new Tag(nativeTag.Object) - { - Timeout = REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS - }; + [Fact] + public async Task Timeout_returns_pending_but_eventually_ok() + { + // Arrange + var nativeTag = GetMock(); - // Act - var ex = await Assert.ThrowsAsync(async () => { - await tag.InitializeAsync(); - }); + var tag = new Tag(nativeTag.Object) + { + Timeout = REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS + }; + + // Act + var task = tag.InitializeAsync(); + var statusWhileWaiting = tag.GetStatus(); + await task; + var statusAfterAwaited = tag.GetStatus(); + + // Assert + Assert.Equal(Status.Pending, statusWhileWaiting); + Assert.Equal(Status.Ok, statusAfterAwaited); + } - // Assert - Assert.Equal(Status.ErrorTimeout, ex.Status); - } + [Fact] + public async Task AsyncRead_completes_within_timeout_period() + { + // Arrange + var nativeTag = GetMock(); - [Fact] - public async Task Timeout_returns_pending_but_eventually_ok() + var tag = new Tag(nativeTag.Object) { - // Arrange - var nativeTag = GetMock(); + Timeout = REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS + }; - var tag = new Tag(nativeTag.Object) - { - Timeout = REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS - }; - - // Act - var task = tag.InitializeAsync(); - var statusWhileWaiting = tag.GetStatus(); - await task; - var statusAfterAwaited = tag.GetStatus(); - - // Assert - Assert.Equal(Status.Pending, statusWhileWaiting); - Assert.Equal(Status.Ok, statusAfterAwaited); - } - - [Fact] - public async Task AsyncRead_completes_within_timeout_period() - { - // Arrange - var nativeTag = GetMock(); + // Act + await tag.ReadAsync(); - var tag = new Tag(nativeTag.Object) - { - Timeout = REALISTIC_TIMEOUT_FOR_ALL_OPERATIONS - }; + // Assert + Assert.Equal(Status.Ok, tag.GetStatus()); + } - // Act - await tag.ReadAsync(); - // Assert - Assert.Equal(Status.Ok, tag.GetStatus()); - } + Mock GetMock() + { + const int tagId = 11; + NativeImport.plctag.callback_func_ex callback = null; + Status? status = null; - Mock GetMock() - { - const int tagId = 11; - - NativeImport.plctag.callback_func_ex callback = null; - Status? status = null; - - var nativeTag = new Mock(); - - // The NativeTagWrapper should provide the native tag with a callback. - // We will store this locally when a create call occurs, and fire it shortly after ... - nativeTag - .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) - .Callback(async (attributeString, callbackFunc, userData, timeout) => - { - status = Status.Pending; - callback = callbackFunc; - await Task.Delay(REALISTIC_LATENCY_FOR_CREATE); - status = Status.Ok; - callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_CREATED, (int)NativeImport.STATUS.PLCTAG_STATUS_OK, IntPtr.Zero); - }); - - // ... as well as when a read call occurs - nativeTag - .Setup(m => m.plc_tag_read(It.IsAny(), 0)) - .Callback(async (tagId, timeout) => - { - status = Status.Pending; - callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_READ_STARTED, (int)NativeImport.STATUS.PLCTAG_STATUS_OK, IntPtr.Zero); - await Task.Delay(REALISTIC_LATENCY_FOR_READ); - status = Status.Ok; - callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_READ_COMPLETED, (int)NativeImport.STATUS.PLCTAG_STATUS_OK, IntPtr.Zero); - }); - - // the status was being tracked, so return it if asked - nativeTag - .Setup(m => m.plc_tag_status(It.IsAny())) - .Returns(() => (int)status.Value); - - return nativeTag; - } + var nativeTag = new Mock(); + + // The NativeTagWrapper should provide the native tag with a callback. + // We will store this locally when a create call occurs, and fire it shortly after ... + nativeTag + .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) + .Callback(async (attributeString, callbackFunc, userData, timeout) => + { + status = Status.Pending; + callback = callbackFunc; + await Task.Delay(REALISTIC_LATENCY_FOR_CREATE); + status = Status.Ok; + callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_CREATED, (int)NativeImport.STATUS.PLCTAG_STATUS_OK, IntPtr.Zero); + }); + // ... as well as when a read call occurs + nativeTag + .Setup(m => m.plc_tag_read(It.IsAny(), 0)) + .Callback(async (tagId, timeout) => + { + status = Status.Pending; + callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_READ_STARTED, (int)NativeImport.STATUS.PLCTAG_STATUS_OK, IntPtr.Zero); + await Task.Delay(REALISTIC_LATENCY_FOR_READ); + status = Status.Ok; + callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_READ_COMPLETED, (int)NativeImport.STATUS.PLCTAG_STATUS_OK, IntPtr.Zero); + }); + + // the status was being tracked, so return it if asked + nativeTag + .Setup(m => m.plc_tag_status(It.IsAny())) + .Returns(() => (int)status.Value); + + return nativeTag; } -} + +} \ No newline at end of file diff --git a/src/libplctag.Tests/DisposeTests.cs b/src/libplctag.Tests/DisposeTests.cs index 32532d2d..5c0d8b7c 100644 --- a/src/libplctag.Tests/DisposeTests.cs +++ b/src/libplctag.Tests/DisposeTests.cs @@ -11,82 +11,81 @@ using System.Threading.Tasks; using System.Threading; -namespace libplctag.Tests +namespace libplctag.Tests; + +public class DisposeTests { - public class DisposeTests + + [Fact] + public void Destroy_is_called_if_initialized_and_disposed() { + // Arrange + var nativeTag = new Mock(); + var tag = new Tag(nativeTag.Object); - [Fact] - public void Destroy_is_called_if_initialized_and_disposed() - { - // Arrange - var nativeTag = new Mock(); - var tag = new Tag(nativeTag.Object); + // Act + tag.Initialize(); + tag.Dispose(); - // Act - tag.Initialize(); - tag.Dispose(); + // Assert + nativeTag.Verify(m => m.plc_tag_destroy(It.IsAny()), Times.Once); + } - // Assert - nativeTag.Verify(m => m.plc_tag_destroy(It.IsAny()), Times.Once); - } + [Fact] + public async Task GithubIssue418() + { + // Arrange + const int tagId = 11; + + NativeImport.plctag.callback_func_ex callback = null; + Status? status = null; + + var nativeTag = new Mock(); - [Fact] - public async Task GithubIssue418() + nativeTag + .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) + .Callback(async (attributeString, callbackFunc, userData, timeout) => + { + status = Status.Pending; + callback = callbackFunc; + status = Status.ErrorNotFound; + callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_CREATED, (int)NativeImport.STATUS.PLCTAG_ERR_NOT_FOUND, IntPtr.Zero); + }); + + // the status was being tracked, so return it if asked + nativeTag + .Setup(m => m.plc_tag_status(It.IsAny())) + .Returns(() => (int)status.Value); + + // Act + using(var tag = new Tag(nativeTag.Object)) { - // Arrange - const int tagId = 11; - - NativeImport.plctag.callback_func_ex callback = null; - Status? status = null; - - var nativeTag = new Mock(); - - nativeTag - .Setup(m => m.plc_tag_create_ex(It.IsAny(), It.IsAny(), It.IsAny(), 0)) - .Callback(async (attributeString, callbackFunc, userData, timeout) => - { - status = Status.Pending; - callback = callbackFunc; - status = Status.ErrorNotFound; - callback?.Invoke(tagId, (int)NativeImport.EVENT.PLCTAG_EVENT_CREATED, (int)NativeImport.STATUS.PLCTAG_ERR_NOT_FOUND, IntPtr.Zero); - }); - - // the status was being tracked, so return it if asked - nativeTag - .Setup(m => m.plc_tag_status(It.IsAny())) - .Returns(() => (int)status.Value); - - // Act - using(var tag = new Tag(nativeTag.Object)) + try + { + await tag.InitializeAsync(); + } + catch (LibPlcTagException e) when (e.Status == Status.ErrorNotFound) // we are expecting this exception { - try - { - await tag.InitializeAsync(); - } - catch (LibPlcTagException e) when (e.Status == Status.ErrorNotFound) // we are expecting this exception - { - } } - - // Assert - LibPlcTag.Shutdown(); - } - [Fact] - public void Can_not_use_if_already_disposed() - { - // Arrange - var nativeTag = new Mock(); - var tag = new Tag(nativeTag.Object); + // Assert + LibPlcTag.Shutdown(); - // Act - tag.Dispose(); + } - // Assert - Assert.Throws(() => tag.GetStatus()); - } + [Fact] + public void Can_not_use_if_already_disposed() + { + // Arrange + var nativeTag = new Mock(); + var tag = new Tag(nativeTag.Object); + // Act + tag.Dispose(); + + // Assert + Assert.Throws(() => tag.GetStatus()); } -} + +} \ No newline at end of file diff --git a/src/libplctag.Tests/OtherTests.cs b/src/libplctag.Tests/OtherTests.cs index d1b908c5..3d1d12d7 100644 --- a/src/libplctag.Tests/OtherTests.cs +++ b/src/libplctag.Tests/OtherTests.cs @@ -9,64 +9,63 @@ using Xunit; using Moq; -namespace libplctag.Tests +namespace libplctag.Tests; + +public class OtherTests { - public class OtherTests - { - [Fact] - public void Status_ok_when_first_created() - { - // Arrange - var nativeTag = new Mock(); - var tag = new Tag(nativeTag.Object); + [Fact] + public void Status_ok_when_first_created() + { + // Arrange + var nativeTag = new Mock(); + var tag = new Tag(nativeTag.Object); - // Act + // Act - // Assert - var status = tag.GetStatus(); - Assert.Equal(Status.Ok, status); - } + // Assert + var status = tag.GetStatus(); + Assert.Equal(Status.Ok, status); + } - [Fact] - public void Attribute_string_formatted_correctly() + [Fact] + public void Attribute_string_formatted_correctly() + { + // Arrange + var nativeTag = new Mock(); + var tag = new Tag(nativeTag.Object) { - // Arrange - var nativeTag = new Mock(); - var tag = new Tag(nativeTag.Object) - { - ElementSize = 4, - ElementCount = 10, - PlcType = PlcType.Slc500, - Name = "TagName", - }; + ElementSize = 4, + ElementCount = 10, + PlcType = PlcType.Slc500, + Name = "TagName", + }; - // Act - tag.Initialize(); + // Act + tag.Initialize(); - // Assert - var expectedAttributeString = "plc=slc500&elem_size=4&elem_count=10&name=TagName"; - nativeTag.Verify(m => m.plc_tag_create_ex(expectedAttributeString, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + // Assert + var expectedAttributeString = "plc=slc500&elem_size=4&elem_count=10&name=TagName"; + nativeTag.Verify(m => m.plc_tag_create_ex(expectedAttributeString, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); - } + } - [Fact] - public void Attribute_string_does_not_contain_unset_properties() - { - // Arrange - var nativeTag = new Mock(); - var tag = new Tag(nativeTag.Object); + [Fact] + public void Attribute_string_does_not_contain_unset_properties() + { + // Arrange + var nativeTag = new Mock(); + var tag = new Tag(nativeTag.Object); - // Act - tag.Initialize(); + // Act + tag.Initialize(); - // Assert - var expectedAttributeString = ""; - nativeTag.Verify(m => m.plc_tag_create_ex(expectedAttributeString, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + // Assert + var expectedAttributeString = ""; + nativeTag.Verify(m => m.plc_tag_create_ex(expectedAttributeString, It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); - } } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/BoolPlcMapper.cs b/src/libplctag/DataTypes/BoolPlcMapper.cs index e57ba0b1..d06ab808 100644 --- a/src/libplctag/DataTypes/BoolPlcMapper.cs +++ b/src/libplctag/DataTypes/BoolPlcMapper.cs @@ -9,66 +9,64 @@ using System; using System.Linq; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class BoolPlcMapper : IPlcMapper, IPlcMapper, IPlcMapper, IPlcMapper { + public int? ElementSize => 1; - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class BoolPlcMapper : IPlcMapper, IPlcMapper, IPlcMapper, IPlcMapper - { - public int? ElementSize => 1; + public PlcType PlcType { get; set; } + public int[] ArrayDimensions { get; set; } - public PlcType PlcType { get; set; } - public int[] ArrayDimensions { get; set; } + public int? GetElementCount() + { + if (ArrayDimensions == null) + return null; - public int? GetElementCount() - { - if (ArrayDimensions == null) - return null; + //TODO: Test -> I'm not confident that the overall bool count is packed as a 1D array and not packed by dimension. + //Multiply dimensions for total elements + var totalElements = ArrayDimensions.Aggregate(1, (x, y) => x * y); + return (int)Math.Ceiling((double)totalElements / 32.0); + } - //TODO: Test -> I'm not confident that the overall bool count is packed as a 1D array and not packed by dimension. - //Multiply dimensions for total elements - var totalElements = ArrayDimensions.Aggregate(1, (x, y) => x * y); - return (int)Math.Ceiling((double)totalElements / 32.0); - } + public int? SetArrayLength(int? elementCount) => (int)Math.Ceiling((double)elementCount.Value / 32.0); - public int? SetArrayLength(int? elementCount) => (int)Math.Ceiling((double)elementCount.Value / 32.0); + virtual protected bool[] DecodeArray(Tag tag) + { + if (ElementSize is null) + throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array decoding"); - virtual protected bool[] DecodeArray(Tag tag) + var buffer = new bool[tag.ElementCount.Value * 32]; + for (int ii = 0; ii < tag.ElementCount.Value * 32; ii++) { - if (ElementSize is null) - throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array decoding"); - - var buffer = new bool[tag.ElementCount.Value * 32]; - for (int ii = 0; ii < tag.ElementCount.Value * 32; ii++) - { - buffer[ii] = tag.GetBit(ii); - } - return buffer; + buffer[ii] = tag.GetBit(ii); } + return buffer; + } - virtual protected void EncodeArray(Tag tag, bool[] values) + virtual protected void EncodeArray(Tag tag, bool[] values) + { + for (int ii = 0; ii < tag.ElementCount.Value * 32; ii++) { - for (int ii = 0; ii < tag.ElementCount.Value * 32; ii++) - { - tag.SetBit(ii, values[ii]); - } + tag.SetBit(ii, values[ii]); } + } - bool IPlcMapper.Decode(Tag tag) => tag.GetUInt8(0) != 0; + bool IPlcMapper.Decode(Tag tag) => tag.GetUInt8(0) != 0; - void IPlcMapper.Encode(Tag tag, bool value) => tag.SetUInt8(0, value == true ? (byte)255 : (byte)0); + void IPlcMapper.Encode(Tag tag, bool value) => tag.SetUInt8(0, value == true ? (byte)255 : (byte)0); - bool[] IPlcMapper.Decode(Tag tag) => DecodeArray(tag); + bool[] IPlcMapper.Decode(Tag tag) => DecodeArray(tag); - void IPlcMapper.Encode(Tag tag, bool[] value) => EncodeArray(tag, value); + void IPlcMapper.Encode(Tag tag, bool[] value) => EncodeArray(tag, value); - bool[,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To2DArray(ArrayDimensions[0], ArrayDimensions[1]); + bool[,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To2DArray(ArrayDimensions[0], ArrayDimensions[1]); - void IPlcMapper.Encode(Tag tag, bool[,] value) => EncodeArray(tag, value.To1DArray()); + void IPlcMapper.Encode(Tag tag, bool[,] value) => EncodeArray(tag, value.To1DArray()); - bool[,,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To3DArray(ArrayDimensions[0], ArrayDimensions[1], ArrayDimensions[2]); + bool[,,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To3DArray(ArrayDimensions[0], ArrayDimensions[1], ArrayDimensions[2]); - void IPlcMapper.Encode(Tag tag, bool[,,] value) => EncodeArray(tag, value.To1DArray()); + void IPlcMapper.Encode(Tag tag, bool[,,] value) => EncodeArray(tag, value.To1DArray()); - } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/DintPlcMapper.cs b/src/libplctag/DataTypes/DintPlcMapper.cs index 48e7a7e0..df4547bb 100644 --- a/src/libplctag/DataTypes/DintPlcMapper.cs +++ b/src/libplctag/DataTypes/DintPlcMapper.cs @@ -7,16 +7,15 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class DintPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class DintPlcMapper : PlcMapperBase - { - public override int? ElementSize => 4; + public override int? ElementSize => 4; - override public int Decode(Tag tag, int offset) => tag.GetInt32(offset); + override public int Decode(Tag tag, int offset) => tag.GetInt32(offset); - override public void Encode(Tag tag, int offset, int value) => tag.SetInt32(offset, value); + override public void Encode(Tag tag, int offset, int value) => tag.SetInt32(offset, value); - } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/Extensions/ArrayExtensions.cs b/src/libplctag/DataTypes/Extensions/ArrayExtensions.cs index 74fc0005..68eb5cfa 100644 --- a/src/libplctag/DataTypes/Extensions/ArrayExtensions.cs +++ b/src/libplctag/DataTypes/Extensions/ArrayExtensions.cs @@ -9,111 +9,110 @@ using System.Collections.Generic; using System.Text; -namespace libplctag.DataTypes.Extensions +namespace libplctag.DataTypes.Extensions; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public static class ArrayExtensions { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public static class ArrayExtensions + /// + /// Extension method to flatten a 2D array to a 1D array + /// + /// Array Type + /// 2D array to be flattened + /// 1D array + public static T[] To1DArray(this T[,] input) { - /// - /// Extension method to flatten a 2D array to a 1D array - /// - /// Array Type - /// 2D array to be flattened - /// 1D array - public static T[] To1DArray(this T[,] input) - { - // Step 1: get total size of 2D array, and allocate 1D array. - int size = input.Length; - T[] result = new T[size]; + // Step 1: get total size of 2D array, and allocate 1D array. + int size = input.Length; + T[] result = new T[size]; - // Step 2: copy 2D array elements into a 1D array. - int write = 0; - for (int i = 0; i <= input.GetUpperBound(0); i++) + // Step 2: copy 2D array elements into a 1D array. + int write = 0; + for (int i = 0; i <= input.GetUpperBound(0); i++) + { + for (int z = 0; z <= input.GetUpperBound(1); z++) { - for (int z = 0; z <= input.GetUpperBound(1); z++) - { - result[write++] = input[i, z]; - } + result[write++] = input[i, z]; } - // Step 3: return the new array. - return result; } + // Step 3: return the new array. + return result; + } - /// - /// Extension method to flatten a 3D array to a 1D array - /// - /// Array Type - /// 3D array to be flattened - /// 1D array - public static T[] To1DArray(this T[,,] input) - { - // Step 1: get total size of 3D array, and allocate 1D array. - int size = input.Length; - T[] result = new T[size]; + /// + /// Extension method to flatten a 3D array to a 1D array + /// + /// Array Type + /// 3D array to be flattened + /// 1D array + public static T[] To1DArray(this T[,,] input) + { + // Step 1: get total size of 3D array, and allocate 1D array. + int size = input.Length; + T[] result = new T[size]; - // Step 2: copy 3D array elements into a 1D array. - int write = 0; - for (int i = 0; i <= input.GetUpperBound(0); i++) + // Step 2: copy 3D array elements into a 1D array. + int write = 0; + for (int i = 0; i <= input.GetUpperBound(0); i++) + { + for (int j = 0; j <= input.GetUpperBound(1); j++) { - for (int j = 0; j <= input.GetUpperBound(1); j++) + for (int k = 0; k <= input.GetUpperBound(2); k++) { - for (int k = 0; k <= input.GetUpperBound(2); k++) - { - result[write++] = input[i, j, k]; - } + result[write++] = input[i, j, k]; } } - // Step 3: return the new array. - return result; } + // Step 3: return the new array. + return result; + } - /// - /// Extension method to reshape a 1D array into a 2D array - /// - /// Array Type - /// 1D array to be reshaped - /// Desired height (first index) of 2D array - /// Desired width (second index) of 2D array - /// 2D array - public static T[,] To2DArray(this T[] input, int height, int width) - { - T[,] output = new T[height, width]; + /// + /// Extension method to reshape a 1D array into a 2D array + /// + /// Array Type + /// 1D array to be reshaped + /// Desired height (first index) of 2D array + /// Desired width (second index) of 2D array + /// 2D array + public static T[,] To2DArray(this T[] input, int height, int width) + { + T[,] output = new T[height, width]; - for (int i = 0; i < height; i++) + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) { - for (int j = 0; j < width; j++) - { - output[i, j] = input[i * width + j]; - } + output[i, j] = input[i * width + j]; } - return output; } + return output; + } - /// - /// Extension method to reshape a 1D array into a 3D array - /// - /// Array Type - /// 1D array to be reshaped - /// Desired height (first index) of 3D array - /// Desired width (second index) of 3D array - /// Desired length (third index) of 3D array - /// #D array - public static T[,,] To3DArray(this T[] input, int height, int width, int length) - { - T[,,] output = new T[height, width, length]; + /// + /// Extension method to reshape a 1D array into a 3D array + /// + /// Array Type + /// 1D array to be reshaped + /// Desired height (first index) of 3D array + /// Desired width (second index) of 3D array + /// Desired length (third index) of 3D array + /// #D array + public static T[,,] To3DArray(this T[] input, int height, int width, int length) + { + T[,,] output = new T[height, width, length]; - for (int i = 0; i < height; i++) + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) { - for (int j = 0; j < width; j++) + for (int k = 0; k < length; k++) { - for (int k = 0; k < length; k++) - { - output[i, j, k] = input[i * width * length + j * length + k]; - } + output[i, j, k] = input[i * width * length + j * length + k]; } } - return output; } - + return output; } -} + +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/IPlcMapper.cs b/src/libplctag/DataTypes/IPlcMapper.cs index 40e4e04f..a4566868 100644 --- a/src/libplctag/DataTypes/IPlcMapper.cs +++ b/src/libplctag/DataTypes/IPlcMapper.cs @@ -7,51 +7,50 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public interface IPlcMapper { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public interface IPlcMapper - { - /// - /// You can define different marshalling behaviour for different types - /// The PlcType is injected during PlcMapper instantiation, and - /// will be available to you in your marshalling logic - /// - PlcType PlcType { get; set; } - - - /// - /// Provide an integer value for ElementSize if you - /// want to pass this into the tag constructor - /// - int? ElementSize { get; } - - /// - /// The dimensions of the array. Null if not an array. - /// - int[] ArrayDimensions { get; set; } - - /// - /// This is used to convert the number of array elements - /// into the raw element count, which is used by the library. - /// Most of the time, this will be the dimensions multiplied, but occasionally - /// it is not (e.g. BOOL arrays). - /// - int? GetElementCount(); - - /// - /// This is the method that reads/unpacks the underlying value of the tag - /// and returns it as a C# type - /// - /// Tag to be Decoded - /// C# value of tag - T Decode(Tag tag); - - /// - /// This is the method that transforms the C# type into the underlying value of the tag - /// - /// Tag to be encoded to - /// C# value to be transformed - void Encode(Tag tag, T value); - } + /// + /// You can define different marshalling behaviour for different types + /// The PlcType is injected during PlcMapper instantiation, and + /// will be available to you in your marshalling logic + /// + PlcType PlcType { get; set; } + + + /// + /// Provide an integer value for ElementSize if you + /// want to pass this into the tag constructor + /// + int? ElementSize { get; } + + /// + /// The dimensions of the array. Null if not an array. + /// + int[] ArrayDimensions { get; set; } + + /// + /// This is used to convert the number of array elements + /// into the raw element count, which is used by the library. + /// Most of the time, this will be the dimensions multiplied, but occasionally + /// it is not (e.g. BOOL arrays). + /// + int? GetElementCount(); + + /// + /// This is the method that reads/unpacks the underlying value of the tag + /// and returns it as a C# type + /// + /// Tag to be Decoded + /// C# value of tag + T Decode(Tag tag); + + /// + /// This is the method that transforms the C# type into the underlying value of the tag + /// + /// Tag to be encoded to + /// C# value to be transformed + void Encode(Tag tag, T value); } \ No newline at end of file diff --git a/src/libplctag/DataTypes/IntPlcMapper.cs b/src/libplctag/DataTypes/IntPlcMapper.cs index 45256803..b6931d88 100644 --- a/src/libplctag/DataTypes/IntPlcMapper.cs +++ b/src/libplctag/DataTypes/IntPlcMapper.cs @@ -7,16 +7,15 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class IntPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class IntPlcMapper : PlcMapperBase - { - public override int? ElementSize => 2; + public override int? ElementSize => 2; - override public short Decode(Tag tag, int offset) => tag.GetInt16(offset); + override public short Decode(Tag tag, int offset) => tag.GetInt16(offset); - override public void Encode(Tag tag, int offset, short value) => tag.SetInt16(offset, value); + override public void Encode(Tag tag, int offset, short value) => tag.SetInt16(offset, value); - } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/LintPlcMapper.cs b/src/libplctag/DataTypes/LintPlcMapper.cs index 49643ed2..2d945e96 100644 --- a/src/libplctag/DataTypes/LintPlcMapper.cs +++ b/src/libplctag/DataTypes/LintPlcMapper.cs @@ -7,16 +7,15 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class LintPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class LintPlcMapper : PlcMapperBase - { - public override int? ElementSize => 8; + public override int? ElementSize => 8; - override public long Decode(Tag tag, int offset) => tag.GetInt64(offset); + override public long Decode(Tag tag, int offset) => tag.GetInt64(offset); - override public void Encode(Tag tag, int offset, long value) => tag.SetInt64(offset, value); + override public void Encode(Tag tag, int offset, long value) => tag.SetInt64(offset, value); - } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/LrealPlcMapper.cs b/src/libplctag/DataTypes/LrealPlcMapper.cs index cb970210..1b1c92b3 100644 --- a/src/libplctag/DataTypes/LrealPlcMapper.cs +++ b/src/libplctag/DataTypes/LrealPlcMapper.cs @@ -7,16 +7,15 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class LrealPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class LrealPlcMapper : PlcMapperBase - { - override public int? ElementSize => 8; + override public int? ElementSize => 8; - override public double Decode(Tag tag, int offset) => tag.GetFloat64(offset); + override public double Decode(Tag tag, int offset) => tag.GetFloat64(offset); - override public void Encode(Tag tag, int offset, double value)=> tag.SetFloat64(offset, value); - } -} + override public void Encode(Tag tag, int offset, double value)=> tag.SetFloat64(offset, value); +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/PlcMapperBase.cs b/src/libplctag/DataTypes/PlcMapperBase.cs index ea210a8e..8db78b2e 100644 --- a/src/libplctag/DataTypes/PlcMapperBase.cs +++ b/src/libplctag/DataTypes/PlcMapperBase.cs @@ -11,75 +11,73 @@ using System.Threading.Tasks; using libplctag.DataTypes.Extensions; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public abstract class PlcMapperBase : IPlcMapper, IPlcMapper, IPlcMapper, IPlcMapper { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public abstract class PlcMapperBase : IPlcMapper, IPlcMapper, IPlcMapper, IPlcMapper - { - public PlcType PlcType { get; set; } + public PlcType PlcType { get; set; } - abstract public int? ElementSize { get; } + abstract public int? ElementSize { get; } - public int[] ArrayDimensions { get; set; } + public int[] ArrayDimensions { get; set; } - //Multiply all the dimensions to get total elements - virtual public int? GetElementCount() => ArrayDimensions?.Aggregate(1, (x, y) => x * y); + //Multiply all the dimensions to get total elements + virtual public int? GetElementCount() => ArrayDimensions?.Aggregate(1, (x, y) => x * y); - virtual protected T[] DecodeArray(Tag tag) - { - if (ElementSize is null) - throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array decoding"); + virtual protected T[] DecodeArray(Tag tag) + { + if (ElementSize is null) + throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array decoding"); - var buffer = new List(); + var buffer = new List(); - var tagSize = tag.GetSize(); + var tagSize = tag.GetSize(); - int offset = 0; - while (offset < tagSize) - { - buffer.Add(Decode(tag, offset)); - offset += ElementSize.Value; - } + int offset = 0; + while (offset < tagSize) + { + buffer.Add(Decode(tag, offset)); + offset += ElementSize.Value; + } - return buffer.ToArray(); + return buffer.ToArray(); - } + } - virtual protected void EncodeArray(Tag tag, T[] values) + virtual protected void EncodeArray(Tag tag, T[] values) + { + if (ElementSize is null) { - if (ElementSize is null) - { - throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array encoding"); - } - - int offset = 0; - foreach (var item in values) - { - Encode(tag, offset, item); - offset += ElementSize.Value; - } + throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array encoding"); } - virtual public T Decode(Tag tag) => Decode(tag, 0); - public abstract T Decode(Tag tag, int offset); + int offset = 0; + foreach (var item in values) + { + Encode(tag, offset, item); + offset += ElementSize.Value; + } + } + virtual public T Decode(Tag tag) => Decode(tag, 0); + public abstract T Decode(Tag tag, int offset); - virtual public void Encode(Tag tag, T value) => Encode(tag, 0, value); - public abstract void Encode(Tag tag, int offset, T value); - virtual public void Encode(Tag tag, T[] value) => EncodeArray(tag, value); + virtual public void Encode(Tag tag, T value) => Encode(tag, 0, value); + public abstract void Encode(Tag tag, int offset, T value); - T[] IPlcMapper.Decode(Tag tag) => DecodeArray(tag); + virtual public void Encode(Tag tag, T[] value) => EncodeArray(tag, value); + T[] IPlcMapper.Decode(Tag tag) => DecodeArray(tag); - T[,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To2DArray(ArrayDimensions[0], ArrayDimensions[1]); - void IPlcMapper.Encode(Tag tag, T[,] value) => EncodeArray(tag, value.To1DArray()); + T[,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To2DArray(ArrayDimensions[0], ArrayDimensions[1]); - T[,,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To3DArray(ArrayDimensions[0], ArrayDimensions[1], ArrayDimensions[2]); + void IPlcMapper.Encode(Tag tag, T[,] value) => EncodeArray(tag, value.To1DArray()); - void IPlcMapper.Encode(Tag tag, T[,,] value) => EncodeArray(tag, value.To1DArray()); - } + T[,,] IPlcMapper.Decode(Tag tag) => DecodeArray(tag).To3DArray(ArrayDimensions[0], ArrayDimensions[1], ArrayDimensions[2]); -} + void IPlcMapper.Encode(Tag tag, T[,,] value) => EncodeArray(tag, value.To1DArray()); +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/RealPlcMapper.cs b/src/libplctag/DataTypes/RealPlcMapper.cs index fe770019..74ef1f13 100644 --- a/src/libplctag/DataTypes/RealPlcMapper.cs +++ b/src/libplctag/DataTypes/RealPlcMapper.cs @@ -7,17 +7,16 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class RealPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class RealPlcMapper : PlcMapperBase - { - override public int? ElementSize => 4; + override public int? ElementSize => 4; - override public float Decode(Tag tag, int offset) => tag.GetFloat32(offset); + override public float Decode(Tag tag, int offset) => tag.GetFloat32(offset); - override public void Encode(Tag tag, int offset, float value) => tag.SetFloat32(offset, value); + override public void Encode(Tag tag, int offset, float value) => tag.SetFloat32(offset, value); - } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/Simple/Definitions.cs b/src/libplctag/DataTypes/Simple/Definitions.cs index 87426efb..709582b6 100644 --- a/src/libplctag/DataTypes/Simple/Definitions.cs +++ b/src/libplctag/DataTypes/Simple/Definitions.cs @@ -9,8 +9,7 @@ using System.Collections.Generic; using System.Text; -namespace libplctag.DataTypes.Simple -{ +namespace libplctag.DataTypes.Simple; [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] public class TagBool : Tag { } @@ -99,6 +98,4 @@ public class TagTagInfo : Tag { } public class TagTimer : Tag { } [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class TagUdtInfo : Tag { } - -} + public class TagUdtInfo : Tag { } \ No newline at end of file diff --git a/src/libplctag/DataTypes/SintPlcMapper.cs b/src/libplctag/DataTypes/SintPlcMapper.cs index ba0a99ef..2508974c 100644 --- a/src/libplctag/DataTypes/SintPlcMapper.cs +++ b/src/libplctag/DataTypes/SintPlcMapper.cs @@ -7,17 +7,16 @@ using System; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class SintPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class SintPlcMapper : PlcMapperBase - { - override public int? ElementSize => 1; + override public int? ElementSize => 1; - override public sbyte Decode(Tag tag, int offset) => tag.GetInt8(offset); + override public sbyte Decode(Tag tag, int offset) => tag.GetInt8(offset); - override public void Encode(Tag tag, int offset, sbyte value) => tag.SetInt8(offset, value); + override public void Encode(Tag tag, int offset, sbyte value) => tag.SetInt8(offset, value); - } -} +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/StringPlcMapper.cs b/src/libplctag/DataTypes/StringPlcMapper.cs index b5da622f..9f5046d1 100644 --- a/src/libplctag/DataTypes/StringPlcMapper.cs +++ b/src/libplctag/DataTypes/StringPlcMapper.cs @@ -9,32 +9,31 @@ using System.Collections.Generic; using System.Text; -namespace libplctag.DataTypes +namespace libplctag.DataTypes; + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class StringPlcMapper : PlcMapperBase { - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class StringPlcMapper : PlcMapperBase - { - override public int? ElementSize + override public int? ElementSize + { + get { - get + switch (PlcType) { - switch (PlcType) - { - case PlcType.ControlLogix: return 88; - case PlcType.Plc5: return 84; - case PlcType.Slc500: return 84; - case PlcType.LogixPccc: return 84; - case PlcType.Micro800: return 256; //To be Confirmed - case PlcType.MicroLogix: return 84; - default: throw new NotImplementedException(); - } + case PlcType.ControlLogix: return 88; + case PlcType.Plc5: return 84; + case PlcType.Slc500: return 84; + case PlcType.LogixPccc: return 84; + case PlcType.Micro800: return 256; //To be Confirmed + case PlcType.MicroLogix: return 84; + default: throw new NotImplementedException(); } } + } - override public string Decode(Tag tag, int offset) => tag.GetString(offset); - override public void Encode(Tag tag, int offset, string value) => tag.SetString(offset, value); + override public string Decode(Tag tag, int offset) => tag.GetString(offset); + override public void Encode(Tag tag, int offset, string value) => tag.SetString(offset, value); - } } \ No newline at end of file diff --git a/src/libplctag/DataTypes/TagInfoPlcMapper.cs b/src/libplctag/DataTypes/TagInfoPlcMapper.cs index c26049b7..d994acfd 100644 --- a/src/libplctag/DataTypes/TagInfoPlcMapper.cs +++ b/src/libplctag/DataTypes/TagInfoPlcMapper.cs @@ -10,8 +10,7 @@ using System.Linq; using System.Text; -namespace libplctag.DataTypes -{ +namespace libplctag.DataTypes; [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] public class TagInfo @@ -97,6 +96,4 @@ public void Encode(Tag tag, TagInfo[] value) //TODO: We know this value after we decode once. SHould we trigger a decode or cache the value after first decode? return null; } - } - -} + } \ No newline at end of file diff --git a/src/libplctag/DataTypes/TimerPlcMapper.cs b/src/libplctag/DataTypes/TimerPlcMapper.cs index 46547ea3..0a0ca4fa 100644 --- a/src/libplctag/DataTypes/TimerPlcMapper.cs +++ b/src/libplctag/DataTypes/TimerPlcMapper.cs @@ -10,75 +10,74 @@ using System.Collections.Generic; using System.Text; -namespace libplctag.DataTypes -{ - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class TimerPlcMapper : PlcMapperBase - { +namespace libplctag.DataTypes; - public override int? ElementSize => 12; +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class TimerPlcMapper : PlcMapperBase +{ - public override AbTimer Decode(Tag tag, int offset) - { + public override int? ElementSize => 12; - // Needed to look at RsLogix documentation for structure of TIMER - var DINT2 = tag.GetInt32(offset); - var DINT1 = tag.GetInt32(offset + 4); - var DINT0 = tag.GetInt32(offset + 8); + public override AbTimer Decode(Tag tag, int offset) + { - // The third DINT packs a few BOOLs into it - var bitArray = new BitArray(new int[] { DINT2 }); + // Needed to look at RsLogix documentation for structure of TIMER + var DINT2 = tag.GetInt32(offset); + var DINT1 = tag.GetInt32(offset + 4); + var DINT0 = tag.GetInt32(offset + 8); - var timer = new AbTimer - { - Accumulated = DINT0, // ACC - Preset = DINT1, // PRE - Done = bitArray[29], // DN - InProgress = bitArray[30], // TT - Enabled = bitArray[31] // EN - }; + // The third DINT packs a few BOOLs into it + var bitArray = new BitArray(new int[] { DINT2 }); - return timer; + var timer = new AbTimer + { + Accumulated = DINT0, // ACC + Preset = DINT1, // PRE + Done = bitArray[29], // DN + InProgress = bitArray[30], // TT + Enabled = bitArray[31] // EN + }; - } + return timer; - public override void Encode(Tag tag, int offset, AbTimer value) - { - var DINT0 = value.Accumulated; - var DINT1 = value.Preset; + } - var asdf = new BitArray(32); - asdf[29] = value.Done; - asdf[30] = value.InProgress; - asdf[31] = value.Enabled; - var DINT2 = BitArrayToInt(asdf); + public override void Encode(Tag tag, int offset, AbTimer value) + { + var DINT0 = value.Accumulated; + var DINT1 = value.Preset; - tag.SetInt32(offset, DINT2); - tag.SetInt32(offset + 4, DINT1); - tag.SetInt32(offset + 8, DINT0); + var asdf = new BitArray(32); + asdf[29] = value.Done; + asdf[30] = value.InProgress; + asdf[31] = value.Enabled; + var DINT2 = BitArrayToInt(asdf); - } + tag.SetInt32(offset, DINT2); + tag.SetInt32(offset + 4, DINT1); + tag.SetInt32(offset + 8, DINT0); - static int BitArrayToInt(BitArray binary) - { - if (binary == null) - throw new ArgumentNullException("binary"); - if (binary.Length > 32) - throw new ArgumentException("Must be at most 32 bits long"); - - var result = new int[1]; - binary.CopyTo(result, 0); - return result[0]; - } } - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class AbTimer + static int BitArrayToInt(BitArray binary) { - public int Preset { get; set; } - public int Accumulated { get; set; } - public bool Enabled { get; set; } - public bool InProgress { get; set; } - public bool Done { get; set; } + if (binary == null) + throw new ArgumentNullException("binary"); + if (binary.Length > 32) + throw new ArgumentException("Must be at most 32 bits long"); + + var result = new int[1]; + binary.CopyTo(result, 0); + return result[0]; } } + +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class AbTimer +{ + public int Preset { get; set; } + public int Accumulated { get; set; } + public bool Enabled { get; set; } + public bool InProgress { get; set; } + public bool Done { get; set; } +} \ No newline at end of file diff --git a/src/libplctag/DataTypes/UdtInfoPlcMapper.cs b/src/libplctag/DataTypes/UdtInfoPlcMapper.cs index 5b82ab96..0c53fe4e 100644 --- a/src/libplctag/DataTypes/UdtInfoPlcMapper.cs +++ b/src/libplctag/DataTypes/UdtInfoPlcMapper.cs @@ -10,8 +10,7 @@ using System.Linq; using System.Text; -namespace libplctag.DataTypes -{ +namespace libplctag.DataTypes; [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] public class UdtFieldInfo @@ -107,6 +106,4 @@ public void Encode(Tag tag, UdtInfo value) //TODO: We know this value after we decode once. SHould we trigger a decode or cache the value after first decode? return null; } - } - -} + } \ No newline at end of file diff --git a/src/libplctag/DebugLevel.cs b/src/libplctag/DebugLevel.cs index 5396f011..a9d33948 100644 --- a/src/libplctag/DebugLevel.cs +++ b/src/libplctag/DebugLevel.cs @@ -7,29 +7,28 @@ using libplctag.NativeImport; -namespace libplctag +namespace libplctag; + +/// +/// Debug levels available in the base libplctag library +/// +public enum DebugLevel { - /// - /// Debug levels available in the base libplctag library - /// - public enum DebugLevel - { - /// - None = DEBUG_LEVEL.PLCTAG_DEBUG_NONE, + /// + None = DEBUG_LEVEL.PLCTAG_DEBUG_NONE, - /// - Error = DEBUG_LEVEL.PLCTAG_DEBUG_ERROR, + /// + Error = DEBUG_LEVEL.PLCTAG_DEBUG_ERROR, - /// - Warn = DEBUG_LEVEL.PLCTAG_DEBUG_WARN, + /// + Warn = DEBUG_LEVEL.PLCTAG_DEBUG_WARN, - /// - Info = DEBUG_LEVEL.PLCTAG_DEBUG_INFO, + /// + Info = DEBUG_LEVEL.PLCTAG_DEBUG_INFO, - /// - Detail = DEBUG_LEVEL.PLCTAG_DEBUG_DETAIL, + /// + Detail = DEBUG_LEVEL.PLCTAG_DEBUG_DETAIL, - /// - Spew = DEBUG_LEVEL.PLCTAG_DEBUG_SPEW - } + /// + Spew = DEBUG_LEVEL.PLCTAG_DEBUG_SPEW } \ No newline at end of file diff --git a/src/libplctag/Event.cs b/src/libplctag/Event.cs index 46a1d8a5..79698c21 100644 --- a/src/libplctag/Event.cs +++ b/src/libplctag/Event.cs @@ -7,32 +7,31 @@ using libplctag.NativeImport; -namespace libplctag +namespace libplctag; + +/// +/// Events returned by the base libplctag library +/// +public enum Event { - /// - /// Events returned by the base libplctag library - /// - public enum Event - { - /// - ReadStarted = EVENT.PLCTAG_EVENT_READ_STARTED, + /// + ReadStarted = EVENT.PLCTAG_EVENT_READ_STARTED, - /// - ReadCompleted = EVENT.PLCTAG_EVENT_READ_COMPLETED, + /// + ReadCompleted = EVENT.PLCTAG_EVENT_READ_COMPLETED, - /// - WriteStarted = EVENT.PLCTAG_EVENT_WRITE_STARTED, + /// + WriteStarted = EVENT.PLCTAG_EVENT_WRITE_STARTED, - /// - WriteCompleted = EVENT.PLCTAG_EVENT_WRITE_COMPLETED, + /// + WriteCompleted = EVENT.PLCTAG_EVENT_WRITE_COMPLETED, - /// - Aborted = EVENT.PLCTAG_EVENT_ABORTED, + /// + Aborted = EVENT.PLCTAG_EVENT_ABORTED, - /// - Destroyed = EVENT.PLCTAG_EVENT_DESTROYED, + /// + Destroyed = EVENT.PLCTAG_EVENT_DESTROYED, - /// - Created = EVENT.PLCTAG_EVENT_CREATED - } + /// + Created = EVENT.PLCTAG_EVENT_CREATED } \ No newline at end of file diff --git a/src/libplctag/INative.cs b/src/libplctag/INative.cs index ebc82cd9..884c9303 100644 --- a/src/libplctag/INative.cs +++ b/src/libplctag/INative.cs @@ -14,60 +14,59 @@ [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] -namespace libplctag +namespace libplctag; + +interface INative { - interface INative - { - int plc_tag_abort(int tag); - int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch); - int plc_tag_create(string lpString, int timeout); - int plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout); - string plc_tag_decode_error(int err); - int plc_tag_destroy(int tag); - int plc_tag_get_bit(int tag, int offset_bit); - float plc_tag_get_float32(int tag, int offset); - double plc_tag_get_float64(int tag, int offset); - short plc_tag_get_int16(int tag, int offset); - int plc_tag_get_int32(int tag, int offset); - long plc_tag_get_int64(int tag, int offset); - sbyte plc_tag_get_int8(int tag, int offset); - int plc_tag_get_int_attribute(int tag, string attrib_name, int default_value); - int plc_tag_set_int_attribute(int tag, string attrib_name, int new_value); - int plc_tag_get_byte_array_attribute(int tag, string attrib_name, byte[] buffer, int buffer_length); - int plc_tag_get_size(int tag); - int plc_tag_set_size(int tag, int new_size); - ushort plc_tag_get_uint16(int tag, int offset); - uint plc_tag_get_uint32(int tag, int offset); - ulong plc_tag_get_uint64(int tag, int offset); - byte plc_tag_get_uint8(int tag, int offset); - int plc_tag_lock(int tag); - int plc_tag_read(int tag, int timeout); - int plc_tag_register_callback(int tag_id, callback_func func); - int plc_tag_register_logger(log_callback_func func); - int plc_tag_set_bit(int tag, int offset_bit, int val); - void plc_tag_set_debug_level(int debug_level); - int plc_tag_set_float32(int tag, int offset, float val); - int plc_tag_set_float64(int tag, int offset, double val); - int plc_tag_set_int16(int tag, int offset, short val); - int plc_tag_set_int32(int tag, int offset, int val); - int plc_tag_set_int64(int tag, int offset, long val); - int plc_tag_set_int8(int tag, int offset, sbyte val); - int plc_tag_set_uint16(int tag, int offset, ushort val); - int plc_tag_set_uint32(int tag, int offset, uint val); - int plc_tag_set_uint64(int tag, int offset, ulong val); - int plc_tag_set_uint8(int tag, int offset, byte val); - void plc_tag_shutdown(); - int plc_tag_status(int tag); - int plc_tag_unlock(int tag); - int plc_tag_unregister_callback(int tag_id); - int plc_tag_unregister_logger(); - int plc_tag_write(int tag, int timeout); - int plc_tag_get_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length); - int plc_tag_set_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length); - int plc_tag_get_string_length(int tag, int string_start_offset); - int plc_tag_get_string(int tag, int string_start_offset, StringBuilder buffer, int buffer_length); - int plc_tag_get_string_total_length(int tag, int string_start_offset); - int plc_tag_get_string_capacity(int tag, int string_start_offset); - int plc_tag_set_string(int tag, int string_start_offset, string string_val); - } + int plc_tag_abort(int tag); + int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch); + int plc_tag_create(string lpString, int timeout); + int plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout); + string plc_tag_decode_error(int err); + int plc_tag_destroy(int tag); + int plc_tag_get_bit(int tag, int offset_bit); + float plc_tag_get_float32(int tag, int offset); + double plc_tag_get_float64(int tag, int offset); + short plc_tag_get_int16(int tag, int offset); + int plc_tag_get_int32(int tag, int offset); + long plc_tag_get_int64(int tag, int offset); + sbyte plc_tag_get_int8(int tag, int offset); + int plc_tag_get_int_attribute(int tag, string attrib_name, int default_value); + int plc_tag_set_int_attribute(int tag, string attrib_name, int new_value); + int plc_tag_get_byte_array_attribute(int tag, string attrib_name, byte[] buffer, int buffer_length); + int plc_tag_get_size(int tag); + int plc_tag_set_size(int tag, int new_size); + ushort plc_tag_get_uint16(int tag, int offset); + uint plc_tag_get_uint32(int tag, int offset); + ulong plc_tag_get_uint64(int tag, int offset); + byte plc_tag_get_uint8(int tag, int offset); + int plc_tag_lock(int tag); + int plc_tag_read(int tag, int timeout); + int plc_tag_register_callback(int tag_id, callback_func func); + int plc_tag_register_logger(log_callback_func func); + int plc_tag_set_bit(int tag, int offset_bit, int val); + void plc_tag_set_debug_level(int debug_level); + int plc_tag_set_float32(int tag, int offset, float val); + int plc_tag_set_float64(int tag, int offset, double val); + int plc_tag_set_int16(int tag, int offset, short val); + int plc_tag_set_int32(int tag, int offset, int val); + int plc_tag_set_int64(int tag, int offset, long val); + int plc_tag_set_int8(int tag, int offset, sbyte val); + int plc_tag_set_uint16(int tag, int offset, ushort val); + int plc_tag_set_uint32(int tag, int offset, uint val); + int plc_tag_set_uint64(int tag, int offset, ulong val); + int plc_tag_set_uint8(int tag, int offset, byte val); + void plc_tag_shutdown(); + int plc_tag_status(int tag); + int plc_tag_unlock(int tag); + int plc_tag_unregister_callback(int tag_id); + int plc_tag_unregister_logger(); + int plc_tag_write(int tag, int timeout); + int plc_tag_get_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length); + int plc_tag_set_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length); + int plc_tag_get_string_length(int tag, int string_start_offset); + int plc_tag_get_string(int tag, int string_start_offset, StringBuilder buffer, int buffer_length); + int plc_tag_get_string_total_length(int tag, int string_start_offset); + int plc_tag_get_string_capacity(int tag, int string_start_offset); + int plc_tag_set_string(int tag, int string_start_offset, string string_val); } \ No newline at end of file diff --git a/src/libplctag/ITag.cs b/src/libplctag/ITag.cs index 48c14a25..c0dceb30 100644 --- a/src/libplctag/ITag.cs +++ b/src/libplctag/ITag.cs @@ -9,44 +9,43 @@ using System.Threading; using System.Threading.Tasks; -namespace libplctag +namespace libplctag; + +/// +/// An interface to represent any generic tag without +/// exposing its value +/// +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public interface ITag : IDisposable { - /// - /// An interface to represent any generic tag without - /// exposing its value - /// - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public interface ITag : IDisposable - { - int[] ArrayDimensions { get; set; } - string Gateway { get; set; } - string Name { get; set; } - string Path { get; set; } - PlcType? PlcType { get; set; } - Protocol? Protocol { get; set; } - int? ReadCacheMillisecondDuration { get; set; } - TimeSpan Timeout { get; set; } - bool? UseConnectedMessaging { get; set; } - bool? AllowPacking { get; set; } - TimeSpan? AutoSyncReadInterval { get; set; } - TimeSpan? AutoSyncWriteInterval { get; set; } - DebugLevel DebugLevel { get; set; } + int[] ArrayDimensions { get; set; } + string Gateway { get; set; } + string Name { get; set; } + string Path { get; set; } + PlcType? PlcType { get; set; } + Protocol? Protocol { get; set; } + int? ReadCacheMillisecondDuration { get; set; } + TimeSpan Timeout { get; set; } + bool? UseConnectedMessaging { get; set; } + bool? AllowPacking { get; set; } + TimeSpan? AutoSyncReadInterval { get; set; } + TimeSpan? AutoSyncWriteInterval { get; set; } + DebugLevel DebugLevel { get; set; } - event EventHandler ReadStarted; - event EventHandler ReadCompleted; - event EventHandler WriteStarted; - event EventHandler WriteCompleted; - event EventHandler Aborted; - event EventHandler Destroyed; + event EventHandler ReadStarted; + event EventHandler ReadCompleted; + event EventHandler WriteStarted; + event EventHandler WriteCompleted; + event EventHandler Aborted; + event EventHandler Destroyed; - Status GetStatus(); - void Initialize(); - Task InitializeAsync(CancellationToken token = default); - object Read(); - Task ReadAsync(CancellationToken token = default); - void Write(); - Task WriteAsync(CancellationToken token = default); + Status GetStatus(); + void Initialize(); + Task InitializeAsync(CancellationToken token = default); + object Read(); + Task ReadAsync(CancellationToken token = default); + void Write(); + Task WriteAsync(CancellationToken token = default); - object Value { get; set; } - } + object Value { get; set; } } \ No newline at end of file diff --git a/src/libplctag/LibPlcTag.cs b/src/libplctag/LibPlcTag.cs index 30f05ce1..cab95e8f 100644 --- a/src/libplctag/LibPlcTag.cs +++ b/src/libplctag/LibPlcTag.cs @@ -8,115 +8,114 @@ using System; using static libplctag.NativeImport.plctag; -namespace libplctag +namespace libplctag; + +/// +/// A static class used to access some additional features of the libplctag base library +/// +public static class LibPlcTag { - /// - /// A static class used to access some additional features of the libplctag base library - /// - public static class LibPlcTag - { - static INative _native = new Native(); + static INative _native = new Native(); - private const int LIB_ATTRIBUTE_POINTER = 0; + private const int LIB_ATTRIBUTE_POINTER = 0; - static public int VersionMajor => _native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "version_major", int.MinValue); - static public int VersionMinor => _native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "version_minor", int.MinValue); - static public int VersionPatch => _native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "version_patch", int.MinValue); + static public int VersionMajor => _native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "version_major", int.MinValue); + static public int VersionMinor => _native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "version_minor", int.MinValue); + static public int VersionPatch => _native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "version_patch", int.MinValue); - /// - /// Check if base library meets version requirements - /// - /// Major - /// Minor - /// Patch - /// - static public bool IsRequiredVersion(int requiredMajor, int requiredMinor, int requiredPatch) - { - var result = (Status)_native.plc_tag_check_lib_version(requiredMajor, requiredMinor, requiredPatch); - - if (result == Status.Ok) - return true; - else if (result == Status.ErrorUnsupported) - return false; - else - throw new NotImplementedException(); - } + /// + /// Check if base library meets version requirements + /// + /// Major + /// Minor + /// Patch + /// + static public bool IsRequiredVersion(int requiredMajor, int requiredMinor, int requiredPatch) + { + var result = (Status)_native.plc_tag_check_lib_version(requiredMajor, requiredMinor, requiredPatch); + + if (result == Status.Ok) + return true; + else if (result == Status.ErrorUnsupported) + return false; + else + throw new NotImplementedException(); + } - /// - /// Sets a debug level for the underlying libplctag library - /// - static public DebugLevel DebugLevel - { - get => (DebugLevel)_native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "debug", int.MinValue); - set => _native.plc_tag_set_debug_level((int)value); - } + /// + /// Sets a debug level for the underlying libplctag library + /// + static public DebugLevel DebugLevel + { + get => (DebugLevel)_native.plc_tag_get_int_attribute(LIB_ATTRIBUTE_POINTER, "debug", int.MinValue); + set => _native.plc_tag_set_debug_level((int)value); + } - static readonly object logEventSubscriptionLock = new object(); - static private event EventHandler logEvent; - static bool alreadySubscribedToEvents = false; - static log_callback_func loggerDelegate; - static private void ensureSubscribeToEvents() - { - if (alreadySubscribedToEvents) - return; + static readonly object logEventSubscriptionLock = new object(); + static private event EventHandler logEvent; + static bool alreadySubscribedToEvents = false; + static log_callback_func loggerDelegate; + static private void ensureSubscribeToEvents() + { + if (alreadySubscribedToEvents) + return; - loggerDelegate = new log_callback_func(invokeLogEvent); - var statusAfterRegistration = (Status)_native.plc_tag_register_logger(loggerDelegate); - if (statusAfterRegistration != Status.Ok) - throw new LibPlcTagException(statusAfterRegistration); + loggerDelegate = new log_callback_func(invokeLogEvent); + var statusAfterRegistration = (Status)_native.plc_tag_register_logger(loggerDelegate); + if (statusAfterRegistration != Status.Ok) + throw new LibPlcTagException(statusAfterRegistration); - alreadySubscribedToEvents = true; - } + alreadySubscribedToEvents = true; + } - static void invokeLogEvent(int tag_id, int debug_level, string message) - { - logEvent?.Invoke(null, new LogEventArgs() { DebugLevel = (DebugLevel)debug_level, Message = message.TrimEnd('\n') }); - } + static void invokeLogEvent(int tag_id, int debug_level, string message) + { + logEvent?.Invoke(null, new LogEventArgs() { DebugLevel = (DebugLevel)debug_level, Message = message.TrimEnd('\n') }); + } - /// - /// You can redirect all logging from the library to your own delegate. - /// - /// - /// WARNING There are some important restrictions on logging delegates: - /// The delegate will be called from multiple threads, sometimes simultaneously! Your code must be thread aware and thread-safe. - /// The delegate will be called with one or more mutexes held in almost all cases.You must not call any tag API functions other than plc_tag_decode_error(). If you do there is a large chance that the library will hang. - /// Logging messages come from deep within the library's core routines. Many of these are very delay sensitive. Do not do anything that would block or delay the return of the logging callback to the library! - /// The message string passed to your callback function will be managed by the library. Do not attempt to free its memory or modify the string. If you need to do modifications, make your own copy and return. - /// - static public event EventHandler LogEvent + /// + /// You can redirect all logging from the library to your own delegate. + /// + /// + /// WARNING There are some important restrictions on logging delegates: + /// The delegate will be called from multiple threads, sometimes simultaneously! Your code must be thread aware and thread-safe. + /// The delegate will be called with one or more mutexes held in almost all cases.You must not call any tag API functions other than plc_tag_decode_error(). If you do there is a large chance that the library will hang. + /// Logging messages come from deep within the library's core routines. Many of these are very delay sensitive. Do not do anything that would block or delay the return of the logging callback to the library! + /// The message string passed to your callback function will be managed by the library. Do not attempt to free its memory or modify the string. If you need to do modifications, make your own copy and return. + /// + static public event EventHandler LogEvent + { + add { - add - { - lock (logEventSubscriptionLock) - { - ensureSubscribeToEvents(); - logEvent += value; - } - } - remove + lock (logEventSubscriptionLock) { - lock (logEventSubscriptionLock) - { - logEvent -= value; - } + ensureSubscribeToEvents(); + logEvent += value; } } - - /// - /// After this function returns, the library will have cleaned up all internal threads and resources. - /// You can immediately turn around and call plc_tag_create() again and the library will start up again. - /// Note: you must dispose of all Tags before calling - /// - /// - /// Some wrappers and systems are not able to trigger the standard POSIX or Windows functions when the library is - /// being unloaded or the program is shutting down. In those cases, you can call this function. - /// - static public void Shutdown() + remove { - _native.plc_tag_shutdown(); + lock (logEventSubscriptionLock) + { + logEvent -= value; + } } + } + /// + /// After this function returns, the library will have cleaned up all internal threads and resources. + /// You can immediately turn around and call plc_tag_create() again and the library will start up again. + /// Note: you must dispose of all Tags before calling + /// + /// + /// Some wrappers and systems are not able to trigger the standard POSIX or Windows functions when the library is + /// being unloaded or the program is shutting down. In those cases, you can call this function. + /// + static public void Shutdown() + { + _native.plc_tag_shutdown(); } -} + +} \ No newline at end of file diff --git a/src/libplctag/LibPlcTagException.cs b/src/libplctag/LibPlcTagException.cs index 8c430168..f83869fa 100644 --- a/src/libplctag/LibPlcTagException.cs +++ b/src/libplctag/LibPlcTagException.cs @@ -7,8 +7,7 @@ using System; -namespace libplctag -{ +namespace libplctag; /// /// An exception thrown by the underlying libplctag library @@ -36,5 +35,4 @@ public LibPlcTagException(Status status) { Status = status; } - } -} + } \ No newline at end of file diff --git a/src/libplctag/LogEventArgs.cs b/src/libplctag/LogEventArgs.cs index 55565cd4..afb19cb6 100644 --- a/src/libplctag/LogEventArgs.cs +++ b/src/libplctag/LogEventArgs.cs @@ -7,11 +7,10 @@ using System; -namespace libplctag +namespace libplctag; + +public class LogEventArgs : EventArgs { - public class LogEventArgs : EventArgs - { - public DebugLevel DebugLevel { get; set; } - public string Message { get; set; } - } -} + public DebugLevel DebugLevel { get; set; } + public string Message { get; set; } +} \ No newline at end of file diff --git a/src/libplctag/Native.cs b/src/libplctag/Native.cs index 9a2d2f78..66987871 100644 --- a/src/libplctag/Native.cs +++ b/src/libplctag/Native.cs @@ -10,60 +10,59 @@ using libplctag.NativeImport; using static libplctag.NativeImport.plctag; -namespace libplctag +namespace libplctag; + +class Native : INative { - class Native : INative - { - public int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch) => plctag.plc_tag_check_lib_version(req_major, req_minor, req_patch); - public Int32 plc_tag_create(string lpString, int timeout) => plctag.plc_tag_create(lpString, timeout); - public Int32 plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout) => plctag.plc_tag_create_ex(lpString, func, userdata, timeout); - public int plc_tag_destroy(Int32 tag) => plctag.plc_tag_destroy(tag); - public void plc_tag_shutdown() => plctag.plc_tag_shutdown(); - public int plc_tag_register_callback(Int32 tag_id, callback_func func) => plctag.plc_tag_register_callback(tag_id, func); - public int plc_tag_unregister_callback(Int32 tag_id) => plctag.plc_tag_unregister_callback(tag_id); - public int plc_tag_register_logger(log_callback_func func) => plctag.plc_tag_register_logger(func); - public int plc_tag_unregister_logger() => plctag.plc_tag_unregister_logger(); - public int plc_tag_lock(Int32 tag) => plctag.plc_tag_lock(tag); - public int plc_tag_unlock(Int32 tag) => plctag.plc_tag_unlock(tag); - public int plc_tag_status(Int32 tag) => plctag.plc_tag_status(tag); - public string plc_tag_decode_error(int err) => plctag.plc_tag_decode_error(err); - public int plc_tag_read(Int32 tag, int timeout) => plctag.plc_tag_read(tag, timeout); - public int plc_tag_write(Int32 tag, int timeout) => plctag.plc_tag_write(tag, timeout); - public int plc_tag_get_size(Int32 tag) => plctag.plc_tag_get_size(tag); - public int plc_tag_set_size(Int32 tag, int new_size) => plctag.plc_tag_set_size(tag, new_size); - public int plc_tag_abort(Int32 tag) => plctag.plc_tag_abort(tag); - public int plc_tag_get_int_attribute(Int32 tag, string attrib_name, int default_value) => plctag.plc_tag_get_int_attribute(tag, attrib_name, default_value); - public int plc_tag_set_int_attribute(Int32 tag, string attrib_name, int new_value) => plctag.plc_tag_set_int_attribute(tag, attrib_name, new_value); - public int plc_tag_get_byte_array_attribute(Int32 tag, string attrib_name, byte[] buffer, int buffer_length) => plctag.plc_tag_get_byte_array_attribute(tag, attrib_name, buffer, buffer_length); - public UInt64 plc_tag_get_uint64(Int32 tag, int offset) => plctag.plc_tag_get_uint64(tag, offset); - public Int64 plc_tag_get_int64(Int32 tag, int offset) => plctag.plc_tag_get_int64(tag, offset); - public int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val) => plctag.plc_tag_set_uint64(tag, offset, val); - public int plc_tag_set_int64(Int32 tag, int offset, Int64 val) => plctag.plc_tag_set_int64(tag, offset, val); - public double plc_tag_get_float64(Int32 tag, int offset) => plctag.plc_tag_get_float64(tag, offset); - public int plc_tag_set_float64(Int32 tag, int offset, double val) => plctag.plc_tag_set_float64(tag, offset, val); - public UInt32 plc_tag_get_uint32(Int32 tag, int offset) => plctag.plc_tag_get_uint32(tag, offset); - public Int32 plc_tag_get_int32(Int32 tag, int offset) => plctag.plc_tag_get_int32(tag, offset); - public int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val) => plctag.plc_tag_set_uint32(tag, offset, val); - public int plc_tag_set_int32(Int32 tag, int offset, Int32 val) => plctag.plc_tag_set_int32(tag, offset, val); - public float plc_tag_get_float32(Int32 tag, int offset) => plctag.plc_tag_get_float32(tag, offset); - public int plc_tag_set_float32(Int32 tag, int offset, float val) => plctag.plc_tag_set_float32(tag, offset, val); - public UInt16 plc_tag_get_uint16(Int32 tag, int offset) => plctag.plc_tag_get_uint16(tag, offset); - public Int16 plc_tag_get_int16(Int32 tag, int offset) => plctag.plc_tag_get_int16(tag, offset); - public int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val) => plctag.plc_tag_set_uint16(tag, offset, val); - public int plc_tag_set_int16(Int32 tag, int offset, Int16 val) => plctag.plc_tag_set_int16(tag, offset, val); - public byte plc_tag_get_uint8(Int32 tag, int offset) => plctag.plc_tag_get_uint8(tag, offset); - public sbyte plc_tag_get_int8(Int32 tag, int offset) => plctag.plc_tag_get_int8(tag, offset); - public int plc_tag_set_uint8(Int32 tag, int offset, byte val) => plctag.plc_tag_set_uint8(tag, offset, val); - public int plc_tag_set_int8(Int32 tag, int offset, sbyte val) => plctag.plc_tag_set_int8(tag, offset, val); - public int plc_tag_get_bit(Int32 tag, int offset_bit) => plctag.plc_tag_get_bit(tag, offset_bit); - public int plc_tag_set_bit(Int32 tag, int offset_bit, int val) => plctag.plc_tag_set_bit(tag, offset_bit, val); - public void plc_tag_set_debug_level(int debug_level) => plctag.plc_tag_set_debug_level(debug_level); - public int plc_tag_get_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length) => plctag.plc_tag_get_raw_bytes(tag, start_offset, buffer, buffer_length); - public int plc_tag_set_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length) => plctag.plc_tag_set_raw_bytes(tag, start_offset, buffer, buffer_length); - public int plc_tag_get_string_length(int tag, int string_start_offset) => plctag.plc_tag_get_string_length(tag, string_start_offset); - public int plc_tag_get_string(int tag, int string_start_offset, StringBuilder buffer, int buffer_length) => plctag.plc_tag_get_string(tag, string_start_offset, buffer, buffer_length); - public int plc_tag_get_string_total_length(int tag, int string_start_offset) => plctag.plc_tag_get_string_total_length(tag, string_start_offset); - public int plc_tag_get_string_capacity(int tag, int string_start_offset) => plctag.plc_tag_get_string_capacity(tag, string_start_offset); - public int plc_tag_set_string(int tag, int string_start_offset, string string_val) => plctag.plc_tag_set_string(tag, string_start_offset, string_val); - } -} + public int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch) => plctag.plc_tag_check_lib_version(req_major, req_minor, req_patch); + public Int32 plc_tag_create(string lpString, int timeout) => plctag.plc_tag_create(lpString, timeout); + public Int32 plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout) => plctag.plc_tag_create_ex(lpString, func, userdata, timeout); + public int plc_tag_destroy(Int32 tag) => plctag.plc_tag_destroy(tag); + public void plc_tag_shutdown() => plctag.plc_tag_shutdown(); + public int plc_tag_register_callback(Int32 tag_id, callback_func func) => plctag.plc_tag_register_callback(tag_id, func); + public int plc_tag_unregister_callback(Int32 tag_id) => plctag.plc_tag_unregister_callback(tag_id); + public int plc_tag_register_logger(log_callback_func func) => plctag.plc_tag_register_logger(func); + public int plc_tag_unregister_logger() => plctag.plc_tag_unregister_logger(); + public int plc_tag_lock(Int32 tag) => plctag.plc_tag_lock(tag); + public int plc_tag_unlock(Int32 tag) => plctag.plc_tag_unlock(tag); + public int plc_tag_status(Int32 tag) => plctag.plc_tag_status(tag); + public string plc_tag_decode_error(int err) => plctag.plc_tag_decode_error(err); + public int plc_tag_read(Int32 tag, int timeout) => plctag.plc_tag_read(tag, timeout); + public int plc_tag_write(Int32 tag, int timeout) => plctag.plc_tag_write(tag, timeout); + public int plc_tag_get_size(Int32 tag) => plctag.plc_tag_get_size(tag); + public int plc_tag_set_size(Int32 tag, int new_size) => plctag.plc_tag_set_size(tag, new_size); + public int plc_tag_abort(Int32 tag) => plctag.plc_tag_abort(tag); + public int plc_tag_get_int_attribute(Int32 tag, string attrib_name, int default_value) => plctag.plc_tag_get_int_attribute(tag, attrib_name, default_value); + public int plc_tag_set_int_attribute(Int32 tag, string attrib_name, int new_value) => plctag.plc_tag_set_int_attribute(tag, attrib_name, new_value); + public int plc_tag_get_byte_array_attribute(Int32 tag, string attrib_name, byte[] buffer, int buffer_length) => plctag.plc_tag_get_byte_array_attribute(tag, attrib_name, buffer, buffer_length); + public UInt64 plc_tag_get_uint64(Int32 tag, int offset) => plctag.plc_tag_get_uint64(tag, offset); + public Int64 plc_tag_get_int64(Int32 tag, int offset) => plctag.plc_tag_get_int64(tag, offset); + public int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val) => plctag.plc_tag_set_uint64(tag, offset, val); + public int plc_tag_set_int64(Int32 tag, int offset, Int64 val) => plctag.plc_tag_set_int64(tag, offset, val); + public double plc_tag_get_float64(Int32 tag, int offset) => plctag.plc_tag_get_float64(tag, offset); + public int plc_tag_set_float64(Int32 tag, int offset, double val) => plctag.plc_tag_set_float64(tag, offset, val); + public UInt32 plc_tag_get_uint32(Int32 tag, int offset) => plctag.plc_tag_get_uint32(tag, offset); + public Int32 plc_tag_get_int32(Int32 tag, int offset) => plctag.plc_tag_get_int32(tag, offset); + public int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val) => plctag.plc_tag_set_uint32(tag, offset, val); + public int plc_tag_set_int32(Int32 tag, int offset, Int32 val) => plctag.plc_tag_set_int32(tag, offset, val); + public float plc_tag_get_float32(Int32 tag, int offset) => plctag.plc_tag_get_float32(tag, offset); + public int plc_tag_set_float32(Int32 tag, int offset, float val) => plctag.plc_tag_set_float32(tag, offset, val); + public UInt16 plc_tag_get_uint16(Int32 tag, int offset) => plctag.plc_tag_get_uint16(tag, offset); + public Int16 plc_tag_get_int16(Int32 tag, int offset) => plctag.plc_tag_get_int16(tag, offset); + public int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val) => plctag.plc_tag_set_uint16(tag, offset, val); + public int plc_tag_set_int16(Int32 tag, int offset, Int16 val) => plctag.plc_tag_set_int16(tag, offset, val); + public byte plc_tag_get_uint8(Int32 tag, int offset) => plctag.plc_tag_get_uint8(tag, offset); + public sbyte plc_tag_get_int8(Int32 tag, int offset) => plctag.plc_tag_get_int8(tag, offset); + public int plc_tag_set_uint8(Int32 tag, int offset, byte val) => plctag.plc_tag_set_uint8(tag, offset, val); + public int plc_tag_set_int8(Int32 tag, int offset, sbyte val) => plctag.plc_tag_set_int8(tag, offset, val); + public int plc_tag_get_bit(Int32 tag, int offset_bit) => plctag.plc_tag_get_bit(tag, offset_bit); + public int plc_tag_set_bit(Int32 tag, int offset_bit, int val) => plctag.plc_tag_set_bit(tag, offset_bit, val); + public void plc_tag_set_debug_level(int debug_level) => plctag.plc_tag_set_debug_level(debug_level); + public int plc_tag_get_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length) => plctag.plc_tag_get_raw_bytes(tag, start_offset, buffer, buffer_length); + public int plc_tag_set_raw_bytes(int tag, int start_offset, byte[] buffer, int buffer_length) => plctag.plc_tag_set_raw_bytes(tag, start_offset, buffer, buffer_length); + public int plc_tag_get_string_length(int tag, int string_start_offset) => plctag.plc_tag_get_string_length(tag, string_start_offset); + public int plc_tag_get_string(int tag, int string_start_offset, StringBuilder buffer, int buffer_length) => plctag.plc_tag_get_string(tag, string_start_offset, buffer, buffer_length); + public int plc_tag_get_string_total_length(int tag, int string_start_offset) => plctag.plc_tag_get_string_total_length(tag, string_start_offset); + public int plc_tag_get_string_capacity(int tag, int string_start_offset) => plctag.plc_tag_get_string_capacity(tag, string_start_offset); + public int plc_tag_set_string(int tag, int string_start_offset, string string_val) => plctag.plc_tag_set_string(tag, string_start_offset, string_val); +} \ No newline at end of file diff --git a/src/libplctag/PlcType.cs b/src/libplctag/PlcType.cs index f2f77af2..d0a4003a 100644 --- a/src/libplctag/PlcType.cs +++ b/src/libplctag/PlcType.cs @@ -5,43 +5,42 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag +namespace libplctag; + +public enum PlcType { - public enum PlcType - { - /// - /// Control Logix-class PLC. Synonym for lgx, logix, controllogix, contrologix, compactlogix, clgx. - /// - ControlLogix, - - /// - /// PLC/5 PLC. Synonym for plc5, plc. - /// - Plc5, - - /// - /// SLC 500 PLC. Synonym for slc500, slc. - /// - Slc500, - - /// - /// Control Logix-class PLC using the PLC/5 protocol. Synonym for lgxpccc, logixpccc, lgxplc5, lgx_pccc, logix_pccc, lgx_plc5. - /// - LogixPccc, - - /// - /// Micro800-class PLC. Synonym for micrologix800, mlgx800, micro800. - /// - Micro800, - - /// - /// MicroLogix PLC. Synonym for micrologix, mlgx. - /// - MicroLogix, - - /// - /// Omron PLC. Synonym for omron-njnx, omron-nj, omron-nx, njnx, nx1p2 - /// - Omron, - } + /// + /// Control Logix-class PLC. Synonym for lgx, logix, controllogix, contrologix, compactlogix, clgx. + /// + ControlLogix, + + /// + /// PLC/5 PLC. Synonym for plc5, plc. + /// + Plc5, + + /// + /// SLC 500 PLC. Synonym for slc500, slc. + /// + Slc500, + + /// + /// Control Logix-class PLC using the PLC/5 protocol. Synonym for lgxpccc, logixpccc, lgxplc5, lgx_pccc, logix_pccc, lgx_plc5. + /// + LogixPccc, + + /// + /// Micro800-class PLC. Synonym for micrologix800, mlgx800, micro800. + /// + Micro800, + + /// + /// MicroLogix PLC. Synonym for micrologix, mlgx. + /// + MicroLogix, + + /// + /// Omron PLC. Synonym for omron-njnx, omron-nj, omron-nx, njnx, nx1p2 + /// + Omron, } \ No newline at end of file diff --git a/src/libplctag/Protocol.cs b/src/libplctag/Protocol.cs index 58cffb01..1b4920de 100644 --- a/src/libplctag/Protocol.cs +++ b/src/libplctag/Protocol.cs @@ -5,18 +5,17 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -namespace libplctag +namespace libplctag; + +public enum Protocol { - public enum Protocol - { - /// - /// Allen-Bradley specific flavor of EIP - /// - ab_eip, + /// + /// Allen-Bradley specific flavor of EIP + /// + ab_eip, - /// - /// A Modbus TCP implementation used by many PLCs - /// - modbus_tcp - } + /// + /// A Modbus TCP implementation used by many PLCs + /// + modbus_tcp } \ No newline at end of file diff --git a/src/libplctag/Status.cs b/src/libplctag/Status.cs index 1a0f152b..19c2d46f 100644 --- a/src/libplctag/Status.cs +++ b/src/libplctag/Status.cs @@ -7,131 +7,130 @@ using libplctag.NativeImport; -namespace libplctag +namespace libplctag; + +public enum Status { - public enum Status - { - /// - Pending = STATUS.PLCTAG_STATUS_PENDING, + /// + Pending = STATUS.PLCTAG_STATUS_PENDING, - /// - Ok = STATUS.PLCTAG_STATUS_OK, + /// + Ok = STATUS.PLCTAG_STATUS_OK, - /// - ErrorAbort = STATUS.PLCTAG_ERR_ABORT, + /// + ErrorAbort = STATUS.PLCTAG_ERR_ABORT, - /// - ErrorBadConfig = STATUS.PLCTAG_ERR_BAD_CONFIG, + /// + ErrorBadConfig = STATUS.PLCTAG_ERR_BAD_CONFIG, - /// - ErrorBadConnection = STATUS.PLCTAG_ERR_BAD_CONNECTION, + /// + ErrorBadConnection = STATUS.PLCTAG_ERR_BAD_CONNECTION, - /// - ErrorBadData = STATUS.PLCTAG_ERR_BAD_DATA, + /// + ErrorBadData = STATUS.PLCTAG_ERR_BAD_DATA, - /// - ErrorBadDevice = STATUS.PLCTAG_ERR_BAD_DEVICE, + /// + ErrorBadDevice = STATUS.PLCTAG_ERR_BAD_DEVICE, - /// - ErrorBadGateway = STATUS.PLCTAG_ERR_BAD_GATEWAY, + /// + ErrorBadGateway = STATUS.PLCTAG_ERR_BAD_GATEWAY, - /// - ErrorBadParam = STATUS.PLCTAG_ERR_BAD_PARAM, + /// + ErrorBadParam = STATUS.PLCTAG_ERR_BAD_PARAM, - /// - ErrorBadReply = STATUS.PLCTAG_ERR_BAD_REPLY, + /// + ErrorBadReply = STATUS.PLCTAG_ERR_BAD_REPLY, - /// - ErrorBadStatus = STATUS.PLCTAG_ERR_BAD_STATUS, + /// + ErrorBadStatus = STATUS.PLCTAG_ERR_BAD_STATUS, - /// - ErrorClose = STATUS.PLCTAG_ERR_CLOSE, + /// + ErrorClose = STATUS.PLCTAG_ERR_CLOSE, - /// - ErrorCreate = STATUS.PLCTAG_ERR_CREATE, + /// + ErrorCreate = STATUS.PLCTAG_ERR_CREATE, - /// - ErrorDuplicate = STATUS.PLCTAG_ERR_DUPLICATE, + /// + ErrorDuplicate = STATUS.PLCTAG_ERR_DUPLICATE, - /// - ErrorEncode = STATUS.PLCTAG_ERR_ENCODE, + /// + ErrorEncode = STATUS.PLCTAG_ERR_ENCODE, - /// - ErrorMutexDestroy = STATUS.PLCTAG_ERR_MUTEX_DESTROY, + /// + ErrorMutexDestroy = STATUS.PLCTAG_ERR_MUTEX_DESTROY, - /// - ErrorMutexInit = STATUS.PLCTAG_ERR_MUTEX_INIT, + /// + ErrorMutexInit = STATUS.PLCTAG_ERR_MUTEX_INIT, - /// - ErrorMutexLock = STATUS.PLCTAG_ERR_MUTEX_LOCK, + /// + ErrorMutexLock = STATUS.PLCTAG_ERR_MUTEX_LOCK, - /// - ErrorMutexUnlock = STATUS.PLCTAG_ERR_MUTEX_UNLOCK, + /// + ErrorMutexUnlock = STATUS.PLCTAG_ERR_MUTEX_UNLOCK, - /// - ErrorNotAllowed = STATUS.PLCTAG_ERR_NOT_ALLOWED, + /// + ErrorNotAllowed = STATUS.PLCTAG_ERR_NOT_ALLOWED, - /// - ErrorNotFound = STATUS.PLCTAG_ERR_NOT_FOUND, + /// + ErrorNotFound = STATUS.PLCTAG_ERR_NOT_FOUND, - /// - ErrorNotImplemented = STATUS.PLCTAG_ERR_NOT_IMPLEMENTED, + /// + ErrorNotImplemented = STATUS.PLCTAG_ERR_NOT_IMPLEMENTED, - /// - ErrorNoData = STATUS.PLCTAG_ERR_NO_DATA, + /// + ErrorNoData = STATUS.PLCTAG_ERR_NO_DATA, - /// - ErrorNoMatch = STATUS.PLCTAG_ERR_NO_MATCH, + /// + ErrorNoMatch = STATUS.PLCTAG_ERR_NO_MATCH, - /// - ErrorNoMem = STATUS.PLCTAG_ERR_NO_MEM, + /// + ErrorNoMem = STATUS.PLCTAG_ERR_NO_MEM, - /// - ErrorNoResources = STATUS.PLCTAG_ERR_NO_RESOURCES, + /// + ErrorNoResources = STATUS.PLCTAG_ERR_NO_RESOURCES, - /// - ErrorNullPtr = STATUS.PLCTAG_ERR_NULL_PTR, + /// + ErrorNullPtr = STATUS.PLCTAG_ERR_NULL_PTR, - /// - ErrorOpen = STATUS.PLCTAG_ERR_OPEN, + /// + ErrorOpen = STATUS.PLCTAG_ERR_OPEN, - /// - ErrorOutOfBounds = STATUS.PLCTAG_ERR_OUT_OF_BOUNDS, + /// + ErrorOutOfBounds = STATUS.PLCTAG_ERR_OUT_OF_BOUNDS, - /// - ErrorRead = STATUS.PLCTAG_ERR_READ, + /// + ErrorRead = STATUS.PLCTAG_ERR_READ, - /// - ErrorRemoteErr = STATUS.PLCTAG_ERR_REMOTE_ERR, + /// + ErrorRemoteErr = STATUS.PLCTAG_ERR_REMOTE_ERR, - /// - ErrorThreadCreate = STATUS.PLCTAG_ERR_THREAD_CREATE, + /// + ErrorThreadCreate = STATUS.PLCTAG_ERR_THREAD_CREATE, - /// - ErrorThreadJoin = STATUS.PLCTAG_ERR_THREAD_JOIN, + /// + ErrorThreadJoin = STATUS.PLCTAG_ERR_THREAD_JOIN, - /// - ErrorTimeout = STATUS.PLCTAG_ERR_TIMEOUT, + /// + ErrorTimeout = STATUS.PLCTAG_ERR_TIMEOUT, - /// - ErrorTooLarge = STATUS.PLCTAG_ERR_TOO_LARGE, + /// + ErrorTooLarge = STATUS.PLCTAG_ERR_TOO_LARGE, - /// - ErrorTooSmall = STATUS.PLCTAG_ERR_TOO_SMALL, + /// + ErrorTooSmall = STATUS.PLCTAG_ERR_TOO_SMALL, - /// - ErrorUnsupported = STATUS.PLCTAG_ERR_UNSUPPORTED, + /// + ErrorUnsupported = STATUS.PLCTAG_ERR_UNSUPPORTED, - /// - ErrorWinsock = STATUS.PLCTAG_ERR_WINSOCK, + /// + ErrorWinsock = STATUS.PLCTAG_ERR_WINSOCK, - /// - ErrorWrite = STATUS.PLCTAG_ERR_WRITE, + /// + ErrorWrite = STATUS.PLCTAG_ERR_WRITE, - /// - ErrorPartial = STATUS.PLCTAG_ERR_PARTIAL, + /// + ErrorPartial = STATUS.PLCTAG_ERR_PARTIAL, - /// - ErrorBusy = STATUS.PLCTAG_ERR_BUSY - } + /// + ErrorBusy = STATUS.PLCTAG_ERR_BUSY } \ No newline at end of file diff --git a/src/libplctag/Tag.cs b/src/libplctag/Tag.cs index 730b346f..f8168775 100644 --- a/src/libplctag/Tag.cs +++ b/src/libplctag/Tag.cs @@ -18,8 +18,7 @@ [assembly: InternalsVisibleTo("libplctag.Tests")] -namespace libplctag -{ +namespace libplctag; public sealed class Tag : IDisposable { @@ -1337,6 +1336,4 @@ void coreLibEventCallback(int eventTagHandle, int eventCode, int statusCode, Int } } - } - -} + } \ No newline at end of file diff --git a/src/libplctag/TagEventArgs.cs b/src/libplctag/TagEventArgs.cs index c053d122..7a7f7ba7 100644 --- a/src/libplctag/TagEventArgs.cs +++ b/src/libplctag/TagEventArgs.cs @@ -7,10 +7,9 @@ using System; -namespace libplctag +namespace libplctag; + +public class TagEventArgs : EventArgs { - public class TagEventArgs : EventArgs - { - public Status Status { get; set; } - } -} + public Status Status { get; set; } +} \ No newline at end of file diff --git a/src/libplctag/TagOfT.cs b/src/libplctag/TagOfT.cs index 40214523..6b7ec8a9 100644 --- a/src/libplctag/TagOfT.cs +++ b/src/libplctag/TagOfT.cs @@ -10,252 +10,251 @@ using System.Threading; using System.Threading.Tasks; -namespace libplctag +namespace libplctag; + +/// +/// A class that allows for strongly-typed objects tied to PLC tags +/// +/// A class that handles data conversion +/// The desired C# type of Tag.Value +[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] +public class Tag : IDisposable, ITag where M : IPlcMapper, new() { - /// - /// A class that allows for strongly-typed objects tied to PLC tags - /// - /// A class that handles data conversion - /// The desired C# type of Tag.Value - [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] - public class Tag : IDisposable, ITag where M : IPlcMapper, new() - { - private readonly Tag _tag; - private readonly IPlcMapper _plcMapper; + private readonly Tag _tag; + private readonly IPlcMapper _plcMapper; - public Tag() - { - _plcMapper = new M(); - _tag = new Tag() - { - ElementSize = _plcMapper.ElementSize, - }; - - _tag.ReadStarted += (s, e) => ReadStarted?.Invoke(this, e); - _tag.ReadCompleted += (s, e) => - { - // If AutoSyncReadInterval is configured, then this event was almost certainly not triggered - // by a call to Read/ReadAsync - and therefore the data needs to be decoded. - if(AutoSyncReadInterval != null) DecodeAll(); - ReadCompleted?.Invoke(this, e); - }; - _tag.WriteStarted += (s, e) => WriteStarted?.Invoke(this, e); - _tag.WriteCompleted += (s, e) => WriteCompleted?.Invoke(this, e); - _tag.Aborted += (s, e) => Aborted?.Invoke(this, e); - _tag.Destroyed += (s, e) => Destroyed?.Invoke(this, e); - } - - /// - public Protocol? Protocol + public Tag() + { + _plcMapper = new M(); + _tag = new Tag() { - get => _tag.Protocol; - set => _tag.Protocol = value; - } + ElementSize = _plcMapper.ElementSize, + }; - /// - public string Gateway + _tag.ReadStarted += (s, e) => ReadStarted?.Invoke(this, e); + _tag.ReadCompleted += (s, e) => { - get => _tag.Gateway; - set => _tag.Gateway = value; - } + // If AutoSyncReadInterval is configured, then this event was almost certainly not triggered + // by a call to Read/ReadAsync - and therefore the data needs to be decoded. + if(AutoSyncReadInterval != null) DecodeAll(); + ReadCompleted?.Invoke(this, e); + }; + _tag.WriteStarted += (s, e) => WriteStarted?.Invoke(this, e); + _tag.WriteCompleted += (s, e) => WriteCompleted?.Invoke(this, e); + _tag.Aborted += (s, e) => Aborted?.Invoke(this, e); + _tag.Destroyed += (s, e) => Destroyed?.Invoke(this, e); + } - /// - public string Path - { - get => _tag.Path; - set => _tag.Path = value; - } + /// + public Protocol? Protocol + { + get => _tag.Protocol; + set => _tag.Protocol = value; + } - /// - public PlcType? PlcType - { - get => _tag.PlcType; - set - { - _tag.PlcType = value; - if(value.HasValue) - _plcMapper.PlcType = value.Value; - } - } + /// + public string Gateway + { + get => _tag.Gateway; + set => _tag.Gateway = value; + } - /// - public string Name - { - get => _tag.Name; - set => _tag.Name = value; - } + /// + public string Path + { + get => _tag.Path; + set => _tag.Path = value; + } - /// - public bool? UseConnectedMessaging + /// + public PlcType? PlcType + { + get => _tag.PlcType; + set { - get => _tag.UseConnectedMessaging; - set => _tag.UseConnectedMessaging = value; + _tag.PlcType = value; + if(value.HasValue) + _plcMapper.PlcType = value.Value; } + } - /// - public bool? AllowPacking - { - get => _tag.AllowPacking; - set => _tag.AllowPacking = value; - } + /// + public string Name + { + get => _tag.Name; + set => _tag.Name = value; + } - /// - public int? ReadCacheMillisecondDuration - { - get => _tag.ReadCacheMillisecondDuration; - set => _tag.ReadCacheMillisecondDuration = value; - } + /// + public bool? UseConnectedMessaging + { + get => _tag.UseConnectedMessaging; + set => _tag.UseConnectedMessaging = value; + } - /// - public TimeSpan Timeout - { - get => _tag.Timeout; - set => _tag.Timeout = value; - } + /// + public bool? AllowPacking + { + get => _tag.AllowPacking; + set => _tag.AllowPacking = value; + } - /// - public TimeSpan? AutoSyncReadInterval - { - get => _tag.AutoSyncReadInterval; - set => _tag.AutoSyncReadInterval = value; - } + /// + public int? ReadCacheMillisecondDuration + { + get => _tag.ReadCacheMillisecondDuration; + set => _tag.ReadCacheMillisecondDuration = value; + } - /// - public TimeSpan? AutoSyncWriteInterval - { - get => _tag.AutoSyncWriteInterval; - set => _tag.AutoSyncWriteInterval = value; - } + /// + public TimeSpan Timeout + { + get => _tag.Timeout; + set => _tag.Timeout = value; + } - /// - public DebugLevel DebugLevel - { - get => _tag.DebugLevel; - set => _tag.DebugLevel = value; - } + /// + public TimeSpan? AutoSyncReadInterval + { + get => _tag.AutoSyncReadInterval; + set => _tag.AutoSyncReadInterval = value; + } - /// - public uint? MaxRequestsInFlight - { - get => _tag.MaxRequestsInFlight; - set => _tag.MaxRequestsInFlight = value; - } + /// + public TimeSpan? AutoSyncWriteInterval + { + get => _tag.AutoSyncWriteInterval; + set => _tag.AutoSyncWriteInterval = value; + } - /// - /// Dimensions of Value if it is an array - /// Ex. {2, 10} for a 2 column, 10 row array - /// Non-arrays can use null (default) - /// - public int[] ArrayDimensions - { - get => _plcMapper.ArrayDimensions; - set - { - _plcMapper.ArrayDimensions = value; - _tag.ElementCount = _plcMapper.GetElementCount(); - } - } + /// + public DebugLevel DebugLevel + { + get => _tag.DebugLevel; + set => _tag.DebugLevel = value; + } - /// - public void Initialize() - { - _tag.Initialize(); - DecodeAll(); - } + /// + public uint? MaxRequestsInFlight + { + get => _tag.MaxRequestsInFlight; + set => _tag.MaxRequestsInFlight = value; + } - /// - public async Task InitializeAsync(CancellationToken token = default) + /// + /// Dimensions of Value if it is an array + /// Ex. {2, 10} for a 2 column, 10 row array + /// Non-arrays can use null (default) + /// + public int[] ArrayDimensions + { + get => _plcMapper.ArrayDimensions; + set { - await _tag.InitializeAsync(token).ConfigureAwait(false); - DecodeAll(); + _plcMapper.ArrayDimensions = value; + _tag.ElementCount = _plcMapper.GetElementCount(); } + } - /// - public async Task ReadAsync(CancellationToken token = default) - { - await _tag.ReadAsync(token).ConfigureAwait(false); - DecodeAll(); - return Value; - } + /// + public void Initialize() + { + _tag.Initialize(); + DecodeAll(); + } - /// - public T Read() - { - _tag.Read(); - DecodeAll(); - return Value; - } + /// + public async Task InitializeAsync(CancellationToken token = default) + { + await _tag.InitializeAsync(token).ConfigureAwait(false); + DecodeAll(); + } - object ITag.Read() => Read(); + /// + public async Task ReadAsync(CancellationToken token = default) + { + await _tag.ReadAsync(token).ConfigureAwait(false); + DecodeAll(); + return Value; + } - async Task ITag.ReadAsync(CancellationToken token) => await ReadAsync().ConfigureAwait(false); + /// + public T Read() + { + _tag.Read(); + DecodeAll(); + return Value; + } - /// - public async Task WriteAsync(CancellationToken token = default) - { - if (!_tag.IsInitialized) - await _tag.InitializeAsync(token).ConfigureAwait(false); + object ITag.Read() => Read(); - EncodeAll(); - await _tag.WriteAsync(token).ConfigureAwait(false); - } + async Task ITag.ReadAsync(CancellationToken token) => await ReadAsync().ConfigureAwait(false); - /// - public async Task WriteAsync(T value, CancellationToken token = default) - { - Value = value; - await WriteAsync(token).ConfigureAwait(false); - } + /// + public async Task WriteAsync(CancellationToken token = default) + { + if (!_tag.IsInitialized) + await _tag.InitializeAsync(token).ConfigureAwait(false); - /// - public void Write() - { - if (!_tag.IsInitialized) - _tag.Initialize(); + EncodeAll(); + await _tag.WriteAsync(token).ConfigureAwait(false); + } - EncodeAll(); - _tag.Write(); - } + /// + public async Task WriteAsync(T value, CancellationToken token = default) + { + Value = value; + await WriteAsync(token).ConfigureAwait(false); + } - /// - public void Write(T value) - { - Value = value; - Write(); - } + /// + public void Write() + { + if (!_tag.IsInitialized) + _tag.Initialize(); - void DecodeAll() - { - Value = _plcMapper.Decode(_tag); - } + EncodeAll(); + _tag.Write(); + } - void EncodeAll() - { - _plcMapper.Encode(_tag, Value); - } + /// + public void Write(T value) + { + Value = value; + Write(); + } - /// - public Status GetStatus() => _tag.GetStatus(); + void DecodeAll() + { + Value = _plcMapper.Decode(_tag); + } - public void Dispose() => _tag.Dispose(); + void EncodeAll() + { + _plcMapper.Encode(_tag, Value); + } - ~Tag() - { - Dispose(); - } + /// + public Status GetStatus() => _tag.GetStatus(); - /// - /// The local memory value that can be transferred to/from the PLC - /// - public T Value { get; set; } - object ITag.Value { get => Value; set => Value = (T)value; } - public event EventHandler ReadStarted; - public event EventHandler ReadCompleted; - public event EventHandler WriteStarted; - public event EventHandler WriteCompleted; - public event EventHandler Aborted; - public event EventHandler Destroyed; + public void Dispose() => _tag.Dispose(); + ~Tag() + { + Dispose(); } -} + + /// + /// The local memory value that can be transferred to/from the PLC + /// + public T Value { get; set; } + object ITag.Value { get => Value; set => Value = (T)value; } + public event EventHandler ReadStarted; + public event EventHandler ReadCompleted; + public event EventHandler WriteStarted; + public event EventHandler WriteCompleted; + public event EventHandler Aborted; + public event EventHandler Destroyed; + +} \ No newline at end of file