libplctag is an open source C library for Linux, Windows and macOS using EtherNet/IP or Modbus TCP to read and write tags in PLCs.
libplctag.NET is a .NET wrapper for libplctag.
This repository contains two .NET packages that are published to Nuget.org:
Package | Downloads | Stable | Preview |
---|---|---|---|
libplctag | |||
libplctag.NativeImport |
This is the package intended for use in .NET applications. It depends on libplctag.NativeImport to gain access to the underlying libplctag native library.
It provides an API for libplctag that should feel natural to .NET developers by supporting the following features:
- Values are strongly-typed (both Atomic types and User-Defined Types).
- Errors are thrown as Exceptions
- Async/Await
- Native resource cleanup
Most developers will not need to directly reference the Native Import library. This library automatically extracts platform-specific version of the base libplctag library needed for the libplctag .NET wrapper.
If you wish to override this behavior you can do so: Using a non packaged version of the native libplctag library
Documentation for the base library API can be found here. Further examples of its usage can be found here.
The libplctag native library can be compiled for many platforms, and not all supported platforms are shipped with this wrapper. If you get a TypeLoadException
, chances are that you can still use this wrapper but you will need to supply the runtime yourself.
In most cases only the libplctag package will be needed. It can be added in Visual Studio through the package manager or via the commandline:
dotnet add package libplctag
// Instantiate the tag with the appropriate mapper and datatype
var myTag = new TagDint()
{
//Name is the full path to tag.
Name = "PROGRAM:SomeProgram.SomeDINT",
//Gateway is the IP Address of the PLC or communication module.
Gateway = "10.10.10.10",
//Path is the location in the control plane of the CPU. Almost always "1,0".
Path = "1,0",
PlcType = PlcType.ControlLogix,
Protocol = Protocol.ab_eip,
Timeout = TimeSpan.FromSeconds(5)
};
// Read the value from the PLC
int output = myTag.Read();
// Output to Console
Console.WriteLine($"Original value: SomeProgram.SomeDINT = {output}");
// Write a new value to the PLC then read it back
myTag.Write(37);
output = myTag.Read();
// Output to Console
Console.WriteLine($"Updated value: SomeProgram.SomeDINT = {output}");
In advanced scenarios, tags can be instantiated using generics (ex. Tag<DintPlcMapper, int>
, Tag<BoolPlcMapper, bool>
) and can be referenced via an ITag
interface.
For more detail and further usage, see the examples in the example projects:
UWP, Xamarin.Forms and some other frameworks produce executables that, when installed, can not modify their own application directory. libplctag.NativeImport relies on the ability to extract the native library to this location, so on these platforms, libplctag will not work.
The workaround is to supply the appropriate binary(ies) yourself:
- Get the native library (i.e. plctag.dll) from Releases.
- Add this file to your project such that it is copied to the output directory.
- Set
plctag.ForceExtractLibrary = false
before any other calls to libplctag.
Watch out for x64/x86 mismatches between the native library you downloaded and your solution.
This bug is tracked in libplctag#137
- Package the libplctag functionality in a way that is convenient to use in .NET applications.
- Be cross-platform: It should support any platform that libplctag can be built for, and supports .NET Standard 2.0
Please review the Contributions guidance.
libplctag.NET is part of the libplctag organization, so the same policies apply.