Home / PLC / Communication with Siemens S7 Plc with C# and S7.Net plc driver

Communication with Siemens S7 Plc with C# and S7.Net plc driver

In this article I explain how to implement a Siemens S7 plc driver by using the open source driver S7.Net.

You can find S7.Net sources on GitHub: https://github.com/killnine/s7netplus.

How to video

Summary

Why S7.Net: features and capabilities of the driver

S7.Net is a plc driver written in C#, this means that you don’t have to handle any interoperability with native code, but it just use OO programming and all the .Net features that you are familiar with.

Basic capabilities:
• Can connect and disconnect with the plc using sockets
• Can read and write bytes from a single area of memory, given a starting address and the number of bytes.

High level features:
• Can directly map DBs to classes and structs
• The types of C# are mapped into types of S7 and there are converters for every type (double to REAL, int to DEC, etc)
• It is easy to use, well written and perfectly readable
• It’s open source, MIT license permit you to use it in every commercial application
• Did I already say that it’s written in C#, no interop at all?

What it’s not good about S7.Net, roadmap for future upgrades:
• Lack of documentation
• Lack of a function that permit to read/write multiple non-connected variables with a single request to the plc.

Documentation on the driver and S7 protocol

The documentation of S7.Net is here.

S7.Net exposes a class called Plc that contains all the methods that we can use to communicate with the plc:

public class Plc 
{
    public string IP { get; set; }
    public bool IsConnected { get; }
    public CpuType CPU { get; set; }
    public Int16 Rack { get; set; }
    public Int16 Slot { get; set; }
    public string Name { get; set; }
    public object Tag { get; set; }
    public bool IsAvailable { get; }
    public ErrorCode Open(){...}
    public void Close(){...}
    public byte[] ReadBytes(DataType dataType, int DB, int startByteAdr, int count){...}
    public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount){...}
    public object Read(string variable){...}
    public object ReadStruct(Type structType, int db){...}
    public void ReadClass(object sourceClass, int db){...}
    public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value){...}
    public object Write(DataType dataType, int db, int startByteAdr, object value){...}
    public object Write(string variable, object value){...}
    public ErrorCode WriteStruct(object structValue, int db){...}
    public ErrorCode WriteClass(object classValue, int db){...}
    public string LastErrorString { get; }
    public ErrorCode LastErrorCode { get; }
}

To connect and disconnect you can use the Open() and Close() functions, to communicate you can use any of the methods to read and write variables from the plc memory.

Every method return an ErrorCode, or the object that it’s supposed to return if there are no errors.

Unfortunately this is quite a messy concept, because usually drivers throws exceptions in case of errors, or returns a value indicating the error and put the requested values inside a memory area passing a pointer to that area.

Here there is the documentation of the protocol, in german: http://www.bj-ig.de/service/verfuegbare-dokumentationen/s7-kommunikation/index.html

Some more documentation about the S7 protocol can be found at the snap7 website: http://snap7.sourceforge.net/ or inside the package of Snap7 full (latest full: http://sourceforge.net/projects/snap7/files/1.2.1/ )

Also some documentation on S7 protocol can be found inside LibNoDave.

Getting started with S7.Net

This paragraph explains how to get S7.Net driver and all the steps from compile the driver to add it into your application.

You can choose to add download it from NuGet (fastest way) or to include the full sources in your project.

The steps to download from NuGet are:

1) Right click on the project and select “Manage NuGet packages” NuGet 1

2) Search for S7.Net in the online tab, then install it.
NuGet 2

3) Repeat for every project where you need the dll.

These steps are to include the sources in your application:

1) Download the driver from the Github repository https://github.com/killnine/s7netplus.
S7 net download

2) Copy the S7.Net project folder in the folder that contain your project

3) Add the S7.Net project to the solution

4) Add the references

S7 Net solution reference

A simple application with S7.Net

I know that create an application to showcase the use of the driver is difficult and will not meet everyone requirements, that’s why i tried to keep it as simple as possible, just to show how to create a PLC object, how to handle a polling to refresh the data read from the PLC and how to visualize the data around the application in a simple way. Also how to write data to the PLC.

The sample application is a WPF application that can connect and disconnect from the PLC, read and write the DB1 variables on the PLC and visualize the values.

The application contains 2 projects, one is a wrapper for the plc driver, the other one is a project that contains all the graphic pages.

You can download the sample application on GitHub: https://github.com/mesta1/S7.Net-example.

S7 Net UI example

Creating a wrapper of S7.Net

Using a wrapper for the plc driver is a strategy that I usually use to implement features that the plc driver doesn’t contain, and to abstract the most of the code required to interface the application with the plc driver.

The wrapper exposes an interface IPlcSyncDriver that contains all the methods needed to communicate with the PLC:

  • connect/disconnect and connection state
  • read/write single and multiple variables, called Tags (a Tag is an object that contains an address and a value)
  • read/write a class (special feature of the S7.Net protocol)
public interface IPlcSyncDriver
{        
    ConnectionStates ConnectionState { get; }    
    void Connect(); 
    void Disconnect();      
    List<Tag> ReadItems(List<Tag> itemList); 
    void ReadClass(object sourceClass, int db); 
    void WriteItems(List<Tag> itemList); 
}   

As you can see this methods are less and differents from the ones named inside the IPlc interface of the driver, and the reason is because I handle the communication errors by throwing exceptions, so the returned values can be just the objects that i need.

Also I just use the highest level features of the driver, and handle all the complexity inside the wrapper.

I use the concept of Tags inside my wrapper, where a Tag is an object containing an address and a value. This is familiar if you already used OPC Servers and Clients, but here it is much more simple and basic.

Creation of PLC class inside the main project

In the main project I usually define the class that contains the PLC values and communication thread in a singleton.

You can read more about it in this article:
https://www.mesta-automation.com/writing-your-first-hmi-project-in-c-wpf/

You can find the plc class inside PlcConnectivity folder.

The class exposes the properties and methods that are used in all application to communicate with the PLC:

public ConnectionStates ConnectionState 
public DB1 Db1  
public TimeSpan CycleReadTime 

public void Connect(string ipAddress)
public void Disconnect()
public void Write(string name, object value)
public void Write(List<Tag> tags)

Inside the class there is a multi-threaded timer that will poll the plc once every 100 ms (see constructor).

The timer callback is responsible to refresh the tags and to calculate the time passed after every read.

You can of course use multiple timers, with different Interval value, to better manage the network resources.

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{            
    if (plcDriver == null || plcDriver.ConnectionState != ConnectionStates.Online)
    {
        return;
    }
    timer.Enabled = false;
    CycleReadTime = DateTime.Now - lastReadTime;
    try            
    {                
        RefreshTags();
    }            
    finally 
    {
        timer.Enabled = true;
        lastReadTime = DateTime.Now;
    }
}

To read values from the PLC I use the feature that read a class directly from a DB.

private void RefreshTags()
{
    plcDriver.ReadClass(Db1, 1);
}

Write values on the PLC

To write the variables I use the method that permit to write a single object by giving an address and a value:

	
Plc.Instance.Write(PlcTags.DwordVariable, dwordVar);

