package luytenautomation;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Persmachines extends Applet {
private int AANTAL_MACHINES = 10; //aantal machines
private Label[] labelRij; //eerste kolom: nummer machine
private Button[] statusRij; //tweede kolom: knoppen met status machine
private TextField[] productieRij;//derde kolom: tekstvakken met #geproduceerde stuks per machine
public void init() {
setFont(new Font("Arial",Font.PLAIN,18));
labelRij = new Label[AANTAL_MACHINES];
statusRij = new Button[AANTAL_MACHINES];
productieRij = new TextField[AANTAL_MACHINES];
for (int i=0;i<AANTAL_MACHINES;i++){
labelRij[i]=new Label(""+(i+1),Label.CENTER);
statusRij[i]=new Button("Werkt niet");
statusRij[i].setBackground(Color.RED);
productieRij[i] = new TextField("0");
productieRij[i].setEditable(false);
}
setLayout(new GridLayout(AANTAL_MACHINES+1,3,25,10));
add(new Label("NUMMER MACHINE",Label.CENTER));
add(new Label("STATUS",Label.CENTER));
add(new Label("# GEPRODUCEERDE STUKS",Label.CENTER));
for (int i=0;i<AANTAL_MACHINES;i++){
add(labelRij[i]);
add(statusRij[i]);
add(productieRij[i]);
}
}
}
Bij de knoppen moet komen:
// This statement assigns all classes whose source text contains
// this statement to a package.
package de.siemens.simaticnet.itcp.example;
// By importing a package or a class, all declarations
// are made visible that may be made visible in other packages
// based on their access class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import de.siemens.simaticnet.itcp.api.*;
/**
* Example6.java
* <p>Title: Example 6 - Using the ITCP Beans.</p>
* <p>Description: A button with a ”switch” function.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Organization: Siemens AG SIMATIC NET</p>
*
* A button with the ”switch” function
* Memory bit M10.0 is read and written.
*
* Components used:
* S7CP
* S7Device
* S7Variable
* AWT Button
*
* @author ITCP Team
* @version 1.0
*
*/
public class Example6 extends Applet implements PropertyChangeListener, MouseListener {
/*-----------------------------------Implemented Interfaces*/
/*----------------------------Basic Class Applet*/
// Declaration of the required components
private S7CP s7CP1 = null;
Examples:
117
S7Beans / AppletsProgramming Tips
Release 07/2005
C79000-G8976-C180-02
private S7Device s7Device1 = null;
private S7Variable s7Variable1 = null;
private Button button1 = null;
/**
* Is always called when the applet is initialized.
* This occurs immediately after it is loaded.
*
* @see #start
* @see #stop
* @see #destroy
*/
public void init() {
super.init();
// Sets the name of the component to the specified character string.
setName(”Example6”);
// Sets the layout manager for this component.
setLayout(null);
// Sets the size of the applet width / height
setSize(426, 240);
/*-----------Height*/
/*------Width*/
// Create an instance for the S7CP bean.
// S7CP is the Ethernet access point to the station
s7CP1 = new S7CP();
// Assign the IP address
// ######## Project-specific adaptation of the IP address necessary ########
s7CP1.setHostString(new HostString (”192.168.1.1:80”));
/*------------------------------------------------Specify port number
normally :80*/
/*-----------------------------------IP address as string*/
// Create an instance for the S7Device bean.
// S7Device is used to address the communication partner in the station.
s7Device1 = new S7Device();
// The address is made up of the rack number and slot number of the
// module. The default of both methods for addressing is ’0’.
// This means that the rack number is unnecessary (.setRack(0)).
// As we want to communicate with the CPU, the slot of the CPU must
// be entered.
// ######## Project-specific adaptation of the rack and slot number ########
// ######## necessary ########
s7Device1.setSlot(2);
/*----------------Slot number 2 (int)*/
// Create an instance for the S7Variable bean.
// The S7Variable bean represents the variable to be read
// or written.
s7Variable1 = new S7Variable();
// The variable is described by an S7 ANY pointer
s7Variable1.setS7Anypointer(
new S7Anypointer((int)1, (int)1, (int)131, (int)0, (int)10, (int)0));
/*--------------------------------------------------------------Bit number 0 ..7*/
/*-----------------------------------------------------Memory area offset*/
/*---------------------------------------------DB number or ’0’*/
/*-----------------------------------Memory area 131 == M*/
/*---------------------------Repetition factor 1 .. n*/
/*-------------------Data type 1 == BOOL*/
// Sets the name of the component to the specified character string.
s7Variable1.setVariableName(”s7Variable1”);
// Create an instance for a button.
button1 = new Button();
// Sets the output text to the specified character string.
button1.setLabel(”Switch”);
// Sets the name of the component to the specified character string.
button1.setName(”Switch”);
// Specify the start position and size of the component.
Examples:
118
S7Beans / AppletsProgramming Tips
Release 07/2005
C79000-G8976-C180-02
button1.setBounds(10, 10, 100, 25);
/*------------------------------Component height*/
/*-------------------------Component width*/
/*--------------------Start position Y*/
/*----------------Start position X*/
// Insert the component in the applet.
add(button1, button1.getName());
// Apart from the definition of the methods to be executed when an event
// occurs, an object must register with the corresponding
// event source.
// This is done by calling the addXXXListener method of the event source,
// where »XXX« stands for the corresponding event type.
// The addXXXListener methods all expect a reference to the relevant
// interface:
s7CP1.addPropertyChangeListener(this);
s7Device1.addPropertyChangeListener(this);
s7Variable1.addPropertyChangeListener(this);
button1.addMouseListener(this);
}
/**
* Executes after initialization of an applet.
* With browsers, start() is then also called when a page containing
* an applet is reloaded.
*
* @see #init
* @see #stop
* @see #destroy
*/
public void start() {
super.start();
// Read the defined S7Variable to display the current status.
// Reading is triggered by the processGet() method
// of S7Variable bean.
// If the new values exist, the S7Variable bean triggers a
// PropertyChangeEvent.
s7Variable1.processGet();
}
/**
* There is a call when the browser or the applet viewer is minimized
* to an icon or an HTML page containing an applet is exited in
* a browser.
*
* @see #init
* @see #start
* @see #destroy
*/
public void stop() {
super.stop();
}
/**
* Is always called when the applet is destroyed.
*
* @see #init
* @see #start
* @see #stop
*/
public void destroy() {
super.destroy();
// This method deletes all S7Bean instances and discards all threads.
// After calling this method, a reinitialization is necessary.
S7Api.terminate();
}
/**
* Method for handling events for the PropertyChangeListener interface.
Examples:
119
S7Beans / AppletsProgramming Tips
Release 07/2005
C79000-G8976-C180-02
*
* @param evt PropertyChangeEvent
*/
public void propertyChange(PropertyChangeEvent evt) {
// Query whether event was triggered by S7CP.
if (evt.getSource() == s7CP1)
// If YES
// Pass event to the S7Device instance
s7Device1.propertyChange(evt);
// Query whether or not event was triggered by S7Device.
if (evt.getSource() == s7Device1)
// If YES
// Pass event to the S7Variable instance
s7Variable1.propertyChange(evt);
// Query whether or not event was triggered by S7Variable.
if (evt.getSource() == s7Variable1) {
// If YES, the new data from the station is included in the event.
// The new data is made available as Boolean by the getNewValue()
// method.
// Query the signal state true or false (RLO1 / RLO0) of the
// value read.
if (((Boolean)evt.getNewValue()).booleanValue()) {
/*------------------------------Convert Boolean value to ’boolean’ */
/*---------------Read new value*/
/*---Converts the new value to Boolean*/
// If true (RLO1), then set text and color of the button.
// Sets the output text to the specified character string.
button1.setLabel(”Switch is ON”);
// Sets the background color to red.
button1.setBackground(Color.red);
} else {
// If false (RLO0), then set text and color of the button.
// Sets the output text to the specified character string.
button1.setLabel(”Switch is OFF”);
// Sets the background color to green.
button1.setBackground(Color.green);
}
}
}
/**
* Method for handling events for the MouseListener interface.
*
* mousePressed is called when a mouse button is pressed.
*
* @param e java.awt.event.ActionEvent
*/
public void mousePressed(MouseEvent e) {
// Query whether or not button was triggered.
if (e.getSource() == button1) {
// If YES, write new value to the PLC.
// Query the last status of the station value and write back
// inverted.
if (((Boolean)s7Variable1.getValue()).booleanValue()) {
/*-----------------------------------Convert Boolean value to ’boolean’ */
/*-----------------------Read last status*/
/*---Converts the last status to Boolean*/
// If the last status was true (RLO1), then new value false (RLO0).
// The new data is transferred to the setValue() method as string.
// The setValue() method then writes the value to the station.
// Calling setValue() automatically retriggers the processGet() method to
// read the area.
s7Variable1.setValue(String.valueOf(false));
// The waitOnNewData method only allows setValue() to be called again
// only after a wait time has elapsed or after the arrival of new data.
Examples:
120
S7Beans / AppletsProgramming Tips
Release 07/2005
C79000-G8976-C180-02
// This prevents fast button clicks.
s7Variable1.waitOnNewData(2000);
/*------------------------Wait time in msec. 2000 == 2 sec.*/
} else {
// If the last status was false (RLO0), then new value true (RLO1).
s7Variable1.setValue(String.valueOf(true));
s7Variable1.waitOnNewData(2000);
}
}
}
// The MouseListener interface has more than one method.
// If you require only one of these methods to process the event,
// the methods you do not require must be made available as dummy
// implementations.
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}