Wednesday, February 1, 2023

Contoh Mengakses PLC Dengan Java - I

 

Programmable Logic Controller, disingkat PLC merupakan peralatan elektronik yang dibangun dari mikroprosesor untuk memonitor keadaan dari peralatan input untuk kemudian di analisa sesuai dengan kebutuhan perencana (programmer) untuk mengontrol keadaan output. Sinyal input diberikan kedalam input card.
Ada 2 jenis input card, yaitu :
  • Analog input card
  • Digital input card

Contoh untuk mengakses PLC :

1. Connect to PLC

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);
         
        authentication.User("");//Enter User here
        authentication.Serial("");//Enter Serial here
         
        Device.setConnecttimeout(1000);
        Device.setReadtimeout(2000);
        ConnectResult res = Device.Connect();
         
        JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.OK_CANCEL_OPTION);
    }
}



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);
         
        authentication.User("");//Enter User here
        authentication.Serial("");//Enter Serial here
         
        Device.setConnecttimeout(1000);
        Device.setReadtimeout(2000);
        ConnectResult res = Device.Connect();
         
        JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.OK_CANCEL_OPTION);
    }
}
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);
         
        authentication.User("");//Enter User here
        authentication.Serial("");//Enter Serial here
         
        Device.setConnecttimeout(1000);
        Device.setReadtimeout(2000);
        ConnectResult res = Device.Connect();
         
        JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.OK_CANCEL_OPTION);
    }
}
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);
         
        authentication.User("");//Enter User here
        authentication.Serial("");//Enter Serial here
         
        Device.setConnecttimeout(1000);
        Device.setReadtimeout(2000);
        ConnectResult res = Device.Connect();
         
        JOptionPane.showMessageDialog(null, res.Message(), "", JOptionPane.OK_CANCEL_OPTION);
    }
}
2. Disconnect from PLC

untuk memutuskan koneksi ke PLC cukup dengan 1 baris perintah : Device.DisConnect();


3. Automatic connect to PLC

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);
         
        // 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));
    }
}


4. Simple reading data from PLC

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());
    }
 }
}



Reference : https://www.plccom.net/en/plccom/for-s7/fors7-code-examples/

Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...