Git Product home page Git Product logo

s7plcrx's Introduction

License Build Nuget NuGet

Alt

S7PlcRx

S7PlcRx

Reactive S7 PLC Communications Library

Introduction

S7PlcRx is a library that provides a simple interface to communicate with Siemens S7 PLCs.

Features

  • Read and Write to PLC
  • Read from PLC with Reactive Subscription

Getting Started

Installation

S7PlcRx is available on NuGet.

In the Siemens PLC program you need to enable PUT/GET communication in the PLC settings. You will also need to set any DBs you want to access to be non-optimized.

Package Manager

Install-Package S7PlcRx

.NET CLI

dotnet add package S7PlcRx

Usage

Setup Tags and Observe values in PLC

using S7PlcRx;

var plc = new RxS7(S7PlcRx.Enums.CpuType.S71500, "PLC_IP_ADDRESS", 0, 5);
// Add Tag without Polling
plc.AddUpdateTagItem<double>("Tag0", "DB500.DBD0").SetTagPollIng(false);
// Add Tag with Polling
plc.AddUpdateTagItem<double>("Tag1", "DB500.DBD8");

plc.IsConnected
    .Where(x => x)
    .Take(1)
    .Subscribe(async _ =>
    {
        Console.WriteLine("Connected");

        // Read Tag Value manually
        var tag0 = await plc.Value<double>("Tag0");
    });

// Subscribe to Tag Values
plc.Observe<double>("Tag0").Subscribe(x => Console.WriteLine($"Tag0: {x}"));
plc.Observe<double>("Tag1").Subscribe(x => Console.WriteLine($"Tag1: {x}"));
// Start Polling on previously disabled Tag
plc?.GetTag("Tag0")?.SetTagPollIng(true);

You can also use the S7xxxx static classes to setup the PLC. For example, S7PlcRx.S71500.Create(...... to setup a S7-1500 PLC.

Add Tag without Generic Type

plc.AddUpdateTagItem(typeof(double), "Tag0", "DB500.DBD0");

Add Tag with Chainable Options

plc.AddUpdateTagItem<double>("Tag0", "DB500.DBD0").SetTagPollIng(false)
   .AddUpdateTagItem(typeof(double), "Tag1", "DB500.DBD8");

Remove Tag

plc.RemoveTagItem("Tag0");

SetPolling

Polling is enabled by default when a Tag is added. You can disable and enable polling on a Tag at any time.

// Create a new Tag and with no Polling
plc.AddUpdateTagItem<double>("Tag0", "DB500.DBD0").SetTagPollIng(false);

// Set Polling on Tag
plc?.GetTag("Tag0")?.SetTagPollIng(true);

// Stop Polling on Tag
plc?.GetTag("Tag0")?.SetTagPollIng(false);

Write to PLC

plc.Value<double>("Tag0", 1.0);

Read PLC CPU Info

// Get CPU Info from PLC Async
var cpuInfo = await plc.CpuInfo();

// Get CPU Info from PLC Reactive
plc.GetCpuInfo().Subscribe(info =>
    {
    });

This returns a CpuInfo string Array with the following values: AS Name, Module Name, Copyright, Serial Number, Module Type Name, Order Code, Version.

Using the Watchdog

The Watchdog is a feature that writes a value to a specific address in the PLC at a set interval. This can be used to keep the PLC alive and prevent it from going into stop mode.

The functionallity of the PLC is down to the user to implement.

The recommended way to use the Watchdog is to use a timer to decrement a value in the PLC. If the value reaches 0 then the PLC can be stopped. The Watchdog will always write a value back to the PLC to reset the timer and resume normal operation.

var plc = new RxS7(S7PlcRx.Enums.CpuType.S71500, "PLC_IP_ADDRESS", 0, watchDogAddress, 5, watchDogValueToWrite, watchDogInterval);

The Watchdog is disabled by default. To enable the Watchdog set the watchDogAddress, it must be an address of type DBW. The watchDogInterval is the time in seconds between each write and the watchDogValueToWrite is the value to write to the PLC.

Supported Data Types

  • Bool - DB?.DBX?.?
  • Byte - DB?.DBB?
  • Byte[] - DB?.DBB? set length to number of bytes when creating array tags - example AddUpdateTagItem<byte[]>(PlcData, "DB100.DBB0", 64) - creates a tag with 64 bytes
  • Int - DB?.DBW?
  • Int[] - DB?.DBW?
  • UInt - DB?.DBW?
  • UInt[] - DB?.DBW?
  • DInt - DB?.DBD?
  • DInt[] - DB?.DBD?
  • UDInt - DB?.DBD?
  • UDInt[] - DB?.DBD?
  • Real - DB?.DBD?
  • Real[] - DB?.DBD?
  • LReal - DB?.DBD?
  • LReal[] - DB?.DBD?
  • String - TODO - for the time being use a Byte Array
  • Word - DB?.DBW?
  • Word[] - DB?.DBW?
  • DWord - DB?.DBD?
  • DWord[] - DB?.DBD?

Further types will be added in the future.

Supported PLCs

  • Logo-0BA8
  • S7-200
  • S7-300
  • S7-400
  • S7-1200
  • S7-1500

s7plcrx's People

Contributors

chrispulman avatar dependabot[bot] avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

s7plcrx's Issues

consider making AddUpdateTagItem public

Hi, thx for your wonderful library.

I see RxS7 can add tags by this extension method

public static class ExtensionsMixins
{
    public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string address, int? arrayLength = null)
    {

However, sometimes the tag type is unknown at compile time. In that case, I can't add tag without hack (e.g. using reflection).

RxS7 does have an method that can add tag in which the concrete type is not required at compile time. Unfortunately, that is internal:

   internal void AddUpdateTagItem(Tag tag)
  • Is it suitable to make the internal AddUpdateTagItem(Tag tag) as well as the RemoveTagItem(string tagName) method public ?
  • Or, is it suitable to add two extension methods that pass the AddUpdateTagItem<T> in the form of AddUpdateTagItem(T t)? e.g.:
    • AddUpdateTagItem(this IRxS7 @this, Type type ,string tagName, string address, int? arrayLength = null)
    • (ITag? tag, IRxS7? plc) AddUpdateTagItem(this (ITag? _, IRxS7? plc) @this, Type type ,string tagName, string address, int? arrayLength = null)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.