S7-1200 ve PC yi C# ile haberleştirme

 


Önemli uyarı: PLC ile pc haberleşmesinde S7.Net sıkıntı çıkarabiliyor, fabrika ortamında kullanılan bir yazılım yapacaksanız S7.Net yerine Modbus TCP protokolünü kullanın... Modbus kütüphanesiyle ilgili bir yazıyı ileride yayınlayacağım 03.06.2022

S7.Net Plus paketi ile bu mümkün oluyor. Daha önceki bir yazıda S7.Net in NuGet ile nasıl kolayca Visual Studio ya veya SharpDevelop a yüklendiğinden bahsetmiştim.. O yazıda error handling (hata yakalama) durumları yoktu, o yüzden konuya tekrar değinmek istedim. İncelemek isterseniz, paketin pdf dökümanına linkten ulaşabilirsiniz. Bu paket sayesinde otomasyon projelerinizde HMI veya Scada niyetine C# ı kullanabilirsiniz. Plc içeren projeniz için zaten bir PC ve C# kullanmanız gerekiyorsa, Siemens plc ile haberleşmek şart olacağından yine bu paket imdadımıza yetişiyor. Bu paket, Siemens in kendi S7 protokolünü kullanmaktadır. Diğer plc markaları için bu işlerde modbus protokolü kullanılıyor. Modbus protokolüne de başka bir yazıda değinmek istiyorum.

Konuya ilk kez dalanlar için bir not iletmek isterim. Eğer PLCSIM ve Tia Portal ile çalışacaksak NetToPLCSim programına ihtiyaç var, yok gerçek plc ile çalışacaksak bu programa ihtiyaç yok. Plc de PUT/GET komutlarına izin vermemiz ve bir DB de işlem yapılacaksa plc DB özelliklerinden "Optimized block access" tikini kaldırmamız gerekiyor.



Plc deki MW100 den okuma yapıyoruz.. 




Plc de bulunan DB1 e yazma yapıyoruz.. 





using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using S7.Net;

namespace S7.net_Example
{

    public partial class MainForm : Form
    {
         Plc _S71200 = null;

         public MainForm()
        {
                InitializeComponent();
         }

         void MainFormFormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (_S71200 != null)
            {
                _S71200.Close();
            }
         }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
          }

            void BtnConnectClick(object sender, EventArgs e)
            {
                try
                {
                    string ipAddress = txtIPAddress.Text;
                    _S71200 = new Plc(CpuType.S71200, ipAddress, 0, 1);
                    _S71200.Open();

                    if (_S71200.IsConnected)
                    {
                        btnConnect.Enabled = false;
                    }
                }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, "PLC ile bağlantı kurulamadı !", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                void BtnDisconnectClick(object sender, EventArgs e)
                {
                    try
                    {
                        if (_S71200 != null)
                        {
                            _S71200.Close();
                         }
                        btnConnect.Enabled = true;
                      }
                    catch (Exception ex)
                   {
                        MessageBox.Show(this, ex.Message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                 }

                void BtnReadClick(object sender, EventArgs e)
                {
                    try
                   {
                        if (_S71200 != null)
                        {
                            string mAddress = txtMAddress.Text;
                            object result = _S71200.Read(mAddress);
                            txtReadValue.Text = string.Format("{0}", result.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                         MessageBox.Show(this, ex.Message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                  }

                void BtnWriteClick(object sender, EventArgs e)
                {
                    try
                    {
                        if (_S71200 != null)
                        {
                            string mAddress = txtMAddress.Text;
                            ushort value = Convert.ToUInt16(txtWriteValue.Text);
                            _S71200.Write(mAddress, value);
                        }
                    }
                    catch (Exception ex)
                    {
                            MessageBox.Show(this, ex.Message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
        }
}

Okunacak veya yazılacak değerlere göre C# kodları değişebiliyor.. Bu yazıda Word hafızalara okuma yazma yaptık.


Value conversion between C# and S7 plc 


 Read S7 Word: 

ushort result = (ushort)plc.Read("DB1.DBW0"); 


 Write S7 Word: 

ushort val = 40000; 

plc.Write("DB1.DBW0", val); 


 Read S7 Int / Dec, you need to use the method ConvertToShort(): 

short result = ((ushort)plc.Read("DB1.DBW0")).ConvertToShort(); 


 Write S7 Int / Dec, you need to use the method ConvertToUshort(): 

short value = -100; 

plc.Write("DB1.DBW0", value.ConvertToUshort()); 


 Read S7 DWord: 

uint result = (uint)plc.Read("DB1.DBD40"); 


 Write S7 DWord: 

uint val = 1000; 

plc.Write("DB1.DBD40", val); 


 Read S7 Dint, you need to use ConvertToInt(): 

int result2 = ((uint)plc.Read("DB1.DBD60")).ConvertToInt(); 


 Write S7 Dint: 

int value = -60000; 

plc.Write("DB1.DBD60", value); 


 Read S7 Real, you need to use ConvertToDouble(): 

double result = ((uint)plc.Read("DB1.DBD40")).ConvertToDouble(); 


 Write S7 Real, you need to use ConvertToInt(): 

double val = 35.687; 

plc.Write("DB1.DBD40", val.ConvertToUInt()); 


 Read bool from byte 

byte myByte = 5; // 0000 0101 

myByte.SelectBit(0) // true 

myByte.SelectBit(1) // false 


Dökümanda yazmayan db den bit okuma..

bool dbBit = (bool)plc.Read("DB1.DBX64.2");

 

Csharp ın içinde data tiplerini değiştirmek için örneğin text box tan aldığınız string değeri ushort a çevirmek için faydalı bir site buldum. Çevirilecek data tiplerini seçince otomatik olarak C# kodunu oluşturuyor.

Yorumlar

Bu blogdaki popüler yayınlar

VBA - Mscomm (seri port) ile veri loglama

RJ45 2 - Novexx barkod yazıcıya, S7-1200 plc ile etiket yazdırma

Uzak I/O modülü olarak PLC kullanmak