Code examples PLCcom for S7

The integration of PLCcom for S7 is very easy and user-friendly. 
In the following examples will show you the most important functionality.

You will find other documentations with more code examples in our example application in the downloadpackage or online:

Connect to PLC
  • C#
  • Visual Basic
  • Java
using PLCcom;

public class myTestClass
{
    //declare the Device 
    private PLCcomDevice Device;
    private void btnConnect_Click(object sender, System.EventArgs e)
    {
        //create TCP_ISO_Device instance from PLCcomDevice
        Device = new TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel);
        //or create MPI_Device instance from PLCcomDevice 
        //Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel); 
        //or create PPI_Device instance from PLCcomDevice 
        //Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);

        authentication.User = "";
        //Enter User here
        authentication.Serial = "";
        //Enter Serial here
        Device.Connecttimeout = 1000;
        Device.Readtimeout = 2000;
        ConnectResult res = Device.Connect();
        if (!res.Quality.Equals(OperationResult.eQuality.GOOD))
        {
            MessageBox.Show(res.Message);
        }
    }
}
Imports PLCcom

Public Class myTestClass
	Private Device As PLCcomDevice
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

            'create TCP_ISO_Device instance from PLCcomDevice
            Device = New TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel)
            'or create MPI_Device instance from PLCcomDevice 
            'Device = New MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel) 
            'or create PPI_Device instance from PLCcomDevice 
            'Device = New PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel)

           authentication.User = "" 'Enter User here
        authentication.Serial = "" 'Enter Serial here
        Device.IPAdress = "1.2.3.4" 'Enter PLC IP-Adress here
        Device.Rack = 0
        Device.Slot = 2
        Device.Connecttimeout = 1000
        Device.Readtimeout = 2000 
        Dim res As ConnectResult = Device.Connect()
        If Not res.Quality.Equals(OperationResult.eQuality.GOOD) Then
            MessageBox.Show(res.Message)
        End If 
    End Sub 
End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    //declare the Device
    PLCcomDevice Device;

    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
      //Connecting Device

        //create TCP_ISO_Device instance from PLCcomDevice
        Device = new TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel);
        //or create MPI_Device instance from PLCcomDevice
        //Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel);
        //or create PPI_Device instance from PLCcomDevice
        //Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);
        //Enter User here
        authentication.User("");
        //Enter Serial here
        authentication.Serial("");
        Device.setConnecttimeout(1000);
        Device.setReadtimeout(2000);
        ConnectResult res = Device.Connect();
        if (!res.Quality().equals(OperationResult.eQuality.GOOD)) {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.OK_CANCEL_OPTION);
        }
    }
}

Example: Connect an application to a PLC

Disconnect from PLC
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;

    class newClass
    {
        PLCcomDevice Device;

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            Device.DisConnect();
        }
    }
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice

    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        Device.DisConnect()
    End Sub

End Class
import PLCCom.*;

 public class main {
   //declare the Device
   private PLCcomDevice Device;

   // Disconnect Device
   private void btnDisConnectActionPerformed(java.awt.event.ActionEvent evt) {
       Device.DisConnect();
   }
 }

Example: Close the PLC connection

Automatic connect to plc
  • C#
  • Visual Basic
  • Java
using PLCcom;
 
public class myTestClass
{
    //declare the Device 
    private PLCcomDevice Device;
    private void btnConnect_Click(object sender, System.EventArgs e)
    {
        //create TCP_ISO_Device instance from PLCcomDevice
        Device = new TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel);
        //or create MPI_Device instance from PLCcomDevice 
        //Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel); 
        //or create PPI_Device instance from PLCcomDevice 
        //Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);
 
        authentication.User = "<Enter User here>";
        authentication.Serial = "<Enter Serial here>";
 
        //Set auto connect
        //The connection is opened automatically when it is needed. If after the expiry of the given period, no more requests are sent, the connection is automatically closed.
        Device.setAutoConnect(true, 10000);
    }
}
Imports PLCcom
 
Public Class myTestClass
    'declare the Device 
    Private Device As PLCcomDevice
    Private Sub btnConnect_Click(sender As Object, e As System.EventArgs)
        'create TCP_ISO_Device instance from PLCcomDevice
        Device = New TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel)
        'or create MPI_Device instance from PLCcomDevice 
        'Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel); 
        'or create PPI_Device instance from PLCcomDevice 
        'Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);
 
        authentication.User = "<Enter User here>"
        authentication.Serial = "<Enter Serial here>"
 
        'Set auto connect
        'The connection is opened automatically when it is needed. If after the expiry of the given period, no more requests are sent, the connection is automatically closed.
        Device.setAutoConnect(True, 10000)
    End Sub
End Class
import PLCCom.*;
import javax.swing.JOptionPane;
 
public class NewClass {
 
    //declare the Device
    PLCcomDevice Device;
 
    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
      //Connecting Device
 
        //create TCP_ISO_Device instance from PLCcomDevice
        Device = new TCP_ISO_Device("192.168.1.2", 0, 2, ePLCType.S7_300_400_compatibel);
        //or create MPI_Device instance from PLCcomDevice
        //Device = new MPI_Device("COM2", 0, 2, eBaudrate.b38400, eSpeed.Speed187k, ePLCType.S7_300_400_compatibel);
        //or create PPI_Device instance from PLCcomDevice
        //Device = new PPI_Device("COM2", 0, 2, eBaudrate.b9600, ePLCType.S7_200_compatibel);
        //Enter User here
        authentication.User("<enter user here>");
        //Enter Serial here
        authentication.Serial("<enter serial here>");
        Device.setConnecttimeout(1000);
        Device.setReadtimeout(2000);
         
        // Set Auto Connect State
        //The connection is opened automatically when it is needed. 
        //If after the expiry of the given period, no more requests are sent, the connection is automatically closed.
        Device.setAutoConnect(true, 10000));
    }
}

