C# app to/from PLC

ADS is a device-independent software module that can be used to communicate with PLC. We don’t need to know the inner working of the DLL (dynamic link library), we just need to understand how we can use the library to read data from the PLC or writing data to PLC. The DLL (TwinCAT.Ads.dll) can be downloaded from the Beckhoff site or it can be downloaded from the sample provided in this section. We are using the .NET version of the DLL. We have organized the sample in 4 projects in the Visual Studio solutions

=>PlcDataManager – a wrapper DLL that wraps the TwinCAT.Ads.dll  for easy usages in my application.

=>TwinCATProject1 – a TwinCAT PLC program that we use to test the sample

=>WindowsFormsDirectADS – a Windows Form application that uses directly the ADS library provided by Beckhoff

=>WindowsFormTestWithDLL a Windows Form application that uses the PLCDataManager library

See also https://www.hemelix.com/scada-hmi/twincat-hmi/beckhoff-pc-plc-communication-module-development/

 

 

How to display data to the UI when it appears from a different thread.

        private void adsClient_AdsNotificationEx(object sender, AdsNotificationExEventArgs e)
        {
            if (Convert.ToInt32(e.UserData) == handleBool)
            {
                bool b = Convert.ToBoolean(e.Value);
                Invoke(new Action(() =>
                {
                    textBox_bool.Text = b.ToString();
                }));
            }
            if (Convert.ToInt32(e.UserData) == handleInteger)
            {
                int intData = Convert.ToInt32(e.Value);
                Invoke(new Action(() =>
                {
                    textBox_int.Text = intData.ToString();
                }));
            }
            if (Convert.ToInt32(e.UserData) == handleReal)
            {
                float floatData = Convert.ToSingle(e.Value);
                Invoke(new Action(() =>
                {
                    textBox_real.Text = floatData.ToString();
                }));
            }
            if (Convert.ToInt32(e.UserData) == handleString)
            {
                string stringData = Convert.ToString(e.Value);
                Invoke(new Action(() =>
                {
                    textBox_string.Text = stringData;
                }));
            }
        }
 

 

How can we read data from PLC or how can we write data to PLC and display over ADS. We shall explain it  by the following sample.

Download the windows form application developed with C# ADS_Windows_Example_Hemelix.zip

TIPs

=> If you run the test sample on your PC then the PLC and PC should be in the same network (same LAN or VPN)

=> The PLC  AMS Net ID should be added to the PC, more https://www.hemelix.com/automation/structured-text-how-to-add-route-for-plc/