c# communicates with Siemens plc

Preface: The tools used in the example are Portu v15 + advanced v5.0 + s7netplus.

I write this because there are too few introductions to PLC on the Internet, and the information is scattered. As a beginner, I took too many detours here. So record it. And some common bugs and things that need attention

Since the writing is relatively detailed, it will be quite long.

PortalV18 + advanced v5.0 installation

Just watch this tutorial: This is the tutorial for the big boss’s WeChat official account: just follow it step by step to install it. It is worth noting. During the installation process, turn off the firewall that comes with the system.

If you have installed 360 and computer manager etc. Also turn it off. It’s best to uninstall it otherwise some weird problems may occur. Remember!

TIA Portal V18 software installation tutorial

Start the blog

First, follow the tutorial and there will be six icons after the special account:

Don’t worry about the above 4, among which TIA Portal V18 is Portal programming software (hereinafter referred to as Portal). S7-plcsim advanced V5.0 is simulation software (used to simulate real plc) (hereinafter referred to as simulation software)

First open the blog:

Create a new project: project name customization

Slow project creation is normal. It has nothing to do with computers

On the startup page, click “Devices and Networks” and create it as shown in the picture. Checking Open Device View indicates entering the programming page. There is a direct opening device view on the startup page. Even if a device (i.e. CPU) has not been created, you can go in directly and create it in it.

During the creation process, you will be asked to check several commonly used options. I remember they are about security. My own computer is shut down. Anyway, if it is only for c# to simulate communication, all those permissions will be opened.

After creation, the approximate interface will look like this: Here we only introduce the C# communication aspect, so the specific use of the Portal software will not be explained.

When communicating with c#, the main purpose is to read the data in the data block. Just create a database directly according to the steps in the figure. Any name

Introduction to the main types in plc and c#:

The code is introduced. Others are ordinary types

 //types in plc and c# types bool => Bit
    //Byte => byte
    //word => ushort
    //DWord => uint
    //Int => short
    //DInt => int
    //Real => float
    //LReal => double
    //String => string //string can only store ascii code
    //DateTimeLong=>datetime
    //s7wstring=>string //wstring can store Chinese characters and takes up twice as many characters as string

Set the value directly on the interface:

What needs special explanation is db12. This is his name. You can write it as you like.

The meaning of type array is Example: Array[0..1] of Int. An array with subscripts starting from 0 and having two sub-items. is of int type

In the picture, db12[0] is the first value. Double-click the starting value at the back to set it directly.

The above is how to add data to the Portal software. As for the specific usage method, you can find a video tutorial.

Connection simulation

Set portal access permissions

This step is critical and prone to many bugs.

First turn on the computer

You will find that there is an additional virtual network card whose name starts with Siemens. This is a virtual network card installed by the simulation software for you.

Local plc communication uses this

Right click > Properties

Check the first one. Double click on the second one

Check the box to use the following IP address:

The recommended IP address is 192.168.xx.dd.

xx recommends writing an address no larger than 100. Same thing with dd. If the number is too large, some strange problems may occur.

The subnet mask is 255.255.255.0. Do not change. After setting, click Save

Second, open the Portal software

Find Devices and Networks and double-click the green dot 2. The IP address will be set below. Set the IP address to be the same as in the virtual network card. The last section is a little larger than the virtual network card

For example, the virtual network card is 192.168.43.a, so it is best to be 192.168.43.a + 1. The last digit should be larger and must not be consistent. This is very important.

Third: Check the block to support simulation

Click on the project > right click and find properties > click on protection > check the block to support simulation when compiling > click OK

Fourth: Set access permissions

Click on the device>Right-click Properties>General>Protect plc configuration device>Uncheck Do not click OK yet

Access Level》Select Full Access》

Connection mechanism》Check to allow put/get access》Cancel secure communication》Click OK

This is to enable c# to connect to our plc.

Apart from checking put/get access permissions (very important), I mentioned everything else once when I turned on the device. Cancel everything that can be canceled when opening the view.

Fifth: Cancel data block access optimization

Find the program block>Data block>Right-click Properties>General>Cancel Optimized block access The database is accessible from opc ua Two items

This concludes the setup of the blog.

Start the simulation software advanced v5

The interface looks like this

First open the tcp/ip connection. plc communication is based on tcp

The numbers represent the meaning on the left

1: Open tcp/ip connection

2: local means local. Select this with a virtual network card. If you are using Ethernet, you can check it. However, the above IP address must also be changed to the Ethernet iop address. No further details

3: Write the connection name as you like without repeating it. It is recommended to use date.

4: Write the address set in the Portal software. (This must be consistent)

5: Same as above

6: Indicates the model number, here we take 1500 as an example. It needs to be consistent with the device selected by the Portal software.

7: Turn on

After startup:

There will be problems here

Green: Normal Yellow: Waiting Red: Error reported

The first of the three lights indicates: whether it is operating normally.

The second one indicates that there is an error in the plc

The third one represents the power supply (there will basically be no problems in simulation)

It is normal for the first light to be yellow or green after turning it on.

A common problem here: the second light keeps flashing red

Solution 1: Check whether the IP of the virtual network card is consistent with the one set by the simulation software. If they are consistent, an error will be reported. The IP of the simulation software needs to be consistent with the IP of the Portal. Cannot be consistent with the IP of the network card

Solution 2: Delete the authorization file. Reauthorize. Select “Floating address in the network”. The authorized folder name is “ax nf zz”. It needs to be deleted first. How to delete it can be found on Baidu.

It is recommended to use Everything software to remove voidtools

Solution 3: Delete 360, Tinder and other security software and reinstall it. (Very important)

Until only the first light is on. and not red

Download simulation

The above has prepared the environment. If there is no problem with the above, you can proceed to the next step.

Manually select device》Click to download

Select pnie》Select siemens virtual network card》Start the search

It is very easy to report an error at this time

For example: The device was not found at the specified address (check whether the IP address is correct.)

Two devices found, one incompatible device found:

Solution 1: Open Control Panel>Settings Small Icon>Find Settings pg/pc interface

Select serial cable.ppL.1>The third one below>Click OK>Search again

Solution 2: Download tool https://download.csdn.net/download/qq_53217825/88333254

After downloading, there will be an exe file inside. Just restart after installation. Do not change the installation process, just keep going to the next step.

Solution 3: Delete 360, Tinder and other security software and reinstall it (you only need to reinstall the simulation software). (Very important)

All the above is done. If you still have questions, you can send a private message or comment.

Download to device

There are no bugs in the straight road》Click to download

Click to connect

Click to load

Click Finish

At this time, the first light of the simulation software turns green.

If it is otherwise, it means there is a bug.

Following the above operations, the setup of the Portal software is basically completed.

c# uses s7netplus to communicate

Open vs. Add a console program (this is just an example, you can add any type of program)

Introduce nuget package: S7netplus

Just make the connection as follows.

Plc plc = new Plc(CpuType.S71500, "192.168.43.14" + "", 0, 1);//The rack number and slot number are usually 0 and 1 in the ip writing simulation software set ip.

plc.Open();//Open plc connection
if (plc.IsConnected) //Determine whether it is successful
{
    Console.WriteLine("PLC connection successful");
}
plc.Close();//Close the connection

Regarding reading data in C#, please see my other article: http://t.csdn.cn/92rfL