Example: The connection is opened automatically when it is needed. If after the expiry of the given period, no more requests are sent, the connection is automatically closed.

Reading data from PLC
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;
 
namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
 
        private void btnRead_Click(object sender, EventArgs e)
        {
            //declare a ReadRequest object
            //set the request parameters
            //read 10 Bytes from DB100
            ReadDataRequest myReadRequest = new ReadDataRequest(eRegion.DataBlock, 100, 0, eDataType.BYTE, 10);
 
            //read from device
            ReadDataResult res = Device.ReadData(myReadRequest);
 
            //evaluate results
            txtMessage.Text = res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                foreach (byte b in (byte[])res.GetValues())
                {
                    txtResult.Text += b.ToString() + Environment.NewLine;
                }
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom
Imports System.Windows.Forms
 
Namespace CodeDokuCSharp
    Class newClass
        Private Device As PLCcomDevice
 
        Private Sub btnRead_Click(sender As Object, e As EventArgs)
            'declare a ReadRequest object
            'set the request parameters
            'read 10 Bytes from DB100
            Dim myReadRequest As New ReadDataRequest(eRegion.DataBlock, 100, 0, eDataType.[BYTE], 10)
 
            'read from device
            Dim res As ReadDataResult = Device.ReadData(myReadRequest)
 
            'evaluate results
            txtMessage.Text = res.Message
            If res.Quality = OperationResult.eQuality.GOOD Then
                For Each b As Byte In DirectCast(res.GetValues(), Byte())
                    txtResult.Text += b.ToString() & Environment.NewLine
                Next
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End Sub
    End Class
End Namespace
import PLCCom.*;
 
 public class main {
 
   private void btnreadRawDataActionPerformed(java.awt.event.ActionEvent evt) {
    //set the request parameters
    //in this case => read 10 Bytes from DB1
    ReadDataRequest myReadDataRequest = new ReadDataRequest(eRegion.DataBlock,  //Region
                                                            1,                  //DB / only for datablock operations otherwise 0
                                                            0,                  //read start adress
                                                            eDataType.BYTE,     //desired datatype
                                                            10);                //Quantity of reading values
 
    //read from device
    System.out.println("begin Read...");
    ReadDataResult res = Device.readData(myReadDataRequest);
 
    //evaluate results
    if (res.Quality() == OperationResult.eQuality.GOOD) {
        int Position = 0;
        for (Object item : res.getValues()) {
            ystem.out.println("read Byte " + String.valueOf(Position) + " " + item.toString());
            Position++;
        }
    } else {
        System.out.println("read not successfull! Message: " + res.Message());
    }
 }
}

Example:

Read data from PLC. Read raw data from PLC buffer.

You will find more 'read' examples of specific data types (String, DINT, etc.) in our documentation or in our example application!

Reading data from multiple areas combined
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
 
    class newClass
    {
        PLCcomDevice Device;
 
        private void btnReadCollection_Click(object sender, EventArgs e)
        {
            //declare a ReadRequest object
            ReadDataRequestCollection RequestCollection = new ReadDataRequestCollection();
 
            #region Edit Collection
            //set request parameter
 
            //Example => add a read Request read 5 bits from datablock 1 at start byte 6 and bit 1 with item name Item1
            RequestCollection.AddReadDataRequest(new ReadDataRequest(eRegion.DataBlock, 1, 6, eDataType.BIT, 5, 1), "Item1");
 
            //Example => add a read Request read 20 bytes from datablock 204 at start byte 4 with item name Item2
            RequestCollection.AddReadDataRequest(new ReadDataRequest(eRegion.DataBlock, 204, 4, eDataType.BYTE, 20), "Item2");
 
            //Example => add a read Request read 230 bytes from markers at start byte 240 with item name Item3
            RequestCollection.AddReadDataRequest(new ReadDataRequest(eRegion.Flags_Markers, 0, 240, eDataType.BYTE, 230), "Item3");
 
            #endregion
 
            //set optimize flag to true for optimized acces to plc
            RequestCollection.Optimize_Mode = true;
 
            //read from device
            ReadDataResultCollection ResultCollection = Device.ReadData(RequestCollection);
 
            //evaluate results
            bool isItemAvalible = false;
            //Option 1 >>> find Itemresult with keyname
            ReadDataResult res1 = ResultCollection.GetResult("Item1", out isItemAvalible);
            ReadDataResult res2 = ResultCollection.GetResult("Item2", out isItemAvalible);
            ReadDataResult res3 = ResultCollection.GetResult("Item3", out isItemAvalible);
 
            txtMessage.Text = DateTime.Now.ToString() + ": " + ResultCollection.Message;
            txtResult.Text = string.Empty;
 
            //Option2 >>> find itemresult with foreach-loop
            StringBuilder sb = new StringBuilder();
            foreach (ReadDataResult res in ResultCollection.GetResults())
            {
                sb.Append("Start Item:");
                sb.Append(res.Itemkey);
                sb.Append(" ");
                sb.Append("Quality:");
                sb.Append(" ");
                sb.Append(res.Quality.ToString());
                sb.Append(Environment.NewLine);
 
                if (res.Quality == OperationResult.eQuality.GOOD)
                {
                    Array Values = res.GetValues();
                    for (int i = 0; i < Values.Length; i++)
                    {
                        sb.Append(Values.GetValue(i).ToString());
                        sb.Append(Environment.NewLine);
                    }
                    sb.Append("End Item:");
                    sb.Append(res.Itemkey);
                }
            }
            txtResult.Text = sb.ToString();
            sb.Append(Environment.NewLine);
            sb.Append(Environment.NewLine);
        }
    }
Imports PLCcom
 
    Class newClass
        Private Device As PLCcomDevice
 
        Private Sub btnReadCollection_Click(sender As Object, e As EventArgs)
            'declare a ReadRequest object
            Dim RequestCollection As New ReadDataRequestCollection()
 
            '#Region "Edit Collection"
            'set request parameter
 
            'Example => add a read Request read 5 bits from datablock 1 at start byte 6 and bit 1 with item name Item1
            RequestCollection.AddReadDataRequest(New ReadDataRequest(eRegion.DataBlock, 1, 6, eDataType.BIT, 5, 1), "Item1")
 
            'Example => add a read Request read 20 bytes from datablock 204 at start byte 4 with item name Item2
            RequestCollection.AddReadDataRequest(New ReadDataRequest(eRegion.DataBlock, 204, 4, eDataType.[BYTE], 20), "Item2")
 
            'Example => add a read Request read 230 bytes from markers at start byte 240 with item name Item3
            RequestCollection.AddReadDataRequest(New ReadDataRequest(eRegion.Flags_Markers, 0, 240, eDataType.[BYTE], 230), "Item3")
 
            '#End Region
 
            'set optimize flag to true for optimized acces to plc
            RequestCollection.Optimize_Mode = True
 
            'read from device
            Dim ResultCollection As ReadDataResultCollection = Device.ReadData(RequestCollection)
 
            'evaluate results
            Dim isItemAvalible As Boolean = False
            'Option 1 >>> find Itemresult with keyname
            Dim res1 As ReadDataResult = ResultCollection.GetResult("Item1", isItemAvalible)
            Dim res2 As ReadDataResult = ResultCollection.GetResult("Item2", isItemAvalible)
            Dim res3 As ReadDataResult = ResultCollection.GetResult("Item3", isItemAvalible)
 
            txtMessage.Text = (DateTime.Now.ToString() & ": ") + ResultCollection.Message
            txtResult.Text = String.Empty
 
            'Option2 >>> find itemresult with foreach-loop
            Dim sb As New StringBuilder()
            For Each res As ReadDataResult In ResultCollection.GetResults()
                sb.Append("Start Item:")
                sb.Append(res.Itemkey)
                sb.Append(" ")
                sb.Append("Quality:")
                sb.Append(" ")
                sb.Append(res.Quality.ToString())
                sb.Append(Environment.NewLine)
 
                If res.Quality = OperationResult.eQuality.GOOD Then
                    Dim Values As Array = res.GetValues()
                    For i As Integer = 0 To Values.Length - 1
                        sb.Append(Values.GetValue(i).ToString())
                        sb.Append(Environment.NewLine)
                    Next
                    sb.Append("End Item:")
                    sb.Append(res.Itemkey)
                End If
            Next
            txtResult.Text = sb.ToString()
            sb.Append(Environment.NewLine)
            sb.Append(Environment.NewLine)
        End Sub
    End Class
import PLCCom.*;
 
 public class main {
 //declare the Device
 private PLCcomDevice Device;
 
 // read on PLC
    private void btnreadCollectionActionPerformed(java.awt.event.ActionEvent evt) {
 
        try {
            //declare a ReadRequest object
            JOptionPane.showMessageDialog(null, resources.getString("Message_ReadDataCollection"), "", JOptionPane.INFORMATION_MESSAGE);
 
            ReadDataRequestCollection RequestCollection = new ReadDataRequestCollection();
 
            //Example => add a read Request read 230 bytes from markers at start byte 240 with item name Item3
            RequestCollection.AddReadDataRequest(new ReadDataRequest(eRegion.Flags_Markers, 0, 240, eDataType.BYTE, 230), "Item3");
 
            // @formatter:off
            //Example => add a read Request read 5 bits from datablock 1 at start byte 6 and bit 1 with item name Item1
            ReadDataRequest RequestItem = new ReadDataRequest((eRegion) eRegion.DataBlock, // Region
                                            Integer.valueOf(1), // DB only for datablock operations otherwise  0
                                            Integer.valueOf(6), // Read start address
                                            eDataType.BIT, // Target Datatype
                                            Integer.valueOf(5), // quantity of objects to be read
                                            Byte.valueOf((byte) 1)); // Address of first Bit
 
            // @formatter:on
 
            // add new request to request collection
            RequestCollection.addReadDataRequest(RequestItem, "Item1");
 
            // @formatter:off
            //Example => add a read Request read 20 bytes from datablock 204 at start byte 4 with item name Item2
            ReadDataRequest RequestItem = new ReadDataRequest((eRegion) eRegion.DataBlock, // Region
                                            Integer.valueOf(204), // DB only for datablock operations otherwise  0
                                            Integer.valueOf(4), // Read start address
                                            eDataType.BYTE, // Target Datatype
                                            Integer.valueOf(20)); // quantity of objects to be read
 
            // @formatter:on
 
            // add new request to request collection
            RequestCollection.addReadDataRequest(RequestItem, "Item2");
 
 
            // @formatter:off
            //Example => add a read Request read 230 bytes from markers at start byte 240 with item name Item3
            ReadDataRequest RequestItem = new ReadDataRequest((eRegion) eRegion.Flags_Markers, // Region
                                            Integer.valueOf(0), // DB only for datablock operations otherwise  0
                                            Integer.valueOf(240), // Read start address
                                            eDataType.BYTE, // Target Datatype
                                            Integer.valueOf(230)); // quantity of objects to be read
 
            // @formatter:on
 
            // add new request to request collection
            RequestCollection.addReadDataRequest(RequestItem, "Item3");
            // 
            RequestCollection.setOptmize(true);
 
            //read from device
            ReadDataResultCollection ResultCollection = Device.readData(RequestCollection);
 
            //evaluate results
            Boolean[] isItemAvalible = new Boolean[1];
 
            for (ReadItemResult res : ResultCollection.getResults()) {
                txtResult.append("Start Item:");
                txtResult.append(res.Itemkey());
                txtResult.append(" ");
                txtResult.append("Quality:");
                txtResult.append(" ");
                txtResult.append(res.Quality().toString());
                txtResult.append(System.getProperty("line.separator"));
 
                if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
                    Object[] Values = (Object[]) res.getValue();
                    for (Object val : Values) {
                        txtResult.append(val.toString());
                        txtResult.append(System.getProperty("line.separator"));
                    }
                    txtResult.append("End Item:");
                    txtResult.append(res.Itemkey());
                }
            }
            txtResult.append(System.getProperty("line.separator"));
            txtResult.append(System.getProperty("line.separator"));
        } catch (Exception ex) {
        }
    }
 }