You can read an example of write and read in the handlers of the MainWindow page.

PLC data visualization

To visualize the values I used a DispatcherTimer (but you can use also MVVM and DataBinding):

void timer_Tick(object sender, EventArgs e)
{
    btnConnect.IsEnabled = Plc.Instance.ConnectionState == ConnectionStates.Offline;
    btnDisconnect.IsEnabled = Plc.Instance.ConnectionState != ConnectionStates.Offline;
    lblConnectionState.Text = Plc.Instance.ConnectionState.ToString();
    ledMachineInRun.Fill = Plc.Instance.Db1.BitVariable0 ? Brushes.Green : Brushes.Gray;
    lblSpeed.Content = Plc.Instance.Db1.IntVariable;
    lblTemperature.Content = Plc.Instance.Db1.RealVariable;
    lblAutomaticSpeed.Content = Plc.Instance.Db1.DIntVariable;
    lblSetDwordVariable.Content = Plc.Instance.Db1.DWordVariable;
    // statusbar
    lblReadTime.Text = Plc.Instance.CycleReadTime.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
}

Sample code and testing

You can download the sample application on GitHub: https://github.com/mesta1/S7.Net-example.

You can also download the sample application for plc created with Step7 5.5: S7_pro1

To test the application you can use Step7 PLCSim, the guide to connect to it via NetToPlcSim is here: https://www.mesta-automation.com/nettoplcsim-how-to-connect-step-7-plc-sim-to-scadas/

