MASES.PLC4Net 0.12.0

Suggested Alternatives

MASES.PLCOnNet

Additional Details

Changed name to avoid trademarks issues

dotnet add package MASES.PLC4Net --version 0.12.0
                    
NuGet\Install-Package MASES.PLC4Net -Version 0.12.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MASES.PLC4Net" Version="0.12.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MASES.PLC4Net" Version="0.12.0" />
                    
Directory.Packages.props
<PackageReference Include="MASES.PLC4Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MASES.PLC4Net --version 0.12.0
                    
#r "nuget: MASES.PLC4Net, 0.12.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=MASES.PLC4Net&version=0.12.0
                    
Install MASES.PLC4Net as a Cake Addin
#tool nuget:?package=MASES.PLC4Net&version=0.12.0
                    
Install MASES.PLC4Net as a Cake Tool

title: Usage PLC4X suite for .NET _description: Describes how to use PLC4Net, set-up environment, identify the JVM and write good code

PLC4Net usage

To use PLC4Net classes the developer can write code in .NET using the same classes available in the official Java packages. If classes or methods are not available yet it is possible to use the approach synthetized in What to do if an API was not yet implemented

The following code snippets and examples are mutuated from PLC4X official website

Read data from PLC

The following code connects to a Siemens PLC using the s7 protocol and requests to read some tags:


const string connectionString = "s7://10.10.64.20";

using var plcConnection = PlcDriverManager.Default.ConnectionManager.GetConnection(connectionString);

if (!plcConnection.Metadata.IsReadSupported())
{
    Console.WriteLine("This connection doesn't support reading.");
    return;
}
// Create a new read request:
// - Give the single item requested an alias name
PlcReadRequest.Builder builder = plcConnection.ReadRequestBuilder();
builder.AddTagAddress("value-1", "%Q0.4:BOOL");
builder.AddTagAddress("value-2", "%Q0:BYTE");
builder.AddTagAddress("value-3", "%I0.2:BOOL");
builder.AddTagAddress("value-4", "%DB.DB1.4:INT");
PlcRequest readRequest = builder.Build();

var cf = readRequest.Execute<PlcReadResponse>();
var response = cf.Get();

foreach (Java.Lang.String tagName in response.TagNames)
{
    if (response.GetResponseCode(tagName) == PlcResponseCode.OK)
    {
        int numValues = response.GetNumberOfValues(tagName);
        // If it's just one element, output just one single line.
        if (numValues == 1)
        {
            Console.WriteLine($"Value[{tagName}]: {response.GetObject(tagName)}");
        }
        // If it's more than one element, output each in a single row.
        else
        {
            Console.WriteLine($"Value[{tagName}]:");
            for (int i = 0; i < numValues; i++)
            {
                Console.WriteLine($" - {response.GetObject(tagName, i)}");
            }
        }
    }
    // Something went wrong, to output an error message instead.
    else
    {
        Console.WriteLine($"Error[{tagName}]: {response.GetResponseCode(tagName).Name()}");
    }
}

Write data to PLC

The following code connects to a Siemens PLC using the s7 protocol and requests to write some tags:


const string connectionString = "s7://10.10.64.20";

using var plcConnection = PlcDriverManager.Default.ConnectionManager.GetConnection(connectionString);

if (!plcConnection.Metadata.IsWriteSupported())
{
    Console.WriteLine("This connection doesn't support writing.");
    return;
}
// Create a new write request:
// - Give the single item requested an alias name
// - Pass in the data you want to write (for arrays, pass in one value for every element)
PlcWriteRequest.Builder builder = plcConnection.WriteRequestBuilder();
builder.AddTagAddress("value-1", "%Q0.4:BOOL", true);
builder.AddTagAddress("value-2", "%Q0:BYTE", (byte)0xFF);
builder.AddTagAddress("value-4", "%DB.DB1.4:INT[3]", 7, 23, 42);
PlcRequest writeRequest = builder.Build();

var cf = writeRequest.Execute<PlcWriteResponse>();
var response = cf.Get();

foreach (Java.Lang.String tagName in response.TagNames)
{
    if (response.GetResponseCode(tagName) == PlcResponseCode.OK)
    {
        Console.WriteLine($"Value[{tagName}]: updated");
    }
    // Something went wrong, to output an error message instead.
    else
    {
        Console.WriteLine($"Error[{tagName}]: {response.GetResponseCode(tagName).Name()}");
    }
}

Subscribe to PLC

The following code connects to a Siemens PLC using the s7 protocol and subscribe to data change of some tags:


ConcurrentDictionary<int, PlcConsumerRegistration> _registrationMap = new();
Consumer<PlcSubscriptionEvent> _plcEvent = null;
const string connectionString = "s7://10.10.64.20";

using var plcConnection = PlcDriverManager.Default.ConnectionManager.GetConnection(connectionString);

if (!plcConnection.Metadata.IsSubscribeSupported())
{
    Console.WriteLine("This connection doesn't support subscribing.");
    return;
}
// Create a new subscription request:
// - Give the single tag requested an alias name
PlcSubscriptionRequest.Builder builder = plcConnection.SubscriptionRequestBuilder();
builder.AddChangeOfStateTagAddress("value-1", "{some address}");
builder.AddCyclicTagAddress("value-2", "{some address}", Duration.OfMillis(1000));
builder.AddEventTagAddress("value-3", "{some alarm address}");
PlcRequest subscriptionRequest = builder.Build();

var cf = subscriptionRequest.Execute<PlcSubscriptionResponse>();
var response = cf.Get();

_plcEvent ??= new Consumer<PlcSubscriptionEvent>()
{
    OnAccept = (e) =>
    {
        foreach (Java.Lang.String tagName in e.TagNames)
        {
            Console.WriteLine(e.GetPlcValue(tagName));
        }
    }
};

foreach (PlcSubscriptionHandle subscriptionHandle in response.SubscriptionHandles)
{
    var reg = subscriptionHandle.Register(_plcEvent);
    _registrationMap.GetOrAdd(reg.ConsumerId, (_) => reg); 
}

// wait many events then cleanup

foreach (var item in _registrationMap)
{
    item.Value.Unregister();
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.12.0 364 20 days ago 0.12.0 is deprecated because it is no longer maintained.