Example Applet That Uses the API comm Library.
|
With the Java API library, writing your own
PLC control GUI applet that can be run off any web-browser has never been easier!
An example applet as shown on the left can be created very easily using our API.
What this applet does is to continously display the real time clock data it obtains from
the PLC and it also shows the logic status of Input #1 and Output #1 in the PLC. The label
"Input #1" displays the ON/OFF status of the PLC's Input #1 using different
brightness of GREEN color. The "Output1" element is a push button which allows
you to "force-toggle" the PLC output #1 whenever you click on it. The status of
the output #1 is also shown as different brightness of RED color on the button. The complete source code of this example applet is shown below. Note that only those lines that are displayed in bold blue characters actually involves the API library. The rest and just plain vanilla Java AWT code that most Java programmers are already familiar with. Click here to learn more about the PLC Java communication library "PLCmon.jar". You can also construct your own Java application program to run on other platforms. The picture on the left shows the same Java program described here running on a HP Jornada Pocket PC installed with the Personal Java JVM (free download from Sun). You can remotely read the logic status of the PLC's input #1 and output #1 and the real time clock, and you can also toggle the PLC's output #1 by touching the "Output1" button. Hence the API comm library enable you to easily write and test your Java program on your PC and when ready, just deploy it to other platform or copy it to the PDA for execution. Please click on the PDA picture to see an enlarged photo of this sample Java program on PDA. |
import PLCmon.*; import java.awt.*; import java.applet.*; public class TestApplet extends Applet { String URLString = "127.0.0.1:9080"; // a localhost connection String username = "samples"; String password = ""; int [] DM = new int[4000]; // place holder for the Data Memory DM[1] to DM[4000] ConnectTLServer ctlsvr; Color darkGreen = new java.awt.Color(0,128,0); Color darkRed = new java.awt.Color(128,0,0); public void init() { //{{INIT_CONTROLS setLayout(null); setSize(120,120); panel1.setLayout(null); add(panel1); panel1.setBackground(new java.awt.Color(255,255,128)); panel1.setBounds(0,0,120,120); RTC.setEditable(false); RTC.setText("Hello"); panel1.add(RTC); RTC.setBackground(java.awt.Color.black); RTC.setForeground(java.awt.Color.red); RTC.setFont(new Font("Dialog", Font.BOLD, 16)); RTC.setBounds(20,20,87,24); clockLabel.setText("PLC\'s Clock"); clockLabel.setAlignment(java.awt.Label.CENTER); panel1.add(clockLabel); clockLabel.setBounds(20,0,84,24); output1Button.setLabel("Output1"); panel1.add(output1Button); output1Button.setBackground(new java.awt.Color(128,0,0)); output1Button.setForeground(java.awt.Color.yellow); output1Button.setFont(new Font("Dialog", Font.BOLD, 12)); output1Button.setBounds(20,80,84,22); input1Label.setText("Input #1"); input1Label.setAlignment(java.awt.Label.CENTER); panel1.add(input1Label); input1Label.setBackground(new java.awt.Color(0,128,0)); input1Label.setForeground(java.awt.Color.yellow); input1Label.setFont(new Font("Dialog", Font.BOLD, 12)); input1Label.setBounds(20,48,84,22); //}} ctlsvr = new ConnectTLServer(URLString, username, password); MonitorThread mt = new MonitorThread(); mt.start(); //{{REGISTER_LISTENERS SymMouse aSymMouse = new SymMouse(); output1Button.addMouseListener(aSymMouse); //}} } //{{DECLARE_CONTROLS java.awt.Panel panel1 = new java.awt.Panel(); java.awt.TextField RTC = new java.awt.TextField(); java.awt.Label clockLabel = new java.awt.Label(); java.awt.Button output1Button = new java.awt.Button(); java.awt.Label input1Label = new java.awt.Label(); //}} class MonitorThread extends Thread { private Action monAction[] = new Action[10]; // Don't make these monAction[] variable public public void run() { // to avoid writing action[] variables in other threds while (true) { // run forever (until program exits) readRTC(); // add the list of actions you wish to monitor here readIO(); try { Thread.sleep(200); // update every 0.2 second } catch (Exception e){} } } void readIO() { // Read the logic state of the inputs and outputs monAction[0] = new Action(1,ActionConstant.READSINGLE,ActionConstant.INPUT,1,0); // Read INPUT[1] monAction[1] = new Action(1,ActionConstant.READSINGLE,ActionConstant.OUTPUT,1,0); // Read OUTPUT[1] try { ctlsvr.commAction(monAction, 2); // Execute the two READ actions defined above if ((monAction[0].value & 0x0001)!=0) { // Test bit 0 of 16-bit variable INPUT[1] input1Label.setBackground(Color.green); // it is ON, light up the label in GREEN } else { input1Label.setBackground(darkGreen); // it is OFF, dim the label as dark green. } if ((monAction[1].value & 0x0001)!=0) { // Test bit 0 of 16-bit variable OUTPUT[1] output1Button.setBackground(Color.red); // it is ON, light up the label in GREEN } else { output1Button.setBackground(darkRed); // it is OFF, dim the label as dark green. } } catch (Exception e) { RTC.setText(e.toString()); // show the error message System.out.println(e); } } public void readRTC() { // Read the read time clock: Hours, Minutes and Seconds. // the returned value for READSINGLE is contained in the integer variable "value". monAction[0] = new Action(1,ActionConstant.READSINGLE,ActionConstant.TIME ,1,0); // Read Hour monAction[1] = new Action(1,ActionConstant.READSINGLE,ActionConstant.TIME, 2,0); // Read Minute monAction[2] = new Action(1,ActionConstant.READSINGLE,ActionConstant.TIME, 3,0); // Read Second try { ctlsvr.commAction(monAction, 3); // Execute 3 actions defined above String RTCvalue = monAction[0].value +":"+ monAction[1].value+":"+ monAction[2].value; RTC.setText(RTCvalue); } catch (Exception e) { RTC.setText(e.toString()); // show the error message System.out.println(e); } } } class SymMouse extends java.awt.event.MouseAdapter { public void mouseReleased(java.awt.event.MouseEvent event) { Object object = event.getSource(); if (object == output1Button) output1Button_MouseReleased(event); } public void mousePressed(java.awt.event.MouseEvent event) { Object object = event.getSource(); if (object == output1Button) output1Button_MousePressed(event); } } void output1Button_MousePressed(java.awt.event.MouseEvent event) { Action writeAction[] = new Action[10]; // to do: code goes here. writeAction[0] = new Action(1,ActionConstant.READSINGLE,ActionConstant.OUTPUT ,1,0); // Read OUTPUT[1] try { ctlsvr.commAction(writeAction, 1); // Execute 1 action defined above writeAction[0].actionType = ActionConstant.WRITESINGLE; // change to WRITESINGLE OUTPUT writeAction[0].value ^= 0x0001; // Toggle OUTPUT[1] bit 0 ctlsvr.commAction(writeAction, 1); // Execute the action; } catch (Exception e) { RTC.setText(e.toString()); // show the error message System.out.println(e); } } void output1Button_MouseReleased(java.awt.event.MouseEvent event) { // to do: code goes here. } } |