Coder Social home page Coder Social logo

0000duck / dacs7 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from proemmer/dacs7

0.0 1.0 0.0 2.85 MB

dotnet core library to connect to s7 plcs

License: Apache License 2.0

C# 48.28% PowerShell 0.01% JavaScript 3.62% CSS 10.17% HTML 37.91%

dacs7's Introduction

dacs7

Data access S7 is a library to connect to S7 plcs for reading and writing data.

NuGet

PM>  Install-Package Dacs7

Description

Dacs7 is used to connect to a SIEMENS Plc by using the RFC1006 protocol to perform operations.

Sample-Code

Open and close a connection to the PLC

//create an instance of the client   [IP]:[Port],[Rack],[Slot]
var client = new Dacs7Client("127.0.0.1:102,0,2");

//connect to the plc. If the connection could not be established
//you will get an Dacs7NotConnectedException here.
await client.ConnectAsync();

...

//Check if the client is connected. If yes, than close the connection.
if(client.IsConnected)
    await client.DisconnectAsync();

Using tag methods

This methods are created to address data a given a tag string. All the relevant information will be parsed from this string.

Syntax

Area.Offset,DataType[,Length]

  • Areas: dacs7 supports english and german mnemonic and also its own syntax (The input is not case sensitive)
    • Inputs: i, e, ib
    • Marker: m, f
    • Outputs: q, a, qb
    • Timer: t, tm
    • Counter: c, z, ct
    • Data Blocks: DB[number]
  • Offset: The offset in byte from the beginning of the specified area.
  • DataType:
    • x[bit]: Boolean using a bit you also have to specify the bit number in the datatype part.
    • b: Byte
    • w: Word (ushort)
    • dw: DWord (uint)
    • lw: LongWord (ulong)
    • si: SmallInt (sbyte)
    • i: Int (short)
    • di: DInt (int)
    • li: LongInt (long)
    • r: Real (float)
    • c: Char
    • wc: WChar (string)
    • s: String
    • ws: WString (string)
  • Length: This part is optional, the default value is 1. (a special case is the string type, this specifies the length of the string, so it is currently not possible to read a string array in one command);

Write Results

For each write operation you will get an ItemResponseRetValue.

    public enum ItemResponseRetValue : byte
    {
        Reserved = 0x00,
        [Description("Hardware error")]
        HardwareFault = 0x01,

        [Description("Accessing the object not allowed")]
        AccessFault = 0x03,

        [Description("Invalid address")]
        OutOfRange = 0x05,       //the desired address is beyond limit for this PLC 

        [Description("Data type not supported")]
        NotSupported = 0x06,     //Type is not supported 

        [Description("Data type inconsistent")]
        SizeMismatch = 0x07,     //Data type inconsistent 

        [Description("Object does not exist")]
        DataError = 0x0a,        //the desired item is not available in the PLC, e.g. when trying to read a non existing DB

        [Description("Success")]
        Success = 0xFF,
    }

Read Results

For each read operation you will get and DataValue.

    public class DataValue
    {
        ItemResponseRetValue ReturnCode { get; }
        bool IsSuccessReturnCode { get; }
        Type Type { get; }
        Memory<byte> Data { get; }
        object Value { get; };
        T GetValue<T>()

    }

Read and Write byte data

var testData1 = new byte[100];
var testData2 = new byte[500];

//Write an array of bytes to the PLC by using the tag syntax
var writeResult1 = await _client.WriteAsync(WriteItem.CreateFromTag("DB1114.0,b,100", testData1), 
                                           WriteItem.CreateFromTag("DB1114.100,b,500", testData2));

//Read an array of bytes from the PLC by using the tag syntax
var readResults1 = await _client.ReadAsync(ReadItem.CreateFromTag("DB1114.0,b,100"), 
                                          ReadItem.CreateFromTag("DB1114.100,b,500"));

//Write an array of bytes to the PLC 
var writeResult2 = await _client.WriteAsync(WriteItem.Create("DB1114",0, testData1), 
                                           WriteItem.Create("DB1114",100, testData2));

//Read an array of bytes from the PLC 
var readResults2 = await _client.ReadAsync(ReadItem.Create<byte[]>("DB1114", 0, 100), 
                                          ReadItem.Create<byte[]>("DB1114", 100, 500));

Read and Write bit data

The offset is normally in bytes, but if you address bools, you have to pass the address in bits (byteoffset * 8 + bitoffset)

var readResults = await client.ReadAsync(ReadItem.Create<bool>(datablock, baseOffset),
                                         ReadItem.Create<bool>(datablock, baseOffset + 5))

await client.WriteAsync(WriteItem.Create(datablock, baseOffset, true),
                        WriteItem.Create(datablock, baseOffset + 5, true))

Read and Write string data

IF the given type is a string or char you can also specify if its the Unicode variant of them (this means 2byte per sign). PlcEncoding can be Acsii or Unicode. Unicode is only supported in TIA to address WString an WChar.

var readResults = await client.ReadAsync(ReadItem.Create<string>(datablock, 0, 10, PlcEncoding.Ascii))

await client.WriteAsync(WriteItem.Create(datablock, baseOffset, "TEST      ", PlcEncoding.Ascii))

Compatibility

300 400 WinAC 1200 1500
DB Read/Write X X X X X
EB Read/Write X X X X X
AB Read/Write X X X X X
MB Read/Write X X X X X
TM Read/Write X X X
CT Read/Write X X X

Additional TIA Settings (1200 and 1500 CPUs)

DB Properties

Select the DB in the left pane under 'Program blocks' and click 'Properties' in the context menu.

FullAccess

Select the CPU in the left pane and click 'Properties' in the context menu and go to 'Protection & Security'.

Connection mechanisms

Select the CPU in the left pane and click 'Properties' in the context menu and go to 'Protection & Security/Connection mechanisms'.

dacs7's People

Contributors

karstengorkow avatar proemmer avatar

Watchers

 avatar

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.