176 comments

  1. Hey Mesta, thanks for the article. As the owner of the S7NetPlus project, I want to throw out the obligatory “I accept pull requests” for anyone who is willing to devote some time to improving the project.

    Also, I want to mention that this is actually a re-upload of the original S7Net project on CodePlex. The owner of that project gave me his blessing to fork it over to Github and pick up where he left off. He’s since moved on and wont be making changes.

    I’ve been pulled off this project for the immediate future but hope to get back to it in the spring. Prior to that, I’ll try to improve the ease of adoption by creating a NuGet package of this project. That way you don’t need to compile it yourself to use it.

    Create an issue on the GitHub page with any other requests, questions, or comments. Thanks!

    • I’m glad that you liked the article. If you are familiar with NuGet, it will be a really good addition, because as you can see the biggest pain is to compile and add S7.Net to a C# project.
      I have some small enhancement to push, but I need some time for tests. Also i’m looking on adding some documentation.
      When i will have some update, i’ll make pull request.

    • That’s great news! I will update the article as soon as possible.
      Article and sources are updated with NuGet package.

  2. Hello,

    Is there anyway to generate OnChanged events on the tags, or do you need to periodically poll the PLC for new values?

    I thought the s7 PLCs had something to allow this but im not sure.

    • With this driver you have to periodically poll the plc. You have to implement your OnChanged event in your code.
      If you check Snap7 driver, there is a Partner configuration, but I didn’t spend much time exploring that driver.

  3. Nitin Majgaonkar

    Thanks for detailed explaination and S7.net
    I followed all mentioned steps but unfortunately got stuck at one point. I continuously get one warning message
    – The referenced component ‘S7.Net’ could not be found. [S7NetWrapper]
    Request you please let me know the reason behind it.

    Thanks in advance.

    Regards,
    NM

    • It’s because you have to restore NuGet package. Right click on solution, select “Manage NuGet package for solution”, push “Restore” button in the up-right part of the window that pop out.
      Then recompile the solution and it should work.

  4. Hi, mesto. i have a question on this step: 1) Right click on the project and select “Manage NuGet packages” hwo can i find this project?

    • In the menu on top of Visual Studio 2013, there is File, Edit, View, Project, …
      Then click on Project and near the bottom there is Manage NuGet packages.
      You can do this also in Solution Explorer.

      • Thanks for your reply. I have downloaded the driver. I have a case here, what should I do if visual studio and Step 7 in two computers? Thanks!

        • You don’t need step7 installed to make it work, and if you don’t have Visual Studio installed, you can just take the compiled binaries and run the software in the pc that has Step7.

        • Thanks. But where is the compiled binaries? Could you send me the link? Appreciate for your help!

  5. Hi, Mr. Mesto.

    1. PLC simulator. When I open the project, open plcsim, download. it’s running. But when monitor the DB1 block, no value change? should I input some variable(tried to insert bit memory, not work)?

    2. I have installed visual studio express 2013. when running your app., on the HMI, click ”connect”, it says ”connection error”.

    Thanks!

    • Hello,
      1. Siemens PLCSim doesn’t expose a TCP/IP interface. If you are using Step7 you can use this program: https://www.mesta-automation.com/nettoplcsim-how-to-connect-step-7-plc-sim-to-scadas/

      2. I don’t support Visual studio 2013 express, you should download Visual Studio 2013 community edition. About the communication error, you have to debug your solution step by step, probably adding the sources of S7.Net, instead of the NuGet package.

      • Thanks for reply.

        1. I have followed your link. After setup a local 127.0.0.1 and restart server(Nettoplcsim-S7o-v-0-9-5), the connection error is gone, but no data got from PLCsim(PLCsim is running and I can see there are always 0 for all the four parameters in DB1).

        Another question: what is the usage of this package: Nettoplcsim-S7o-v-0-9-5-src?

        Thanks.

  6. Hi.

    I did not see it listed, but I was wondering if this also works on the siemens softPLC, or just on hw PLCs?
    WIinAC RTX for example?

    Great article by the way.

    • Hi, this is an implementation of S7 protocol, so if your plcs use this protocol to communicate with Step7 or TIA Portal, you can talk to them.

      • i notice in the code that we have to specify cpu type.
        200/300/400 etc.

        any idea how this would work with a softplc?
        in simatic manager main window, where you insert plc type, the softplc is under pc station.

        • If the soft plc has an IP address, then you can just try to connect it by using the S7-300, that it’s the default.

  7. Hello,

    I could make the connection with the plc.

    But I do not understand how to read or write information from the application that gave the example no matter the value set data always gets me Error not found

    • My plc is S7-1500er i can connected but when i tried to put start always enter in the throw validation of:

      object value = tag.ItemValue;
      if (tag.ItemValue is double)
      {
          var bytes = S7.Net.Types.Double.ToByteArray((double)tag.ItemValue);
          value = S7.Net.Types.DWord.FromByteArray(bytes);
      }
      else if (tag.ItemValue is bool)
      {
          value = (bool)tag.ItemValue ? 1 : 0;
      }
      var result = client.Write(tag.ItemName, value);
      if (result is ErrorCode && (ErrorCode)result != ErrorCode.NoError)
      {
          throw new Exception(((ErrorCode)result).ToString() + "\n" + "Tag: " + tag.ItemName);
      }
      

      could you tell me in the start button its correct use for my plc that:

      Plc.Instance.Write(PlcTags.BitVariable, 0.0);
      

      Because always have error.
      So i want to know if i have to change for my plc the PlcTags or send any value or something i do wrong.

      And when its started how i can write/read data from the plc.

      Please Answer me

      • S7.Net doesn’t support S7-1500, but you can edit the sources to support it.
        You have to download the sources and add them in the project, instead of the sources that you downloaded from NuGet.
        In the function

        public ErrorCode Open()

        you have to add:

        case CpuType.S71500:
            // Eigener Tsap
            bSend1[11] = 193;
            bSend1[12] = 2;
            bSend1[13] = 0x10;
            bSend1[14] = 0x2;
            // Fremder Tsap
            bSend1[15] = 194;
            bSend1[16] = 2;
            bSend1[17] = 0x3;
            bSend1[18] = 0x0;
            break;
        

        after all the list of supported plc.
        Then in my application example you have to target the S7 1500 by editing the sources.
        Refer to this thread if you need the complete implementation:
        https://github.com/killnine/s7netplus/issues/21

      • Also the S7-1200 and 1500 has to be configured properly.
        Refer to http://snap7.sourceforge.net/
        and go on left menu on
        Snap7 Communication
        Snap7Client
        Overview
        and scroll to the bottom to see a guide for s7 1200 and 1500.

        • Thks to the help now i update the source code of the dll code increasing but still has the same error and will not let me start.

          Might help me with the topic I’m learning about this and need solving.

          Again enter in the throw:

          if (result is ErrorCode && (ErrorCode)result != ErrorCode.NoError)
          {
              throw new Exception(((ErrorCode)result).ToString() + “\n” + “Tag: ” + tag.ItemName);
          }
          

          So maybe i send bad to start:

          (contained of startbutton== Plc.Instance.Write(PlcTags.BitVariable, 0.0);)

          Or i have to do another change in the code of the dll could you indicate me what i have to do to resolve please.

        • The sample application was done to read memory from the PLC program made for Step 7 v 5.5.
          If you don’t have a DB1 in your plc, probably it crashes because the plc tries to read DB1 but can’t find it.
          Did you replicate the DB structures as they are in the S7_pro1 project ?
          Also did you configure the DB as Snap7 documentation suggested ?

        • ok let me explain i have to connect with a plc to write and read data.

          That plc S7-1500 with rack=0 and slot:1 and
          in the plc i created a datablock for you to write in: this is DB5
          and a datablock for you to read in it: this is DB6
          OpArea: AREA_DATA
          (this means you want to read in a data block, area_in for example would mean you directly want to read an digital input port from the plc)

          DBNr. 6
          (as i sad, i prepared the plc here so that you can read in db 6 and write in db 5

          OpType: TYP_BIT
          (here you can input what typ of data you want to read..in this first case we just want to read a single bit with1 or 0)

          OpAnz: 1
          (in this case: is the number of bits we want to read, fo rexample: if you read 8 bits here..would be the same result as if you would read OpAnz 1 with OpType Byte )

          Offset: 0
          (this is kind of internal adress of the bits in the chossen db. you can see the offset in the picture i attached. the offset you enter here is the number before the dot=

          Bitnr: 0
          (this is the second part of the offset and the number after the dot. per offset you can have bits from 0..7 before you increase the offset.

          So now you want to read in DB6 Bit 0.0
          As an result you should get 1 or 0

          The plc Imput its: 9kW1wkL.png

          So, I connected with that the example do the connection modified the system and update the dll with new Enum, etc.

          When i tried do the start in the project for read the data (i guest) i got that error.

          Sorry, I don’t see the structure of S7_pro1 project.

          Pls could you tell me if i’m connected why i can’t read the data? maybe it’s the another form so, you have any source or example to that

        • On S7.Net sources, there is a project called UnitTest.
          On that project you find a simple usage of the library.
          Just make a simple application with a code like this:

          Plc plc;
          plc = new Plc(CpuType.S71500, “127.0.0.1”, 0, 2);
          var error = plc.Open();

          Check that there are no errors, of course you have to insert the correct IP, etc…

          ushort result = (ushort)plc.Read(“DB5.DBW2”);
          Console.WriteLine(result);
          // expected 0

          ushort val = 40000;
          plc.Write(“DB5.DBW2”, val);

          result = (ushort)plc.Read(“DB5.DBW2”);
          Console.WriteLine(result);
          // expected 40000

        • Probably you also need to specify slot = 0 and rack = 0 when you create the plc.
          like this:
          plc = new Plc(CpuType.S71500, “127.0.0.1”, 0, 0);
          Because as I read on other forums, S7 1200 and S7 1500 accept only this configuration.
          Make some tests and let me know, unfortunately i don’t have a S7 1500 CPU right now.

        • ok i do the test i recived that:

          Z4Tc1nU.jpg

          so, could you tell me i write the 4000 and -100 into the plc?

          that’s right?

        • Yes you can put a breakpoint, go online with the plc and see if the memory changes.

        • ok tell one more thing if i wanna write in the plc (“DB5”) i have to use : (“DB5.DBW2”) so if i understand to read the plc (“DB6”) i have to use Read(“DB6.DBW2”);….

          that’s right?.

        • Yes but you are reading only the word DBW 2.0.
          Now that you get started you can experiment by yourself, just by checkIng the code in the unit tests and in my example application.

        • Mesta you have any mail to contact you because i have to do some question about’s the offset values to read/Write that’s values in the plc or write me to a mail y.yanten@ottcomputer.ec pls

        • You can contact me with this: https://www.mesta-automation.com/contact/
          But please try to use comments, so other people can read it.

        • I try to explain to your response to comments can be read by all and facilitate future developments.

          Are assumed to have read values are DB6.XXX corresponding to the values of “DB_PLC_Input” so my serious question:

          “Bool_Bit” How can I read / write these variables and how would represent by analogy to the example.
          “Word_Number” equals “World Variable” and is represented with “DB6.DBD4”
          “Double_Int_Number” it represents “DB6.DBD6”, is that correct?

          In other words if I want to read “DB_PLC_Input” I change the last value with the offset value assigned?

          Attached image I hope to help dispel doubts

          PLCSoft2.jpg

        • The driver reads and writes bytes.
          If you want to read the Bool_Bit, offset 0.0, you should write DB5.DBX0.0 (1 bit)
          If you want to read the Word_Number,offset 2.0, you should write DB5.DBW2 (2 bytes)
          If you want to read the Int_Number,offset 4.0, you should write DB5.DBW4 (2 bytes) + conversion.
          If you want to read the Double_Number,offset 6.0, you should write DB5.DBB6 (4 bytes) + conversion
          If you want to read the Real_Number,offset 10.0, you should write DB5.DBB10 (4 bytes) + conversion
          String and char are not supported at the moment, maybe someone will add a patch in the future.
          If you want to read Date_Time_Bit, you should write DB5.DBX20.0 (1 bit)
          Usually it’s best to read everything in one shot, by using read class or read struct, or read bytes and get the array of bytes.
          The conversion that you need are all in the driver and you can check the Unit tests to see how to use to convert from bytes to real, int, dint and so on…

  8. Hi mesta
    I configure the DB snap7 documentation suggested
    but i dont know DB configure in plc ? Should i some varieable (memory input)
    i connect plc s7 1200 to app= connect
    then i will start but error
    why ?

  9. hi,
    I am a complete dummy on this matters. Thank you for the article. I have a question: I need, physically, to send an open/close operation, I mean to set the values to 0 or 1. How can I do this? Can I do it with Write method?

    • Hi Daniela, to get started with simple communication you can download the sources (https://goo.gl/LeZkbk) and look into unit tests. Write should work for bits, here is an example about integer:

      ushort val = 40000;
      plc.Write("DB1.DBW0", val);
      ushort result = (ushort)plc.Read("DB1.DBW0");
      
  10. Plc.DB1.cs :The following code was added.

    // DB1.DBX16.0
    public bool BitVariable160 { get; set; }
    public bool BitVariable161 { get; set; }
    public bool BitVariable162 { get; set; }
    

    PlcTag.cs :The following code was added.

    public const string BitVariable160 = "DB1.DBX16.0";
    

    MainWindow.xaml.cs : The following code was added.

     void timer_Tick(object sender, EventArgs e)
    {
      ledMachineInRun.Fill = Plc.Instance.Db1.BitVariable160 ? Brushes.Green : Brushes.Gray;
    } 
    /// Writes a bit to 1      
    private void btnStart2_Click(object sender, EventArgs e)
    {
        Plc.Instance.Write(PlcTags.BitVariable160, 1);
        textBox1.Text = Plc.Instance.Db1.BitVariable160.ToString();               
    }
    /// Writes a bit to 0        
    private void btnStop2_Click(object sender, EventArgs e)
    {       
        Plc.Instance.Write(PlcTags.BitVariable160, 0);
        textBox1.Text = Plc.Instance.Db1.BitVariable160.ToString();
    }
    

    From the PLC, it is possible to read and write. However, the C # program does not read.
    Why is this?

  11. Cesar Sosa Zuñiga

    Hi, I’ve been working with a simiens s7 1200 and a C# interface but i noticed some problems or bugs, i don’t know. Let me explain it: I’ve been working in existing network in a hub node so my plc communicates with the s71200 through a 8 ports hub. In my first release of my HMI with C# everything was doing fine but later i updated my software to increase it performance performance but i discovered that sometimes my pc can’t communicate with the plc during one or two seconds. i.e. my code has a 2 seconds timer that verify if the PLc is available and if it connectable. the problem come when i can read that is available but as i mentioned before it is not connectable. trying to find a solution I used the step by step instruction to see what was going wrong:

                    if (plc.IsAvailable)
                    {
                        ErrorCode connectionResult = plc.Open(); //open connection
                        if (connectionResult.Equals(ErrorCode.NoError))
                        {
                            gB_Control.Enabled = true;
                            ts_ProgressBar.Value = 100;
                            tSrip_Conectado.Text = "A Text";
                        }
                        else
                        {
                            ts_ProgressBar.Value = 0;
                            tSrip_Conectado.Text = "B text";
                        }
                        plc.Close(); //close connection
                    }
                    else
                    {
                        ts_ProgressBar.Value = 0;
                        tSrip_Conectado.Text = "C Text";
                    }
                }
    

    usually i get the A text but some times i get the b Text and i don’t know why. after a few tests the plc got frozen, and the pc wasn’t able to get connected with the plc so my code only returns me the b text. i solve it formatting the plc.

    Right now i just want to know where it can be the problem.

    • Why are you opening the socket every time you start a communication ? You can keep the socket open and just close it if the plc doesn’t respond or if you close the communication. Did you try to keep the socket open ? Also check LastErrorString variable, it if contains some informations.

  12. Hello Mesta, excellent work you are doing!. I have just two doubts / questions (S7-1200) :

    1 – Do we need to activate PLC for working with this program ? do we need to put some blocks ?
    2 -Where can I find offset / address of this tags in DB: (I can see adress it only in Default tag table but in DB can’t see.
    https://goo.gl/photos/z64FgbnjtdRo1AxV7

    I had to change for: plcDriver = new S7NetPlcDriver(CpuType.S71200, ipAddress, 0, 1);
    I can connect, but I see only 0 https://goo.gl/photos/rUxW5MrUioDe3fhU8

    Best regards.

  13. Hi i’m using vs2010 and Nuget, but S7.net simply doesnt show up when i would like to add this to my project.
    Is Visual studio 2010 not supported ?

    • VS 2010 should be supported, but I don’t know if the NuGet package was made compatible with VS 2010. Try to download the sources, compile in VS2010 and add the project directly into your application. Sources should be compatible also with .Net 3.5.

  14. Hi mesta,
    Thanks for this libraray projects.If u can do one video for how connect ,read,write and plc part too.we are reading reading but can’t achieve.Please in your video do tia portal as simulation and c# part.Like many people will understand and not ask so many question.Video is best training.
    thanks

  15. please do one video training with tia portal in local tcp/ip

  16. can i have an example on how to connect to omron plc using modbus tcp/ip in c# ?

    your blog is a life saver.

  17. Hi.
    Great work thank you!
    I have a question, how would you do to save data from a DB inside of a Database? It would be nice if you would have ideas 🙂

    Thanks!

    • I usually save alarms and events on a database, and I do that just by polling the plc and checking if there are some of them active that I didn’t recorded already.
      How I save these, it depends on the type of database, type of data, etc…

      • Hi Mesta,

        When you are saving alarms and events on a database, how do you do to avoid writing inside the database the same alarm each time the timer is checking the status of the PLC?

        Thanks 🙂

        • Hi, for alarms and events logging I use a dedicated class/thread indipendent from the plc, because there can be multiple plcs but the alarm logging is always a single part of the application.
          In the plc class I poll the plc to know the current state of the alarm, if it’s active or not.
          In the alarm logging class I scan the plc alarms to check if they are active or inactive. Once an alarm on the plc become active (from 0 to 1) I log it in the database and mark it with an “acknowledged” flag. When it becomes inactive (from 1 to 0) I reset the “acknowledged” flag.

  18. Hi mesta,
    Thank you for introducing this useful library. I am now trying s7netplus and have a question, I am using s7-1200 PLC and checked ip(“192.168.0.51”), rack(0) and slot(1) from TIA portal. But I get ErrorCode = ConnectionError when trying to establish a connection.
    Instead, I get no error with rack = 0 and slot = 0, but always have an ErrorCode = WrongVarFormat from reading anything, for example, “var data = plc.Read(“MD5642″);”. Did I do anything wrong? Thank you.

    • It’s a matter of conversion. Try to download the sources and look at unit tests. Writing the documentation is the next step that we are addressing.

  19. Hello, I’m now using this nice library, strangely, I’ve some incoherent values.
    I would like to get the DWord DB172.DW1 value, the first value represents a quantity, the second value is either 0 or 1,my syntax like this like (in vb.net) :
    Dim result as Object = MyPLC(“DB172.DBW1″)
    MsgBox(result.ToString & ” ” & result.GetType.ToString)

    I obtain as displayed message :
    12 System.UInt16

    On DB172.DBW2, I obtain this one :
    3073 System.UInt16

    What am I doing wrong? Thanks

    • Hello, you should specify what values you see on the plc, what you see from the library and what function of S7.Net you are using. Probably you need to check that you are reading the exact word and bytes, and using a correct conversion. Check the documenation to see how to make a proper conversion.

  20. Alberto Acerbis

    Hi mesta,
    I try to use your great library. I haven’t problem to connect to PLC, but everytime I try to read a variable, or set one, I have an error message “WriteData”.
    After this operation I don’t see any change in my PLC.
    Did I do anything wrong? Thank you

    • Hello, you should download the sources and go through the code to check what’s wrong. Actually this may happen from a number of reasons depending on your code. Also if you are using S7 1200/1500 check the documentation to set the DB with Full access.

  21. Hello, how i can read input, output and merkers ? the read/write DB it is ok.
    Thanks a lot.
    Mark

    • s7 1200 adn s7 300

      • Hello, you can specify the datatype that you want to read in ReadBytes, and use the start byte address field to specify from where to start.

        • Please can you post an example ? Thanks a lot.

        • int val = 1;
          plc.Write("M0.0", val);
          bool result = (bool)plc.Read("M0.0");
          
        • this code does not work. there’s not a write/read in the sample project. what can i do ?
          Thanks a lot!

        • Hello Mark, the sample project shows how to use the S7.Net driver to build your application, it is not intended to be adopted as a general architecture for an HMI, as it is too simplistic.

          The code works with the S7.Net driver, but in the example the driver is abstracted away to show how to make an abstraction on the driver and implement just what you need.

          If you don’t need the abstraction, you can just replace everything with direct calls to the S7.Net driver, without extra-classes, interfaces and so on.
          This will make your life easier for the first steps.

  22. hello! Another question : i see that with the s7 300 the led machine in the project, change color by the bit DB1.DBX0.0 (green If i set it with start button and gray If i reset bit with stop button). With s71200 it is always gray but the DB1.DBX0.0 bit change it status. What i do to get the same in 1200? Thanks a lot!
    Mark

    • Update:

      i test the connection a lot of time and with the s7 300 no problem.
      with the 1200, in the project i change cpu type, 0 , 1 and in plc program access on DB etc…
      but when i try debug mode, in the label i can’t see the value that i write in the DB. the label value it’s always 0.
      Also the led machine is always gray.
      What can i do ?
      excuse me but i am a beginner with plc !
      Thanks a lot.

    • plc = new Plc(CpuType.S71200, “ip address here”, 0, 0);

      plc = new Plc(CpuType.S71200, “ip address here”, 0, 1);

      i tried both, alway connect but i have always this

      https://goo.gl/photos/rUxW5MrUioDe3fhU8

      on tia portal i see the change of value when i write in the textbox but on the application i read always 0.
      Best Regards.
      Mark.

  23. Mesta, i found the error. In my plc program i’ve committed an issue. Now i can see and write all from the application. I have another question : if i write a value in textbox from the solution, i read it in plc in real time, but if i write a value on db by tia portal, i can not see it in my application. why ? thanks a lot for your disponibility.

  24. I see that in s7_pro (the program plc), in OB1 there are some instructions that must be copied to plc program.
    Now i create an ellipse and two button, one for set and one for reset a merker on plc.
    From Tia portal i can see the value of merker changing, but my ellipse doesen’t change color.
    Can you help me with code to read from plc ?
    Thanks a lot.
    Regards!

  25. Hi,

    We’re struggling to read any data in DataBlocks using this project and a S7-1200 PLC.
    Stepping through the source code we always receive the error “WrongNumberReceivedBytes” which is here in PLC.cs line number 332.

    “int numReceived = _mSocket.Receive(bReceive, 512, SocketFlags.None);
    if (bReceive[21] != 0xff)
    throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());

    We are calling “ushort result = (ushort)plc.Read(“DB120.DBW4″);” which exists in the PLC datablock.

    Is there an error with the number of bytes being sent in the “_mSocket.Send(package.array, package.array.Length, SocketFlags.None);” method causing the PLC to respond with the error?

    Many Thanks
    Tom

  26. Hi,
    i’m using S7.NET driver to read variables from S7300.
    I don’t understand how to show real value of a counter.

    Example: i put in a VAT the variable DB12.DBW0 (defined as WORD in Simatic)
    If i show it as a counter i see 98 (the real value), while if i show it as decimal i see the value 152.

    In my C# project i would like to show the real value (98), but i alway see 152:
    I convert the value from plc in this way:
    var valueCounter = (ushort) S7.Net.Types.Counter.FromByteArray(lst.Skip(0).Take(2).ToArray());

    Someone can help me?
    Many Thanks
    Francesco

  27. solved in this way:

    I read from plc in this way:
    ushort valueCounter = S7.Net.Types.Word.FromByteArray(lst.Skip(0).Take(2).ToArray());

    and the i convert in this way:
    txtCounter.text = valueCounter.ToString(“X”);

    So in my text field i visualize the real value of the counter.

    I don’t know if there is a better way, but this works. Howewer any suggestion is welcome.
    ciao

  28. HI, let’s speak about timers…

    I defined in S7-300 a DB with inside an array of “S5TIME”.

    When i try to READ the value of one variable all works fine and a see in my double variable in c# the correct value in seconds (in the format x.xx sec). For this i use this conversion:

    var valueTmr = S7.Net.Types.Timer.FromByteArray(lst.Skip(0).Take(2).ToArray());
    rr.SetField(“PLC”, valueTmr);

    My problem is when i try to do the opposit conversion (WRITE in the PLC). I’m not able to find the correct way to convert a double variable from c# to byte array for PLC. I tried a lots of way but i didn’t find the correct one.

    Some one can help me?

    best regards
    Francesco

    • I don’t have a plc to make a test right now, but it seems that you should use a double.

      var bytes = Types.Double.ToByteArray((double)value);

      • unfortunatly your suggestion doesn’t work. Remember that is not a simple conversion between differents types. Infact you have to implement resolution bits.
        S5TIME variable must have this format:
        W#16#tXYZ
        where:
        – “t” is the time resolution (xx00 = 10 msec, xx01 = 100 msec, xx10 = 1 sec, xx11 = 10 sec)
        – “XYZ” is the BCD value

        • I solved with the belowe function.
          Consider if it’s useful to introduce this function in your library.
          ciao
          Francesco

          public static byte[] ConvertDoubleToS5T(double value)
          {

          double cvalue = value * 100;
          int ivalue = (int)cvalue;
          byte[] bcd = new byte[4];
          byte[] S5TWORD = new byte[2];

          if (ivalue 999000) ivalue = 999000;

          if (ivalue 999) & (ivalue 9990) & (ivalue 99900) & (ivalue 0; i–)
          {
          bcd[i] = (byte)(ivalue % 10);
          ivalue /= 10;
          }

          S5TWORD[0] = (byte)(bcd[1]);
          S5TWORD[0] += (byte)(bcd[0] << 4);

          S5TWORD[1] = (byte)(bcd[3]);
          S5TWORD[1] += (byte)(bcd[2] << 4);

          return S5TWORD;
          }

        • sorry, but the the cut&paste did’t work well, i try to post the code again…

          public static byte[] ConvertDoubleToS5T(double value)
          {

          double cvalue = value * 100;
          int ivalue = (int)cvalue;
          byte[] bcd = new byte[4];
          byte[] S5TWORD = new byte[2];

          if (ivalue 999000) ivalue = 999000;

          if (ivalue 999) & (ivalue 9990) & (ivalue 99900) & (ivalue 0; i–)
          {
          bcd[i] = (byte)(ivalue % 10);
          ivalue /= 10;
          }

          S5TWORD[0] = (byte)(bcd[1]);
          S5TWORD[0] += (byte)(bcd[0] << 4);

          S5TWORD[1] = (byte)(bcd[3]);
          S5TWORD[1] += (byte)(bcd[2] << 4);

          return S5TWORD;
          }

        • No, i don’t know why but the code you see in the comment is not the one that i introduced.
          However, if some one is interested in it i can send it in an alternative way

        • Open a pull request on github.

  29. Hi Mesta,

    I am trying exactly same stuff you are doing in the video. I have created Data_block_1[DB1] in TIA Portal V13. I unchecked the optimization block access property as you have said. Then I have enabled full access, and I have permitted PUTGET communication. I have only 3 integers in DB1 block named int1, int2 and int3 occupying offset values of 0.0, 2.0 and 3.0 respectively. I have established communication of PLC with my PC with no problem at all. I can monitor the values in TIA Portal. Address IP of PLC is 192.168.0.101 Then I started writing the code you have written after downloading S7 NuGet package. I have built the program with no errors. Now I will try to read DB1.DBW0.0, int1.

    Here is the code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using S7.Net;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    using (var plc = new Plc(CpuType.S71200, “192.168.0.101”, 0, 1))
    {
    plc.Open();

    var intVariable = (ushort)plc.Read(“DB1.DBW0.0”);
    Console.WriteLine(“DB1.DBW0.0: ” + intVariable);
    Console.ReadKey();
    }
    }
    }
    }

    But when I execute the program, I get an error saying that :

    InvalidCastException was unhandled
    An unhandled exception of type ‘System.InvalidCastException’ occurred in ConsoleApplication1.exe

    I don’t know how I can get this error when I did exactly what you have done.

    Thanks in advance Mesta.

  30. How can I connect my PLC Modicon M340 with C#?.
    I have searched a lot but no way.

  31. Hello you are unbelievable. How can I download simulator code? Microsoft Visual Studio 2015
    simulator = SGsimulator

  32. Hi there, I have done as in tutorial, I am using s71200 after plc.Open() I am checking if (result != ErrorCode.NoError) and then for example I do as follows:
    var db1Bytes = plc.ReadBytes(DataType.DataBlock, 1, 0, 38);
    bool db1Bool1 = db1Bytes[0].SelectBit(0);
    Console.WriteLine(“DB1.DBX0.0: ” + db1Bool1);

    and I get an error IndexOutOfRangeException.. It seems that although there is “NoError” plc.ReadBytes does not read anything.. Could you indicate me what should I check? Regards

  33. Hello and thanks for your tutorial, everything id fine.
    My question is how can we convert variables correctly into c# when it comes to use LReal and LInt;
    the .ConvertToDouble() is not working right on those 64bit variables.
    Thanks for your help, regards.

  34. HI mate, I have a problem with lags with an application I created (windows form application) I am using a timer to read from the PLC (S7 300) periodiclly (every 2 second) seeing that there is many data blocks (I am using read bytes , 200 bytes every request )

    • If by lag you mean that the user interface is unresponsive, you are probably using the Windows.Forms.Timer. You should use a timer that ticks on a parallel thread, like System.Timers.Timer.

  35. Hi Mesta

    i was write the code according to your video and it’s working. but when i change the coil state in the TIA portal, true to false or false to true my console application coil state will note change. it is in the previous coil state. but when i close the console application and again run it, it will show the present state of the coil. so i want to know how to modify this code in order to continuous monitoring. because i want to real time monitor the timers and counter values.

  36. Hi mesta!
    I have a question about ReadClass or ReadStruct. I would like to read lot of bytes DB, but not all of them and they are in different place in DB, for example DB1.DBB10 and next one in DB1.DBB17 and so on. What ReadClass(ReadStruct) does actually? How to read this kind of situation as fast as possible ?
    BR
    pablo

    • ReadClass makes a call on ReadBytes, then assign the values to the class items. You can read up to 200 consecutive bytes with a single TCP call. If you have scattered data, you may want to use ReadMultipleVars. That’s the only way to read up to 200 bytes from different areas/addresses in a single call.

  37. If I have a datablock DB50 for example and it has 1000 bytes how I can read the first 50 bytes and the last 50 bytes in one shot without reading the others byte

    • You can use ReadMultipleVars. That’s the only way to read up to 200 bytes from different areas/addresses in a single call.

      • It would be helpful if you give an example about ReadMultipleVars for the example above Thanks in adavnce

        • You can find an example in the unit tests.

          var dataItems = new List<DataItem>()
          {
              new DataItem
              {
                  Count = 1,
                  DataType = DataType.DataBlock,
                  DB = 2,
                  StartByteAdr = 16384,
                  VarType = VarType.Word
              },
              new DataItem
              {
                  Count = 1,
                  DataType = DataType.DataBlock,
                  DB = 2,
                  StartByteAdr = 16,
                  VarType = VarType.Word
              }
          };
          plc.ReadMultipleVars(dataItems);
          
  38. Hello Mesta,

    Is it possible to establish communication between visual studio and simatic Manager v5.5 using communication interface MPI instead of Ethernet?
    Thanks
    Sakin

    • You can only connect to a real plc or to PlcSim (the simulator that comes with Step7). With PlcSim you can only use ethernet, but you can use an external ethernet card and remove that later.

  39. Es posible trabajar esta librería con Windows Forms? O solo es útil para proyectos en WPF?

    • It can be used also with Windows Forms.

      • I have noticed that in the textbox where you enter the IP address, you can put any IP address and all ways you can connect to PLC, provided the IP address of the PLC is “192.168.0.1”. If change you the IP address to the PLC and introduce you the new IP address in the textboxt in C#, not connecting with PLC. To do so would have to stop the program of C# and go to the “PLC.cs” class and method “Connect” change the new IP address and compile the program again
        ————————————————————————————————————————————————————-.
        public void Connect(string ipAddress)
        {
        if (!IsValidIp(ipAddress))
        {
        throw new ArgumentException(“La dirección IP no es válida.”);
        }
        plcDriver = new S7NetPlcDriver(CpuType.S71200, “192.168.0.120”, 0, 0); <——Change IP Address
        plcDriver.Connect();
        }
        ————————————————————————————————————————————————————–

        I want to know how I can do so that when you change the IP address of the PLC, that new IP address can be put in the textbox of the program in C# without having to stop the program, change and recompile.

        Regards

  40. JONATHAN VAZQUEZ

    i notice that all the config/Examples are done to connect through TCP/IP is it possible to connect using MPI / COM port??

    sry for the bad english

  41. Francesco longo

    Hi Mesta, thanks for your good job. Is the library compatible with s7-200 plc?

  42. Hi Mesta, Thanks for your good job. Actually i am trying to read data from s7300 plc continuously, the connection with plc disconnects after 9th or 10th iteration in c# can you help me regarding this.

  43. I could achieve this https://www.youtube.com/watch?v=hRsuFrI9-zE thanks mate your tutorial is very helpful

  44. Hello Mesta,
    I am using S7-314 PLC to connect to c# application. In my application I receive the cputype, ipaddress, rack and slot and later connect to plc. I’m displaying errorcodes based on which error is thrown. Now if I select s7200, 192.168.0.1,0,2 to connect to plc, it is connecting to plc instead of throwing me errorcode as wrongcpu_type. Could you please tell me what may be wrong here ?
    Thanks in advance

    • Cpu type doesn’t make a check on the processor type, it’s only an helper that sets only the ISO message with the correct values for the communication. Those values are different from S7-200 and S7-300, so maybe the S7-300 is responding some sort of error message. Can’t check as I don’t have the hardware now.

      • Could you please provide me a hint on where to catch these errors?

        • I don’t think there is a way to get plc type consistently. Try to make a read after connecting to a variable that exists, like “M0.0”. If it fails, the configuration is wrong.

  45. Hello Mesta,
    Are you have test PLC project for TIA v14?
    Unfortunately i can’t install v5.5 under Win10, and i continiosly can’t to connect from their PLCSIM.
    if i try to set PLC ip address as ip address of my PC i have ConnectionError, if i leave 192.168.0.1 or something like that – programm can’t recognized them.
    Thank You.

  46. Hello Mesta,
    I’m trying to get data from my plc using s7.net
    All works perfectly with s7300 but i have a question.
    In step7 i have create a DB10, so a DB10.DBD4 and set data type to REAL.
    From a VAT i set it with a value for example 1.52 but how i can read it correctly from my c# application? How i can convert it and which kind of variable should i use? If i work with integer type all works fine.
    Thanks a lot
    Mark

  47. Hi, I found your site on google. I would like to read Data from a S7-1200. after searching in the web I found your site. Connecting to the S7 is successfully. but I have no DATA_BLOCK. is it possible to read the Data with the address. I have tryed but it do not work. attachted I add a Screen from the TIA. Also my sample Project. I hope you can help me.

    https://1drv.ms/f/s!At9eSOEHzBlyhIRLn3w1crSB9bITnA

    thanks!

    • You can also read merkers. Start with plc.Read(“M0.0”); It should work.

      • Hi, If i want read input from S7-1200 Sim. What’s the command should input? like Read(“I0.1000”) or (“I1000.0”), i have try Read(“I1000”) but faild with WrongVarFormat. BTW,when i try read merkers and data block, all succeed, i don’t know why. Thks!

  48. Hello, hope that you can help…
    I’m using fine S7.Net ReadClass() to pull each 500ms DB data into several text boxes on my winform application. This allows me to capture data changes and then write values to some PLC tags.
    It works relatively good but sometimes I got some mismatches between read and write answers: (see below debug data)

    OK – read: 31 03-00-00-1F-02- F0-80-32-01-00-00-00-00-00-0E-00-00-04-01-12-0A-10-02-00-68-02-02-84-00-00-00 etc…
    OK – read answer: 129 03-00-00-81-02-F0-80-32-03-00-00-00-00-00-02-00-6C-00-00-04-01-FF-04-03-40-06-42-08-0 etc…
    OK – read: 31 03-00-00-1F-02-F0-80-32-01-00-00-00-00-00-0E-00-00-04-01-12-0A-10-02-00-68-02-02-84-00-00-00 etc…
    NOK – write answer: 22 03-00-00-16-02-F0-80-32-03-00-00-00-00-00-02-00-01-00-00-05-01-FF-00-00-00-00-00-00-0 etc…
    NOK – write: 36 03-00-00-24-02-F0-80-32-01-00-00-00-00-00-0E-00-05-05-01-12-0A-10-01-00-01-02-02-84-00-00-0F etc…
    OK – read answer: 129 03-00-00-81-02-F0-80-32-03-00-00-00-00-00-02-00-6C-00-00-04-01-FF-04-03-40-06-42-08-0 etc…

    Monitoring data exchange with Wireshark, everything looks OK, no such mismatching!
    Looks like it also occurred on your original “HmiExample” project.
    Did you experienced such behavior ? I’m thinking about read/write thread overlapping but unfortunately I’m not very experienced with all that.

    Thanks by advance for your answer!

  49. Hello, what i raised in my previous post is very similar on what you report recently as bug on

    https://github.com/killnine/s7netplus/issues/94

    Regards,

    • I have to see the code to judge if there is a thread problem. That issue is because Read class doesn’t check for LastErrorCode after executing ReadBytes.
      But I have literally 0 time to spend on that library right now, so if you want to fix it and create a PR you are welcome.

  50. Hello,

    I need to read a whole (big) DB from my CPU1500 into SQL management studio.
    Can this be done with your application.

    This has to run continuously with a costumor so I can easily look up some data that is ordered by date

    Just need a push in the good direction to get started
    The issue is to connect with the PLC and read all the data continuous.
    Can’t it be done in a stored procedure? (Just asking)

    Thanks in advance!

  51. Hello there,
    I have some experience on PLC programming and don’t have practically an experience with C# programming.
    However. I managed to test some S7Net functions like read/write a Byte, Real and WORD. That’s enogh for now. That’s working.
    True, I did it with PLCSIM and NetToPLCSIM. In the near future I’ll have to test that with real hardware.
    So I need some clarifications on that theme.

    1) For instance, regarding sample proect called S7.NET. From where were taken these things like: PATH: ‘S7Net’ Project -> References -> directory ‘Types’ and fies ‘Conversion.sc’, ‘Enums.sc’, ‘PLC.sc’?

    2) As far as I understand, there’s two ways to connect library to the project. The first on is via Project -> Reference-.>add Reference ->Browse.
    And second one is connection of sources. Am I right? Please correct me.

    Regards
    Mik

    • I think I’m using NuGet for the most. Solution Explorer, right click on project, Manage NuGet packages, etc…

  52. By the way, that ‘nuget-pack’ from ‘gitnub’ page is not recognized by Visual Studio 2015.
    What about my first question? What are those files?

  53. By the way, I’ve just checked that ‘nuget-pack’ from github page is not recognized by VS2015.
    What about my first question? What are those files in the project tree?

    Regards
    Mik

  54. hello Mesta,
    I’ve tried to read simpliest structure https://imgur.com/a/EHxha, and got an exception
    Could you look at code? Ican’t get what’s wrong.

    Mik

  55. Hi mesta, i’m having an exception in S7 1200 PLC reading operation. It throws “Wrong Var Format” or it reads the value “10” which is not an actual value in PLC.
    kindly help me. Thanks in advance.

    Regards,
    Brown Sebastian.

  56. Hello
    I can not get the error code
    visual writing fails to assign a void to an implicitly typed variable

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using S7.Net;
    //using S7.Types;

    namespace ConsoleApp1
    {
    class Program
    {
    static void Main(string[] args)
    {
    using (var plc = new Plc(CpuType.S7300, “176.16.12.205”, 0, 2))
    {

    var result = plc.Open(); // ????????

    byte pravka = (byte)plc.Read(“M80.0”);
    byte razgrogka = (byte)plc.Read(“M50.0”);
    plc.Close();
    bool pravkaBool = Convert.ToBoolean(pravka);
    bool razgrogkaBool = Convert.ToBoolean(razgrogka);
    plc.Close();
    }
    }
    }
    }

  57. Hello Mesta,

    First of all I’d like to say heck of an article on how to use S7.Net driver.
    I have a problem reading/writing anything (bit, word) to a DataBlock in a program in TIA portal V13.

    I have enabled the PUT/GET option in the PLC, the DataBlock is global and I have turned off the optimize option. I am able to read that the PLC is connected and is available. The firmware on my 1214 CPU is: V 4.2.2. I’m using Microsoft Visual Studio 2017.

    Here’s my code:

    private void Form1_Load(object sender, EventArgs e)
    {
    try
    {
    plc = new Plc(CpuType.S71200, “192.168.100.201”, 0, 1);
    plc.Open();
    }
    catch (Exception ex)
    {
    MessageBox.Show(this, ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    if (plc != null)
    {
    label1.Text = Convert.ToString(plc.IsAvailable);
    label2.Text = Convert.ToString(plc.IsConnected);
    }

    if (plc != null && plc.IsAvailable && plc.IsConnected)
    {
    bool bit = (bool)plc.Read(“DB1.DBX0.0”);
    }

    When I try to read status of DB10.DX0.0 or another variable I get this error:

    S7.Net.PlcException: ‘Unable to read data from the transport connection: An existing connection was
    forcibly closed by the remote host.’
    SocketException: An existing connection was forcibly closed by the remote host

    Any help/idea is much appreciated.

    • Arijit Dutta

      I am facing the same problem. when i debugging, plc.IsAvailable and plc.IsConnected returns TRUE. But When executing the below line :
      ushort result = (ushort)plc.Read(“DB1.DBW0”);
      txtVal.Text = string.Format(“{0}”, result.ToString());

      It returns an exception. Message: ‘Unable to read data from the transport connection: An existing connection was
      forcibly closed by the remote host.’.

      I have followed all steps which shown in the video (both in TIA configuration and C# programming).

  58. Is there any advantage using s7.Net over Sharp7?

  59. Hello ,
    trying to write from VB to real PLC is not easy way without conversion .
    i try to do two textbox , one for address other for value
    when i write ( address , value )
    plc read it other number
    when i use conversion it works
    but i need use an entered value
    can you help me ?

  60. hai can anyone tell me how to read/write string tags

  61. How could I read the comment for a certain value in a DB?

    Type Value Comment
    Bool True E-stop #1 pressed

    I am able to read the values in the DB but I would like to get the comment as well.

  62. Having issue with latest .net framework, and using the sample 4.5.2 c# app, that was originally compiled or developed with 0.1.2 of s7netplus. I am trying to use it in a 4.7.2 version app. I changed the target framework of the solution (HMIExample and S7NetWrapper) to 4.7.2. And then updated to 0.3.0 of the S7netplus.

    Now I get error that “CS0815 Cannot assign void to an implicitly-typed variable”. It appears to be in S7NetPlcDriver.cs, the Connect() and Write() methods. The lines that are offending are: var error = client.Open(); and var result = client.Write(tag.ItemName, value);

    So I was trying to change the method signature so it returns type ErrorCode, but I can’t edit it (does it pull this from the S7Net.dll?) Then I tried to change the var modifier to be ErrorCode, but then it spits out that it can’t implicitly convert void to ErrorCode. Then I tried to cast it, but that doesn’t work either, says can’t convert.

    Not very experienced in the .NET and c#, but it appears to be in the wrapper that you created (apologies if I am mistaken). Other than reverting to 4.5.2 of .net, I don’t know how to change the Open and Write methods to return an ErrorCode instead of void. Can you provide some direction?

  63. Hi Mesta,
    Firstly, thanks for your helping.
    I connected PLC S7 1200 and PC successfully. The next step, I want to connect my PLC via internet by mobile or PC, with the application I built in Microsoft Studio. Could you suggest me a way to do this?

    Thanks and Best Regards,
    Harold Nguyen

  64. Why it remind: “S7NetWrapper.dll” could not be found, when I commission the project “PlcCommunication”.

  65. Hello! With the help of this tutorial, I was able to write a program and it works through nettoplcsim, but with a real PLC (S7-400) writes “ConnectionError”?
    (CpuType.S7400, ipAddress, 0, 2)

  66. Hi mesta
    how are you. what is your problem. check to picture. please help me.

    https://ibb.co/cL2cR5d

  67. Hi mesta,

    I am trying to connect to SIEMES PLC ET200SP, I tried by S7 but it doesn’t work. It works like the connector was reading and writing correctly but it doesn’t.

    Do you know some librery to connect at ET200SP?

    Thanks,

  68. Hi Mesta
    In your exampe we see how to write values in addresses.
    How can I read a bool value from an address?
    Thanks in advance.

  69. Arijit Dutta

    Hi Mesta,

    I am developing a simple C# windows application with the help of your video. I have followed all the steps shown in video (both in TIA configuration and C# programming) and the manual. But I am facing a problem.

    plc = new Plc(“S71200”, “192.168.3.1”, 0, 1);

    When I am debugging, plc.IsAvailable and plc.IsConnected returns TRUE.

    But When executing the below line :
    ushort result = (ushort)plc.Read(“DB1.DBW0”);
    txtVal.Text = string.Format(“{0}”, result.ToString());

    It returns an exception. Message: ‘Unable to read data from the transport connection: An existing connection was
    forcibly closed by the remote host.’.

    Please help me out.

  70. Hi Mesta,

    How can i read a Lreal data from PLC to C#.

    Thanks.

  71. Hello.
    Do you have any libraries (examples) for reading data from Sinumerik ?

  72. Hi Mesta, Is there a way to read DB with Optimized Data Block with C#?
    Thanks,

  73. Hi, I am a vb.net user and I have managed to read values from my 1200 plc with the S7.dll. Everything is working very nicely.

    Thank you for your great tutorial.

    I now want to write a single value to DB61:

    My Code:

    Using plc = New Plc(CpuType.S71200, “192.172.0.5”, 0, 1)

    plc.Open()

    plc.Write(“DB61.DBD40”, True)

    Dim db61RealVariable As Integer

    db1WordVariable6 = 200 ‘stirrer speed

    plc.Write(“DB61.DBD40”, db61RealVariable)

    plc.close

    I manage to write to the correct place, but get funny values. The 200 value gets a scientific notation …So I think it has something to do with the data type sent. I require assistance to convert the 200 to the correct data type…The plc is set up as a REAL. I am not sure how to handle this in VB.net 2022.

    Assistance appreciated

    thank you

    Glen

Leave a Reply

Your email address will not be published.