Skip to content

Commit

Permalink
Merge pull request #19 from libplctag/new-API
Browse files Browse the repository at this point in the history
Expose all new APIs
  • Loading branch information
timyhac committed Jul 5, 2020
2 parents d73fa3d + 95455cb commit c61f68a
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

namespace ExampleConsoleApp
{
class Program
class Example
{
static void Main(string[] args)
public static void Run()
{

var myTag = new Tag(IPAddress.Parse("10.10.10.10"), "1,0", CpuTypes.LGX, DataTypes.DINT, "PROGRAM:SomeProgram.SomeDINT");
while (myTag.GetStatus() == StatusCode.PLCTAG_STATUS_PENDING)
{
Expand Down Expand Up @@ -37,7 +36,6 @@ static void Main(string[] args)


Console.WriteLine(myDint);

}
}
}
}
File renamed without changes.
132 changes: 132 additions & 0 deletions src/ExampleConsoleApp/NativeImportExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Threading;
using libplctag.NativeImport;

namespace ExampleConsoleApp
{
class NativeImportExample
{
public static void Run()
{

var tagHandle = plctag.create("protocol=ab_eip&gateway=192.168.0.10&path=1,0&cpu=LGX&elem_size=4&elem_count=1&name=MY_DINT", 1000);

while (plctag.status(tagHandle) == 1)
{
Thread.Sleep(100);
}
var statusBeforeRead = plctag.status(tagHandle);
if (statusBeforeRead != 0)
{
Console.WriteLine($"Something went wrong {statusBeforeRead}");
}

plctag.read(tagHandle, 1000);
while (plctag.status(tagHandle) == 1)
{
Thread.Sleep(100);
}
var statusAfterRead = plctag.status(tagHandle);
if (statusAfterRead != 0)
{
Console.WriteLine($"Something went wrong {statusAfterRead}");
}

var theValue = plctag.get_uint32(tagHandle, 0);

plctag.destroy(tagHandle);

Console.WriteLine(theValue);
}

public static void RunCallbackExample()
{

var tagHandle = plctag.create("protocol=ab_eip&gateway=192.168.0.10&path=1,0&cpu=LGX&elem_size=4&elem_count=1&name=MY_DINT", 1000);

while (plctag.status(tagHandle) == 1)
{
Thread.Sleep(100);
}
var statusBeforeRead = plctag.status(tagHandle);
if (statusBeforeRead != 0)
{
Console.WriteLine($"Something went wrong {statusBeforeRead}");
}

var myCallback = new plctag.callback_func(MyCallback);
var statusAfterRegistration = plctag.register_callback(tagHandle, myCallback);
if (statusAfterRegistration != 0)
{
Console.WriteLine($"Something went wrong {statusAfterRegistration}");
}

plctag.read(tagHandle, 1000);
while (plctag.status(tagHandle) == 1)
{
Thread.Sleep(100);
}
var statusAfterRead = plctag.status(tagHandle);
if (statusAfterRead != 0)
{
Console.WriteLine($"Something went wrong {statusAfterRead}");
}

var theValue = plctag.get_uint32(tagHandle, 0);

plctag.destroy(tagHandle);

Console.WriteLine(theValue);
}

public static void MyCallback(int tag_id, int event_id, int status)
{
Console.WriteLine($"Tag Id: {tag_id} Event Id: {event_id} Status: {status}");
}

public static void RunLoggerExample()
{
var myLogger = new plctag.log_callback_func(MyLogger);
var statusAfterRegistration = plctag.register_logger(myLogger);
if (statusAfterRegistration != 0)
{
Console.WriteLine($"Something went wrong {statusAfterRegistration}");
}

var tagHandle = plctag.create("protocol=ab_eip&gateway=192.168.0.10&path=1,0&cpu=LGX&elem_size=4&elem_count=1&name=MY_DINT&debug=4", 1000);

while (plctag.status(tagHandle) == 1)
{
Thread.Sleep(100);
}
var statusBeforeRead = plctag.status(tagHandle);
if (statusBeforeRead != 0)
{
Console.WriteLine($"Something went wrong {statusBeforeRead}");
}

plctag.read(tagHandle, 1000);
while (plctag.status(tagHandle) == 1)
{
Thread.Sleep(100);
}
var statusAfterRead = plctag.status(tagHandle);
if (statusAfterRead != 0)
{
Console.WriteLine($"Something went wrong {statusAfterRead}");
}

var theValue = plctag.get_uint32(tagHandle, 0);

plctag.destroy(tagHandle);

Console.WriteLine(theValue);

}

public static void MyLogger(int tag_id, int debug_level, string message)
{
Console.WriteLine($"Tag Id: {tag_id} Debug Level: {debug_level} Message: {message}");
}
}
}
17 changes: 17 additions & 0 deletions src/ExampleConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace ExampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
Example.Run();
NativeImportExample.Run();
NativeImportExample.RunCallbackExample();
NativeImportExample.RunLoggerExample();