Example:

Read data from diffrent regions in one collection. Creating a 'ReadRequestCollection'. The device receive this collection for reading. The necessary reading accesses will be automatically reduced and optimized.

You will find more 'read' examples of specific data types (String, DINT, etc.) in our documentation or in our example application!

Writing data in PLC
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;
 
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device
 
        private void btnWrite_Click(object sender, EventArgs e)
        {
            //declare a WriteDataRequest object and
            //write 4 bytes in DB100 at Startbyte 0
            WriteDataRequest myWriteRequest = new WriteDataRequest(eRegion.DataBlock, 100, 0);
            //add writable Data here
            myWriteRequest.addByte(new byte[] { 11, 12, 13, 14 });
            //write
            WriteDataResult res = Device.WriteData(myWriteRequest);
 
            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality.Equals(OperationResult.eQuality.GOOD))
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
Imports PLCcom
 
Class newClass
    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device
 
    Private Sub btnWrite_Click(sender As Object, e As EventArgs)
        'declare a WriteDataRequest object and
        'write 4 bytes in DB100 at Startbyte 0
        Dim myWriteRequest As New WriteDataRequest(eRegion.DataBlock, 100, 0)
        'add writable Data here
        myWriteRequest.addByte(New Byte() {11, 12, 13, 14})
        'write
        Dim res As WriteDataResult = Device.WriteData(myWriteRequest)
 
        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class
using PLCcom.*;
     
        private PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device
 
        private void btnWrite_Click(object sender, EventArgs e)
        {
             
             
            WriteDataRequest myWriteRequest = new WriteDataRequest(eRegion.DataBlock,       // Region
                                            Integer.valueOf(100),           // DB / only for data block operations  otherwise 0
                                            Integer.valueOf(0),     // write start address
                                            Byte.valueOf(txtBit.getText()));            // Bit/only for bit operations
             
            //set the request parameters 
            //write 4 bytes in DB100
            WriteRequest[] myWriteRequest = new WriteRequest[1];
            myWriteRequest[0] = new WriteRequest();
 
            //add writable Data here
            myWriteRequest.addByte(new byte[] { 11, 12, 13, 14 });
            //write
            WriteDataResult res = Device.writeData(myWriteRequest);
 
            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res[0].Message;
            if (res[0].Quality.Equals(OperationResult.eQuality.GOOD))
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res[0].Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

Example: 
Writing raw data to PLC.

You will find more 'write' examples of specific data types (String, DINT, etc.) in our documentation or in our example application!

Get basic info from PLC
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void getGetPLCBasicInfo_Click(object sender, EventArgs e)
        {
            //execute function
            BasicInfoResult res = Device.GetBasicInfo();

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Device Name: ");
                sb.Append(res.Name);
                sb.Append(Environment.NewLine);
                sb.Append("Order Number: ");
                sb.Append(res.OrderNumber);
                sb.Append(Environment.NewLine);
                sb.Append("Module Version: ");
                sb.Append(res.ModuleVersion);
                sb.Append(Environment.NewLine);
                sb.Append("Firmware Version: ");
                sb.Append(res.FirmwareVersion);
                sb.Append(Environment.NewLine);
                txtResult.Text = sb.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub getGetPLCBasicInfo_Click(sender As Object, e As EventArgs) Handles getGetPLCBasicInfo.Click
        'execute function
        Dim res As BasicInfoResult = Device.GetBasicInfo()

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality = OperationResult.eQuality.GOOD Then
            Dim sb As New System.Text.StringBuilder()
            sb.Append("Device Name: ")
            sb.Append(res.Name)
            sb.Append(Environment.NewLine)
            sb.Append("Order Number: ")
            sb.Append(res.OrderNumber)
            sb.Append(Environment.NewLine)
            sb.Append("Module Version: ")
            sb.Append(res.ModuleVersion)
            sb.Append(Environment.NewLine)
            sb.Append("Firmware Version: ")
            sb.Append(res.FirmwareVersion)
            sb.Append(Environment.NewLine)
            txtResult.Text = sb.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btngetPLCBasicInfoActionPerformed(java.awt.event.ActionEvent evt) {
        //execute function
        BasicInfoResult res = Device.GetBasicInfo();

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            StringBuilder sb = new StringBuilder();
            sb.append("Device Name: ");
            sb.append(res.Name());
            sb.append(System.getProperty("line.separator"));
            sb.append("Order Number: ");
            sb.append(res.Ordernumber());
            sb.append(System.getProperty("line.separator"));
            sb.append("Module Version: ");
            sb.append(res.ModuleVersion());
            sb.append(System.getProperty("line.separator"));
            sb.append("Firmware Version: ");
            sb.append(res.FirmwareVersion());
            sb.append(System.getProperty("line.separator"));
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Reading basic PLC imformations like order number, model, firmware, etc.

Get mode and state from CPU
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnGetCPUMode_Click(object sender, EventArgs e)
        {
            //execute function
            CPUModeInfoResult res = Device.GetCPUMode();

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                txtResult.Text = "CPU Mode = " + res.CPUModeInfo.ToString() + Environment.NewLine + "CPU State = " + res.CPUStateInfo.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnGetCPUMode_Click(sender As Object, e As EventArgs) Handles btnGetCPUMode.Click
        'execute function
        Dim res As CPUModeInfoResult = Device.GetCPUMode()

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality = OperationResult.eQuality.GOOD Then
            txtResult.Text = ("CPU Mode = " & res.CPUModeInfo.ToString()) + Environment.NewLine & "CPU State = " & res.CPUStateInfo.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btngetCPUModeActionPerformed(java.awt.event.ActionEvent evt) {
        //execute function
        CPUModeInfoResult res = Device.GetCPUMode();

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            StringBuilder sb = new StringBuilder();
            sb.append("CPU Mode = ");
            sb.append(String.valueOf(res.CPUModeInfo()));
            sb.append(System.getProperty("line.separator"));
            sb.append("CPU State = ");
            sb.append(String.valueOf(res.CPUStateInfo()));
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Reading PLC operating mode and status (run, stop, key position, etc.)

Get or set PLC time
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        #region getPLCClockTime

        private void btnGetPLCClockTime_Click(object sender, EventArgs e)
        {
            //execute function
            PLCClockTimeResult res = Device.GetPLCClockTime();

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality.Equals(OperationResult.eQuality.GOOD))
            {
                txtResult.Text = res.PLCClockTime.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region setPLCClockTime

        private void btnSetPLCClockTime_Click(object sender, EventArgs e)
        {
            //execute function
            OperationResult res = Device.SetPLCClockTime(DateTime.Now);

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality.Equals(OperationResult.eQuality.GOOD))
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device


#Region "getPLCClockTime"

    Private Sub btnGetPLCClockTime_Click(sender As Object, e As EventArgs) Handles btnGetPLCClockTime.Click
        'execute function
        Dim res As PLCClockTimeResult = Device.GetPLCClockTime()

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
            txtResult.Text = res.PLCClockTime.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

#End Region


#Region "setPLCClockTime"

    Private Sub btnSetPLCClockTime_Click(sender As Object, e As EventArgs) Handles btnSetPLCClockTime.Click
        'execute function
        Dim res As OperationResult = Device.SetPLCClockTime(DateTime.Now)

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

#End Region

End Class
import PLCCom.*;
import java.util.Calendar;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

	//read PLC time
    private void btnGetPLCTimeActionPerformed(java.awt.event.ActionEvent evt) {

        //execute function
        PLCClockTimeResult res = Device.GetPLCClockTime();

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            Calendar c = res.getPLCClockTime();
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    //set PLC time
    private void btnSetPLCTimeActionPerformed(java.awt.event.ActionEvent evt) {
        Calendar data = new GregorianCalendar();
        //adjust and set hour
        data.set(Calendar.HOUR_OF_DAY, data.get(Calendar.HOUR_OF_DAY) - 0);
        //execute function
        OperationResult res = Device.SetPLCClockTime(data);

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
    
}

Example:
Get / set PLC time

Reading system status list ssl
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnGetSSL_Click(object sender, EventArgs e)
        {
            // important!!! please search the id and index information in the plc-documentation
            // You must convert the specified values hex in decimal
            int SSL_ID = 306; //ID 132 (Hex) 
            int SSL_Index = 4; //Index 4 (Hex) 
            //execute function
            SystemStatusListResult res = Device.GetSystemStatusList(SSL_ID, SSL_Index);

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (SystemStatusListResult.SystemStatusListItemEntry ssle in res.SZLItemEntrys)
                {
                    foreach (byte b in ssle.buffer)
                    {
                        sb.Append(b.ToString());
                        sb.Append(" ");
                    }
                    sb.Append(Environment.NewLine);
                }
                txtResult.Text = sb.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnGetSSL_Click(sender As Object, e As EventArgs) Handles btnGetSSL.Click
        ' important!!! please search the id and index information in the plc-documentation
        ' You must convert the specified values hex in decimal
        Dim SSL_ID As Integer = 306
        'ID 132 (Hex) 
        Dim SSL_Index As Integer = 4
        'Index 4 (Hex) 
        'execute function
        Dim res As SystemStatusListResult = Device.GetSystemStatusList(SSL_ID, SSL_Index)

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality = OperationResult.eQuality.GOOD Then
            Dim sb As New System.Text.StringBuilder()
            For Each ssle As SystemStatusListResult.SystemStatusListItemEntry In res.SZLItemEntrys
                For Each b As Byte In ssle.buffer
                    sb.Append(b.ToString())
                    sb.Append(" ")
                Next
                sb.Append(Environment.NewLine)
            Next
            txtResult.Text = sb.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

        private void btnReadSSL_SZLActionPerformed(java.awt.event.ActionEvent evt) {

        // important!!! please search the id and index information in the plc-documentation
        // You must convert the specified values hex in decimal
        int SSL_ID = 306; //ID 132 (Hex)
        int SSL_Index = 4; //Index 4 (Hex)
        //execute function
        SystemStatusListResult res = Device.GetSystemStatusList(SSL_ID, SSL_Index);

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            StringBuilder sb = new StringBuilder();
            for (SystemStatusListItemEntry ssle : res.getSZLItemEntrys()) {
                for (byte b : ssle.getBuffer()) {
                    sb.append(String.valueOf(b));
                    sb.append(" ");
                }
                sb.append(System.getProperty("line.separator"));
            }
        JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
read data from system state list (if existing)

Get diagnostic data
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnDiagnosticInfo_Click(object sender, EventArgs e)
        {
            //read the diagnosticinfo into DiagnosticInfoResult-object
            //execute function
            DiagnosticInfoResult res = Device.GetDiagnosticInfo();

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                //step through the entries
                foreach (DiagnosticInfoEntry myDiagnosticInfoEntry in res.DiagnosticInfoEntrys)
                {
                    sb.Append("Timestamp: " + myDiagnosticInfoEntry.DiagnosticTimestamp.ToString());
                    sb.Append(" ");
                    sb.Append("ID: " + myDiagnosticInfoEntry.DiagnosticID.ToString());
                    sb.Append(" ");
                    sb.Append("Message: " + myDiagnosticInfoEntry.DiagnosticText);
                    sb.Append(Environment.NewLine);
                }
                txtResult.Text = sb.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnDiagnosticInfo_Click(sender As Object, e As EventArgs) Handles btnDiagnosticInfo.Click
        'read the diagnosticinfo into DiagnosticInfoResult-object
        'execute function
        Dim res As DiagnosticInfoResult = Device.GetDiagnosticInfo()

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality = OperationResult.eQuality.GOOD Then
            Dim sb As New System.Text.StringBuilder()
            'step through the entries
            For Each myDiagnosticInfoEntry As DiagnosticInfoEntry In res.DiagnosticInfoEntrys
                sb.Append("Timestamp: " & myDiagnosticInfoEntry.DiagnosticTimestamp.ToString())
                sb.Append(" ")
                sb.Append("ID: " & myDiagnosticInfoEntry.DiagnosticID.ToString())
                sb.Append(" ")
                sb.Append("Message: " + myDiagnosticInfoEntry.DiagnosticText)
                sb.Append(Environment.NewLine)
            Next
            txtResult.Text = sb.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btnDiagnoseBufferActionPerformed(java.awt.event.ActionEvent evt) {

        //read the diagnosticinfo into DiagnosticInfoResult-object
        //execute function
        DiagnosticInfoResult res = Device.GetDiagnosticInfo();

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            //step through the entries
            StringBuilder sb = new StringBuilder();
            for (DiagnosticInfoEntry myDiagnosticInfoEntry : res.getDiagnosticInfoEntrys()) {
                sb.append("Timestamp: ");
                sb.append(myDiagnosticInfoEntry.getDiagnosticTimestamp().toString());
                sb.append(" ");
                sb.append("ID: ");
                sb.append(String.valueOf(myDiagnosticInfoEntry.getDiagnosticID()));
                sb.append(" ");
                sb.append("Message: ");
                sb.append(myDiagnosticInfoEntry.getDiagnosticText());
                sb.append(System.getProperty("line.separator"));
            }
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
read diagnostic data from PLC

Send password
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnSendPW_Click(object sender, EventArgs e)
        {
            OperationResult res = Device.sendPassWord("EnterPW");

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnSendPW_Click(sender As Object, e As EventArgs) Handles btnSendPW.Click
        Dim res As OperationResult = Device.sendPassWord("EnterPW")

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality = OperationResult.eQuality.GOOD Then
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

        private void btnsendPWActionPerformed(java.awt.event.ActionEvent evt) {

        OperationResult res = Device.sendPassWord("EnterPW");
        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
send password to unlock connection of password-protected PLCs

Start and stop a PLC
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        #region StartPLC

        private void btnStartPLC_Click(object sender, EventArgs e)
        {
            //execute function
            OperationResult res = Device.StartPLC();

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality.Equals(OperationResult.eQuality.GOOD))
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion


        #region StopPLC

        private void btnStopPLC_Click(object sender, EventArgs e)
        {
            //execute function
            OperationResult res = Device.StopPLC();

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality.Equals(OperationResult.eQuality.GOOD))
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

#Region "StartPLC"

    Private Sub btnStartPLC_Click(sender As Object, e As EventArgs) Handles btnStartPLC.Click
        'execute function
        Dim res As OperationResult = Device.StartPLC()

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

#End Region

#Region "StopPLC"

    Private Sub btnStopPLC_Click(sender As Object, e As EventArgs) Handles btnStopPLC.Click
        'execute function
        Dim res As OperationResult = Device.StopPLC()

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality.Equals(OperationResult.eQuality.GOOD) Then
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

#End Region

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

	//Start PLC
    private void btnwriteRawDataActionPerformed(java.awt.event.ActionEvent evt) {

        //execute function
        OperationResult res = Device.StartPLC();

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    //Stop PLC
    private void btnwriteRawDataActionPerformed(java.awt.event.ActionEvent evt) {

        //execute function
        OperationResult res = Device.StopPLC();

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Start / stop PLC (RUN >> STOP or STOP >> RUN)

Reading block list from PLC
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnGetBlockInfo_Click(object sender, EventArgs e)
        {

            eBlockType BlockType = eBlockType.AllBlocks;

            BlockListResult res = Device.GetBlockList(BlockType);

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            txtResult.Text = string.Empty;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                StringBuilder sb = new StringBuilder();
                foreach (BlockListEntry ble in res.BlockList)
                {
                    sb.Append(ble.BlockType.ToString());
                    sb.Append(ble.BlockNumber.ToString());
                    sb.Append(Environment.NewLine);
                }
                txtResult.Text = sb.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnGetBlockList_Click(sender As Object, e As EventArgs) Handles btnGetBlockList.Click

        Dim BlockType As eBlockType = eBlockType.AllBlocks

        Dim res As BlockListResult = Device.GetBlockList(BlockType)

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        txtResult.Text = String.Empty
        If res.Quality = OperationResult.eQuality.GOOD Then
            Dim sb As New System.Text.StringBuilder()
            For Each ble As BlockListEntry In res.BlockList
                sb.Append(ble.BlockType.ToString())
                sb.Append(ble.BlockNumber.ToString())
                sb.Append(Environment.NewLine)
            Next
            txtResult.Text = sb.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btngetPLCBlockListActionPerformed(java.awt.event.ActionEvent evt) {

        eBlockType BlockType = eBlockType.AllBlocks;
        BlockListResult res = Device.GetBlockList(BlockType);

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            StringBuilder sb = new StringBuilder();
            for (BlockListEntry ble : res.getBlockList()) {
                sb.append(ble.getBlockType().toString());
                sb.append(String.valueOf(ble.getBlockNumber()));
                sb.append(System.getProperty("line.separator"));
            }
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Read a list of created PLC objects. This example reads blocks. Additionally you can choose a specific block type (OB, DB, etc.)

Get length of block
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnGetBlockLen_Click(object sender, EventArgs e)
        {
            //get Len from DB100
            int BlockNumber = 100;
            eBlockType BlockType = eBlockType.DB;

            //evaluate results
            BlockListLengthResult res = Device.GetBlockLenght(BlockType, BlockNumber);

            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            txtMessage.ForeColor = res.Quality == OperationResult.eQuality.GOOD ? Color.Black : Color.Red;
            txtResult.Text = string.Empty;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(res.BlockType.ToString());
                sb.Append(res.BlockNumber.ToString());
                sb.Append(" Len:");
                sb.Append(res.BlockLength.ToString());
                sb.Append(Environment.NewLine);

                txtResult.Text = sb.ToString();
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnGetBlockLen_Click(sender As Object, e As EventArgs) Handles btnGetBlockLen.Click
        'get Len from DB100
        Dim BlockNumber As Integer = 100
        Dim BlockType As eBlockType = eBlockType.DB

        'evaluate results
        Dim res As BlockListLengthResult = Device.GetBlockLenght(BlockType, BlockNumber)

        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        txtMessage.ForeColor = If(res.Quality = OperationResult.eQuality.GOOD, Color.Black, Color.Red)
        txtResult.Text = String.Empty
        If res.Quality = OperationResult.eQuality.GOOD Then
            Dim sb As New System.Text.StringBuilder()
            sb.Append(res.BlockType.ToString())
            sb.Append(res.BlockNumber.ToString())
            sb.Append(" Len:")
            sb.Append(res.BlockLength.ToString())
            sb.Append(Environment.NewLine)

            txtResult.Text = sb.ToString()
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btnBlockLenActionPerformed(java.awt.event.ActionEvent evt) {

        //get Len from DB100
        int BlockNumber = 100;
        eBlockType BlockType = eBlockType.AllBlocks;

        //evaluate results
        BlockListLengthResult res = Device.GetBlockLenght(BlockType, BlockNumber);

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            StringBuilder sb = new StringBuilder();
            sb.append(res.getBlockType().toString());
            sb.append(String.valueOf(res.getBlockNumber()));
            sb.append(" Len:");
            sb.append(String.valueOf(res.getBlockLength()));
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Read lenght of a specific block.

Backup block
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnBackupBlock_Click(object sender, EventArgs e)
        {

            //Backup OB1
            int BlockNumber = 1;
            eBlockType Blocktype = eBlockType.OB;

            //open SaveFileDialog
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*";
            DialogResult dr = sfd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //read Block into ReadPLCBlockResult
                ReadPLCBlockResult res = Device.ReadPLCBlock_MC7(Blocktype, BlockNumber);
                txtMessage.Text = res.Message;
                txtResult.Text = "";
                if (res.Quality == OperationResult.eQuality.GOOD)
                {
                    //save buffer in specified file
                    System.IO.FileStream fs = new System.IO.FileStream(sfd.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    fs.Write(res.Buffer, 0, res.Buffer.Length);
                    fs.Close();
                    MessageBox.Show("Block " + Blocktype.ToString() + BlockNumber.ToString() + " successful saved in " + sfd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("operation unsuccessful", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnBackupBlock_Click(sender As Object, e As EventArgs) Handles btnBackupBlock.Click

        'Backup OB1
        Dim BlockNumber As Integer = 1
        Dim Blocktype As eBlockType = eBlockType.OB

        'open SaveFileDialog
        Dim sfd As New SaveFileDialog()
        sfd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*"
        Dim dr As DialogResult = sfd.ShowDialog()
        If dr = DialogResult.OK Then
            'read Block into ReadPLCBlockResult
            Dim res As ReadPLCBlockResult = Device.ReadPLCBlock_MC7(Blocktype, BlockNumber)
            txtMessage.Text = res.Message
            txtResult.Text = ""
            If res.Quality = OperationResult.eQuality.GOOD Then
                'save buffer in specified file
                Dim fs As New System.IO.FileStream(sfd.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)
                fs.Write(res.Buffer, 0, res.Buffer.Length)
                fs.Close()
                MessageBox.Show(("Block " & Blocktype.ToString() & BlockNumber.ToString() & " successful saved in ") + sfd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("operation unsuccessful", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Else
            MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

End Class
import PLCCom.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btnBackup_BlockActionPerformed(java.awt.event.ActionEvent evt) {

        //Backup OB1
        int BlockNumber = 1;
        eBlockType BlockType = eBlockType.OB;

        //open SaveFileDialog
        final JFileChooser dr = new JFileChooser();
        FileFilter filter = new FileNameExtensionFilter("Binary Files *.mc7", "mc7");
        dr.addChoosableFileFilter(filter);
        int returnVal = dr.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //read Block into ReadPLCBlockResult
            ReadPLCBlockResult res = Device.ReadPLCBlock_MC7(BlockType, BlockNumber);

            //evaluate results
            if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
                try {
                    //save buffer in specified file
                    File file = new File(dr.getSelectedFile().getAbsolutePath());

                    //rename file to .mc7, you can adjust the extension
                    if (!file.getAbsolutePath().endsWith(".mc7")) {
                        file = new File(dr.getSelectedFile().getAbsolutePath() + ".mc7");
                    }
                    // if file doesn´t exists, then create it
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    FileOutputStream fs = new FileOutputStream(file);
                    fs.write(res.getBuffer());
                    fs.close();
                    JOptionPane.showMessageDialog(null, "Block " + String.valueOf(String.valueOf(BlockType)) + String.valueOf(BlockNumber) + resources.getString("successful_saved") + dr.getSelectedFile().getName(), "", JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "operation unsuccessful", "", JOptionPane.ERROR_MESSAGE);
                }
                JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "operation aborted", "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Read a specific block in MC7-format and save to a select file.

Among other things you can find block informations (language, author, etc.(plain text)) in the ReadPLCBlockResult's subobject 'BlockInfo'.

Restore block
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnRestoreBlock_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {

                System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                fs.Close();

                //Write Buffer into PLC
                WritePLCBlockRequest Requestdata = new WritePLCBlockRequest(buffer, eBlockType.OB, 1);
                OperationResult res = Device.WritePLCBlock_MC7(Requestdata);
                txtMessage.Text = res.Message;
                txtResult.Text = "";
                if (res.Quality == OperationResult.eQuality.GOOD)
                {

                    MessageBox.Show("Block " + Requestdata.BlockInfo.Header.BlockType.ToString() + Requestdata.BlockInfo.Header.BlockNumber.ToString() + " successful saved in PLC from Source " + ofd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("operation unsuccessful", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnRestoreBlock_Click(sender As Object, e As EventArgs) Handles btnRestoreBlock.Click
        Dim ofd As New OpenFileDialog()
        ofd.Filter = "*.mc7|*.mc7|*.bin|*.bin|*.*|*'.*"
        Dim dr As DialogResult = ofd.ShowDialog()
        If dr = DialogResult.OK Then

            Dim fs As New System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, CInt(fs.Length))
            fs.Close()

            'Write Buffer into PLC
            Dim Requestdata As New WritePLCBlockRequest(buffer, eBlockType.OB, 1)
            Dim res As OperationResult = Device.WritePLCBlock_MC7(Requestdata)
            txtMessage.Text = res.Message
            txtResult.Text = ""
            If res.Quality = OperationResult.eQuality.GOOD Then

                MessageBox.Show(("Block " & Requestdata.BlockInfo.Header.BlockType.ToString() & Requestdata.BlockInfo.Header.BlockNumber.ToString() & " successful saved in PLC from Source ") + ofd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("operation unsuccessful", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Else
            MessageBox.Show("operation aborted", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

End Class
import PLCCom.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btnRestore_BlockActionPerformed(java.awt.event.ActionEvent evt) {
        final JFileChooser dr = new JFileChooser();
        FileFilter filter = new FileNameExtensionFilter("Binary Files *.mc7, *.bin", "mc7", "bin");
        dr.addChoosableFileFilter(filter);
        int returnVal = dr.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = new File(dr.getSelectedFile().getAbsolutePath());
                if (!file.exists()) {
                    JOptionPane.showMessageDialog(null, "operation unsuccessful, file not exist", "", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                InputStream is = null;
                byte[] buffer = null;
                try {
                    is = new BufferedInputStream(new FileInputStream(file));
                    buffer = new byte[is.available()];
                    is.read(buffer);
                } finally {
                    is.close();
                }
                WritePLCBlockRequest Requestdata = new WritePLCBlockRequest(buffer);
                //Write Buffer into PLC
                OperationResult res = Device.WritePLCBlock_MC7(Requestdata);

                //evaluate results
                if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
                    JOptionPane.showMessageDialog(null, "Block " + String.valueOf(Requestdata.getBlockInfo().getHeader().getBlockType()) + String.valueOf(Requestdata.getBlockInfo().getHeader().getBlockNumber()) + "successful saved in PLC! File:" + dr.getSelectedFile().getName(), "", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "operation unsuccessful", "", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "operation aborted", "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Restore block data from a selectable MC7-file.

It is possible to modify the target inforations. E.g. save DB100 to DB200.

Delete block
  • C#
  • Visual Basic
  • Java
using System;
using PLCcom;
using System.Windows.Forms;

namespace CodeDokuCSharp
{
    class newClass
    {
        PLCcomDevice Device;
        //see section 'connect' for declare and connect a PLCcom-Device

        private void btnDeleteBlock_Click(object sender, EventArgs e)
        {
            //Delete DB100
            int BlockNumber = 100;
            eBlockType BlockType = eBlockType.DB;

            OperationResult res = Device.DeleteBlock(BlockType, BlockNumber);
            //evaluate results
            txtMessage.Text = DateTime.Now.ToString() + ": " + res.Message;
            if (res.Quality == OperationResult.eQuality.GOOD)
            {
                MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
Imports PLCcom

Public Class newClass

    Private Device As PLCcomDevice
    'see section 'connect' for declare and connect a PLCcom-Device

    Private Sub btnDeleteBlock_Click(sender As Object, e As EventArgs) Handles btnDeleteBlock.Click
        'Delete DB100
        Dim BlockNumber As Integer = 100
        Dim BlockType As eBlockType = eBlockType.DB

        Dim res As OperationResult = Device.DeleteBlock(BlockType, BlockNumber)
        'evaluate results
        txtMessage.Text = (DateTime.Now.ToString() & ": ") + res.Message
        If res.Quality = OperationResult.eQuality.GOOD Then
            MessageBox.Show("OK", "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(res.Message, "Result:", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

End Class
import PLCCom.*;
import javax.swing.JOptionPane;

public class NewClass {

    PLCcomDevice Device;
    //see section 'connect' for declare and connect a PLCcom-Device

    private void btnDeleteBlockActionPerformed(java.awt.event.ActionEvent evt) {

        //Delete DB100
        int BlockNumber = 100;
        eBlockType BlockType = eBlockType.DB;

        OperationResult res = Device.DeleteBlock(BlockType, BlockNumber);

        //evaluate results
        if (res.Quality().equals(OperationResult.eQuality.GOOD)) {
            JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Example:
Delete a specific block.