Console.Read();
}
}
}
32 changes: 26 additions & 6 deletions src/libplctag.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,44 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTest", "ConsoleTest\ConsoleTest.csproj", "{5C5D581C-2979-4995-A837-F5EE210FC751}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "libplctag", "libplctag\libplctag.csproj", "{A488D8F2-712F-440E-9024-F27A75FEB171}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleConsoleApp", "ExampleConsoleApp\ExampleConsoleApp.csproj", "{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C5D581C-2979-4995-A837-F5EE210FC751}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C5D581C-2979-4995-A837-F5EE210FC751}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C5D581C-2979-4995-A837-F5EE210FC751}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C5D581C-2979-4995-A837-F5EE210FC751}.Release|Any CPU.Build.0 = Release|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Debug|x64.ActiveCfg = Debug|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Debug|x64.Build.0 = Debug|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Debug|x86.ActiveCfg = Debug|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Debug|x86.Build.0 = Debug|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Release|Any CPU.Build.0 = Release|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Release|x64.ActiveCfg = Release|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Release|x64.Build.0 = Release|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Release|x86.ActiveCfg = Release|Any CPU
{A488D8F2-712F-440E-9024-F27A75FEB171}.Release|x86.Build.0 = Release|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Debug|x64.ActiveCfg = Debug|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Debug|x64.Build.0 = Debug|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Debug|x86.ActiveCfg = Debug|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Debug|x86.Build.0 = Debug|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Release|Any CPU.Build.0 = Release|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Release|x64.ActiveCfg = Release|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Release|x64.Build.0 = Release|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Release|x86.ActiveCfg = Release|Any CPU
{0ED1BEC4-113F-4EEE-AAD9-EBCD745E68BA}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 12 additions & 0 deletions src/libplctag/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace libplctag
{
public enum Events
{
PLCTAG_EVENT_READ_STARTED = 1,
PLCTAG_EVENT_READ_COMPLETED = 2,
PLCTAG_EVENT_WRITE_STARTED = 3,
PLCTAG_EVENT_WRITE_COMPLETED = 4,
PLCTAG_EVENT_ABORTED = 5,
PLCTAG_EVENT_DESTROYED = 6
}
}
22 changes: 22 additions & 0 deletions src/libplctag/Exceptions/BusyException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace libplctag
{

public class BusyException : Exception
{
public BusyException()
{
}

public BusyException(string message)
: base(message)
{
}

public BusyException(string message, Exception inner)
: base(message, inner)
{
}
}
}
22 changes: 22 additions & 0 deletions src/libplctag/Exceptions/PartialException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace libplctag
{

public class PartialException : Exception
{
public PartialException()
{
}

public PartialException(string message)
: base(message)
{
}

public PartialException(string message, Exception inner)
: base(message, inner)
{
}
}
}
4 changes: 3 additions & 1 deletion src/libplctag/StatusCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public enum StatusCode
PLCTAG_ERR_TOO_SMALL = -34,
PLCTAG_ERR_UNSUPPORTED = -35,
PLCTAG_ERR_WINSOCK = -36,
PLCTAG_ERR_WRITE = -37
PLCTAG_ERR_WRITE = -37,
PLCTAG_ERR_PARTIAL = -38,
PLCTAG_ERR_BUSY = -39,
}
}
2 changes: 2 additions & 0 deletions src/libplctag/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public void ThrowIfError()
case StatusCode.PLCTAG_ERR_UNSUPPORTED: throw new libplctag.UnsupportedException();
case StatusCode.PLCTAG_ERR_WINSOCK: throw new libplctag.WinsockException();
case StatusCode.PLCTAG_ERR_WRITE: throw new libplctag.WriteException();
case StatusCode.PLCTAG_ERR_PARTIAL: throw new libplctag.PartialException();
case StatusCode.PLCTAG_ERR_BUSY: throw new libplctag.BusyException();
default: throw new System.NotImplementedException();
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/libplctag/libplctag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,46 @@ static plctag()
LibraryExtractor.Init();
}


[DllImport(DLL_NAME, EntryPoint = "plc_tag_check_lib_version", CallingConvention = CallingConvention.Cdecl)]
public static extern int check_lib_version(int req_major, int req_minor, int req_patch);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_create", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 create([MarshalAs(UnmanagedType.LPStr)] string lpString, int timeout);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_destroy", CallingConvention = CallingConvention.Cdecl)]
public static extern int destroy(Int32 tag);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_shutdown", CallingConvention = CallingConvention.Cdecl)]
public static extern int shutdown();


[DllImport(DLL_NAME, EntryPoint = "plc_tag_register_callback", CallingConvention = CallingConvention.Cdecl)]
public static extern int register_callback(Int32 tag_id, callback_func func);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_unregister_callback", CallingConvention = CallingConvention.Cdecl)]
public static extern int unregister_callback(Int32 tag_id);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callback_func(Int32 tag_id, Int32 event_id, Int32 status);


[DllImport(DLL_NAME, EntryPoint = "plc_tag_register_logger", CallingConvention = CallingConvention.Cdecl)]
public static extern int register_logger(log_callback_func func);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_unregister_logger", CallingConvention = CallingConvention.Cdecl)]
public static extern int unregister_logger(Int32 tag_id);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void log_callback_func(Int32 tag_id, int debug_level, [MarshalAs(UnmanagedType.LPStr)] string message);


[DllImport(DLL_NAME, EntryPoint = "plc_tag_lock", CallingConvention = CallingConvention.Cdecl)]
public static extern int plc_tag_lock(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = "plc_tag_unlock", CallingConvention = CallingConvention.Cdecl)]
public static extern int plc_tag_unlock(Int32 tag);


[DllImport(DLL_NAME, EntryPoint = "plc_tag_status", CallingConvention = CallingConvention.Cdecl)]
public static extern int status(Int32 tag);

Expand All @@ -39,6 +73,14 @@ static plctag()
[DllImport(DLL_NAME, EntryPoint = "plc_tag_abort", CallingConvention = CallingConvention.Cdecl)]
public static extern int abort(Int32 tag);


[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_int_attribute", CallingConvention = CallingConvention.Cdecl)]
public static extern int get_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int default_value);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_int_attribute", CallingConvention = CallingConvention.Cdecl)]
public static extern int set_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int new_value);


[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_uint64", CallingConvention = CallingConvention.Cdecl)]
public static extern UInt64 get_uint64(Int32 tag, int offset);

Expand Down Expand Up @@ -99,5 +141,11 @@ static plctag()
[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_int8", CallingConvention = CallingConvention.Cdecl)]
public static extern int set_int8(Int32 tag, int offset, sbyte val);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_get_bit", CallingConvention = CallingConvention.Cdecl)]
public static extern int get_bit(Int32 tag, int offset_bit);

[DllImport(DLL_NAME, EntryPoint = "plc_tag_set_bit", CallingConvention = CallingConvention.Cdecl)]
public static extern int set_bit(Int32 tag, int offset_bit, int val);

}
}

0 comments on commit c61f68a

Please sign in to comment.