Abstract .................................................................................................................................................. 3 Introduction............................................................................................................................................ 4 What is SCADA? .................................................................................................................................. 4 How does SCADA work?...................................................................................................................... 4 SCADA is Electric Distribution ............................................................................................................. 4 Block Diagram ..................................................................................................................................... 5 Block No.1 ....................................................................................................................................... 6 Block No.2 ....................................................................................................................................... 6 Main Block ...................................................................................................................................... 7 Flow Diagram of Block No.1 and No.2 ................................................................................................ 8 Circuit...................................................................................................................................................... 9 Voltage Measurement ................................................................................................................... 10 Microcontroller .................................................................................................................................... 11 TM4C123G ........................................................................................................................................ 11 Arduino.............................................................................................................................................. 12 SCADA Software ................................................................................................................................... 13 SCADA for Block No.1........................................................................................................................ 14 SCADA for Block No.2........................................................................................................................ 14 SCADA for Main Control Block .......................................................................................................... 14 Overview of SCADA software............................................................................................................ 15 Tab “Main” of SCADA........................................................................................................................ 15 Connect Panel ............................................................................................................................... 15 Area Panel..................................................................................................................................... 15 Diagram Panel............................................................................................................................... 16 Tab “Load Shedding” of SCADA......................................................................................................... 16 Area load shedding panel ............................................................................................................ 17 Tab “Graph” of SCADA ...................................................................................................................... 17 Database of SCADA ........................................................................................................................... 18 Wireless Communication of SCADA.................................................................................................. 19 Code ...................................................................................................................................................... 20 Code for TM4C123G.......................................................................................................................... 20 Code for Arduino............................................................................................................................... 242 Code for “Block No.1” SCADA ........................................................................................................... 25 SCADA.java.................................................................................................................................... 25 panel_Area1.java .......................................................................................................................... 25 panel_Areas.java........................................................................................................................... 27 panel_for_tab_1.java ................................................................................................................... 28 panel_ for_tab_2.java .................................................................................................................. 29 panel_ for_tab_3.java .................................................................................................................. 29 panel_Serial_Connect.java ........................................................................................................... 30 panel_Current_Graph.java .......................................................................................................... 31 panel_Voltage_Graph.java ........................................................................................................... 32 panel_tab2_Area1.java................................................................................................................. 32 Thread_Socket.java....................................................................................................................... 35 Thread_Database.java .................................................................................................................. 37 Thread_Diagram_Refreshing ........................................................................................................ 39 Thread_Receiving_Comm............................................................................................................. 39 Thread_Sending_Comm ............................................................................................................... 41 panel_Diagram.............................................................................................................................. 41 Reference............................................................................................................................................. 433 Abstract A model of automation of electric distribution using SCADA (supervisory control and data acquisition). There are 3 consumption areas Area1, Area2 and Area3. Area1 is controlled by a SCADA software running on a laptop, similarly Area2 and Area3 are controlled by another SCADA software running on another laptop. A third laptop is also placed for controlling all the three areas wirelessly through internet, thus acting as a master remote control. SCADA software is coded in java. Communication between the consumption Areas and SCADA software on laptop is carried through microcontrollers Arduino and Tiva TM4C123G. ADC (Analog to digital) modules of both microcontrollers are used for taking input from measuring devices installed in the consumption areas and this data is send to laptops by USB data cable through UART module on the microcontrollers.4 Introduction What is SCADA? SCADA (Supervisory control and data acquisition) is an industrial automation control and data gathering system at the core of many modern industries. SCADA is used for automation in almost every industry for example food, beverage, manufacturing, oil, power, recycling, transportation, agriculture etc. How Do SCADA Systems Work? SCADA systems deploy multiple software and hardware elements that allow industrial organizations to: • Monitor, gather, and process data. • Interact with and control machines and devices such as valves, pumps, motors, relays and more, which are connected through HMI (humanmachine interface) software. • Record events into a log file. 1 SCADA in Electric Distribution SCADA system for a power distribution application is a typically a PCbased software package. Data is collected from the electrical distribution system. SCADA in Electric Distribution is used for following tasks • Gathering data regarding voltage, current etc. from various regions. • Operating circuit breakers and relays in case of faults, maintenance or load shedding. • Storing data from future research on making the operation of the system better. 15 BLOCK Diagram Figure 1:Project Block Diagram6 The block diagram consists of 3 main blocks: 1) Block No.1 2) Block No.2 3) Main Control With further observation we see that “Block No.1” has “Area1”, which is a circuit model representing a consumption area. “Block No.2” has “Area2” and “Area3”, which are also circuit models representing consumption areas. 1) Block No.1 We see that “Block No.1” has “Area1”, which is a circuit model representing a consumption area. The circuit diagram of “Area1” consist of a transformer, relay, variable load, voltage and current measuring equipment. The voltage and current values from the measuring equipment of “Area1” is provided to the microcontroller “TM4C123G”. Output pin from “TM4C123G” is given to the relay in “Area1” for turning it ONOFF. “TM4C123G” is connected to a Laptop through a USB cable, from where its gets the command for the operation of relay, and to which it sends values of current and voltage of “Area1”. The laptop has a Java base SCADA software which has a GUI (Graphics User Interface) for easy use. The software displays value of current and voltages, provides a button for turning “Area1” off, shows graphical representation of current and voltages over time, provides option to turn off “Area1” for specified amount of time and also store important information regarding “Area1” in a database based on MySQL with the help of
Table of Contents Abstract Introduction What is SCADA? How does SCADA work? SCADA is Electric Distribution Block Diagram Block No.1 Block No.2 Main Block Flow Diagram of Block No.1 and No.2 Circuit Voltage Measurement 10 Microcontroller 11 TM4C123G 11 Arduino 12 SCADA Software 13 SCADA for Block No.1 14 SCADA for Block No.2 14 SCADA for Main Control Block 14 Overview of SCADA software 15 Tab “Main” of SCADA 15 Connect Panel 15 Area Panel 15 Diagram Panel 16 Tab “Load Shedding” of SCADA 16 Area load shedding panel 17 Tab “Graph” of SCADA 17 Database of SCADA 18 Wireless Communication of SCADA 19 Code 20 Code for TM4C123G 20 Code for Arduino 24 Code for “Block No.1” SCADA 25 SCADA.java 25 panel_Area1.java 25 panel_Areas.java 27 panel_for_tab_1.java 28 panel_ for_tab_2.java 29 panel_ for_tab_3.java 29 panel_Serial_Connect.java 30 panel_Current_Graph.java 31 panel_Voltage_Graph.java 32 panel_tab2_Area1.java 32 Thread_Socket.java 35 Thread_Database.java 37 Thread_Diagram_Refreshing 39 Thread_Receiving_Comm 39 Thread_Sending_Comm 41 panel_Diagram 41 Reference 43 Abstract A model of automation of electric distribution using SCADA (supervisory control and data acquisition) There are consumption areas Area1, Area2 and Area3 Area1 is controlled by a SCADA software running on a laptop, similarly Area2 and Area3 are controlled by another SCADA software running on another laptop A third laptop is also placed for controlling all the three areas wirelessly through internet, thus acting as a master remote control SCADA software is coded in java Communication between the consumption Areas and SCADA software on laptop is carried through microcontrollers Arduino and Tiva TM4C123G ADC (Analog to digital) modules of both microcontrollers are used for taking input from measuring devices installed in the consumption areas and this data is send to laptops by USB data cable through UART module on the microcontrollers Introduction What is SCADA? SCADA (Supervisory control and data acquisition) is an industrial automation control and data gathering system at the core of many modern industries SCADA is used for automation in almost every industry for example food, beverage, manufacturing, oil, power, recycling, transportation, agriculture etc How Do SCADA Systems Work? SCADA systems deploy multiple software and hardware elements that allow industrial organizations to: • Monitor, gather, and process data • Interact with and control machines and devices such as valves, pumps, motors, relays and more, which are connected through HMI (human-machine interface) software • Record events into a log file [1] SCADA in Electric Distribution SCADA system for a power distribution application is a typically a PC-based software package Data is collected from the electrical distribution system SCADA in Electric Distribution is used for following tasks • Gathering data regarding voltage, current etc from various regions • Operating circuit breakers and relays in case of faults, maintenance or load shedding • Storing data from future research on making the operation of the system better [1] BLOCK Diagram Figure 1:Project Block Diagram The block diagram consists of main blocks: 1) Block No.1 2) Block No.2 3) Main Control With further observation we see that “Block No.1” has “Area1”, which is a circuit model representing a consumption area “Block No.2” has “Area2” and “Area3”, which are also circuit models representing consumption areas 1) Block No.1 We see that “Block No.1” has “Area1”, which is a circuit model representing a consumption area The circuit diagram of “Area1” consist of a transformer, relay, variable load, voltage and current measuring equipment The voltage and current values from the measuring equipment of “Area1” is provided to the microcontroller “TM4C123G” Output pin from “TM4C123G” is given to the relay in “Area1” for turning it ON/OFF “TM4C123G” is connected to a Laptop through a USB cable, from where its gets the command for the operation of relay, and to which it sends values of current and voltage of “Area1” The laptop has a Java base SCADA software which has a GUI (Graphics User Interface) for easy use The software displays value of current and voltages, provides a button for turning “Area1” off, shows graphical representation of current and voltages over time, provides option to turn off “Area1” for specified amount of time and also store important information regarding “Area1” in a database based on MySQL with the help of WampServer 2) Block No.2 We see that “Block No.2” has “Area2” and “Area3”, which is are circuit models representing consumption areas The circuit diagram of “Area2” and “Area3” are identical to “Area1”, thus consisting of a transformer, relay, variable load, voltage and current measuring equipment each The voltage and current values from the measuring equipment of “Area2” and “Area3” are provided to the microcontroller “Arduino” Output pin from “Arduino” is given to the relay in “Area2” and “Area3” for turning them ON/OFF “Arduino” is connected to a Laptop through a USB cable, from where its gets the command for the operation of relays, and to which it sends values of current and voltage of “Area2” and “Area3” The laptop has a Java base SCADA software which has a GUI (Graphics User Interface) for easy use The software displays value of current and voltages, provides a button for turning “Area2” or “Area3” off, shows graphical representation of current and voltages over time, provides option to turn off “Area2” or “Area3” for specified amount of time and also store important information regarding “Area2” and “Area3” in a database based on MySQL with the help of WampServer 3) Main Block We see that there are no consumption areas in “Main Block”, only a laptop linked to the SCADA software of “Block No.1” and “Block No.2” This connection is represented by blue arrows, which show that the connection is wireless Thus the connection is made through the internet using the Sockets class of Java The SCADA software in the laptop of “Main Block” can display value of current and voltages, provides a button for turning “Area1”, “Area2” or “Area3” off, shows graphical representation of current and voltages over time, provides option to turn off “Area1”, “Area2” or “Area3” for specified amount of time and also store important information regarding “Area1”, “Area2” and “Area3” in a database based on MySQL with the help of WampServer Thus this provides an overall control on all the consumption areas in the electric distribution system Flow Diagram of Block No.1 and No.2 Circuit Micro controller • Consist of Transformer, Variable load, Voltage & Current measuring equipment and relays • Sends data to micrcontoller • TM4C123 and Arduino are used for two seperate SCADA softwares • TM4C123 is coded in IAR Embedded Workbench using C language • GPIO, ADC and UART modules of TM4C123 are used SCADA Software • Coded in Eclipse using Java • Displays Voltage & Current Values of each consumption areas • Option to Turn off desired Areas of a specific time • Displays Graphs of Voltages and Currents in each consumption areas Database • Coded in MySQL • wampsever is used for storing data for each consumption area in the local disk • Data is added every 0.5sec • Data can be import to pdf, excel and other various formats Circuit There are two circuits First one represents consumption area “Area1” The second circuit represents two consumption areas, “Area2” and “Area3” The circuit of each consumption area is quite similar Each of one consist of Transformer which takes voltage from wapda and steps down After this a relay is connected in series for turning the consumption area ON/OFF In case of relay being OFF a LED is turned ON indicating the consumption area is off The LED is connected to NO (normally open) pin of the relay The circuit for turning on the LED is: Figure 2: Circuit for Energizing LED Digital output from Tiva TM4C123G is 3.3V whereas relays require 5V for operating This 5V can be obtained from node of figure For this we use the circuit shown in figure Figure 3: Circuit for Energizing Relay Voltage Measurement At node the dc voltage is 17.3V to step it down to 1.65V we require a voltage bridge with the ratio of 9.5:1 Which in terms of standard resisters give the value of 430KΩ and 43KΩ Values are selected high because we only need voltage sample and want to keep in current in divider minimum IC 7805 is placed in series to avoid voltage getting higher than 5V which would damage the microcontroller permanently Figure 4: Voltage divider for TM4C123G In case of voltage input for Arduino the value is set 2.5V instead of 1.65V because the maximum voltage for input in case of Arduino is 5V instead of 3.3V Which gives a ratio of 5.9:1 In case of standard resistors that can be achieved with 430KΩ and 68KΩ resistors Figure 5: Voltage divider for Arduino 10 5) panel_for_tab_2.java import java.awt.Color; import java.awt.GridLayout; import javax.swing.*; public class panel_for_tab_2 extends JPanel { private static final long serialVersionUID = 1L; private panel_tab2_Area1 pT2A1; public panel_for_tab_2(){ setSize(600,400); setBackground(Color.WHITE); setLayout(new GridLayout(2,1)); pT2A1= new panel_tab2_Area1(); add(pT2A1); } } 6) panel_for_tab_3.java import java.awt.Color; import java.awt.GridLayout; import javax.swing.*; public class panel_for_tab_3 extends JPanel { private static final long serialVersionUID = 1L; private panel_Voltage_Graph pVG; private panel_Current_Graph pCG; public panel_for_tab_3() { setSize(600,400); setBackground(Color.WHITE); setLayout(new GridLayout(2,1)); pVG= new panel_Voltage_Graph(); pCG= new panel_Current_Graph(); add(pVG); add(pCG); } } 29 7) panel_Serial_Connect.java import java.awt.*; import javax.swing.*; import com.fazecast.jSerialComm.*; import java.awt.event.*; public class panel_Serial_Connect extends JPanel { private static final long serialVersionUID = 1L; private JButton connectButton; private JComboBox portList; static public SerialPort chosenPort; public panel_Serial_Connect(){ setSize(1100,100); setBackground(Color.WHITE); setLayout(new GridLayout(1,2)); portList = new JComboBox(); connectButton = new JButton("Connect"); SerialPort[] ports = SerialPort.getCommPorts(); connectButton.addActionListener(new ConnectButtonListener()); for(int i = 0; i < ports.length; i++) portList.addItem(ports[i].getSystemPortName()); add(portList); add(connectButton); } SerialPort getchosenPort(){ return chosenPort; } private class ConnectButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(connectButton.getText().equals("Connect")) { chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString()); chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0); if(chosenPort.openPort()) { connectButton.setText("Disconnect"); portList.setEnabled(false); 30 } Thread_Sending_Comm T1= new Thread_Sending_Comm (); T1.start(); Thread_Receiving_Comm T2= new Thread_Receiving_Comm (); T2.start(); Thread_Diagram_Refreshing T3= new Thread_Diagram_Refreshing (); T3.start(); Thread_Database T4= new Thread_Database (); T4.start(); Thread_Socket T5= new Thread_Socket (); T5.start(); } } } } 8) panel_Current_Graph.java import java.awt.BorderLayout; import java.awt.Color; import javax.swing.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class panel_Current_Graph extends JPanel { private static final long serialVersionUID = 1L; static int y=0; static JPanel panel_Current_Graph; static XYSeries XYSeries_Current_Area1; public panel_Current_Graph() { setSize(600,400); setBackground(Color.WHITE); setLayout(new BorderLayout()); XYSeries_Current_Area1 = new XYSeries("Current Readings"); XYSeriesCollection dataset_Current = new XYSeriesCollection(); 31 dataset_Current.addSeries(XYSeries_Current_Area1); JFreeChart chart = ChartFactory.createXYLineChart("Current Readings", "Readings", "Current", dataset_Current); add(new ChartPanel(chart), BorderLayout.CENTER); } } 9) panel_Voltage_Graph.java import java.awt.BorderLayout; import java.awt.Color; import javax.swing.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class panel_Voltage_Graph extends JPanel { private static final long serialVersionUID = 1L; static int x=0; static JPanel panel_Voltage_Graph; static XYSeries XYSeries_Voltage_Area1; public panel_Voltage_Graph() { setSize(600,400); setBackground(Color.WHITE); setLayout(new BorderLayout()); XYSeries_Voltage_Area1 = new XYSeries("Voltage Readings"); XYSeriesCollection dataset_Voltage = new XYSeriesCollection(); dataset_Voltage.addSeries(XYSeries_Voltage_Area1); JFreeChart chart = ChartFactory.createXYLineChart("Voltage Readings", "Readings", "Voltage", dataset_Voltage); add(new ChartPanel(chart), BorderLayout.CENTER); } } 10) panel_tab2_Area1.java import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import javax.swing.*; import javax.swing.border.TitledBorder; 32 public class panel_tab2_Area1 extends JPanel{ private static final long serialVersionUID = 1L; TitledBorder tb_panel_Area1=BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black,3)," Area 1", TitledBorder.CENTER,TitledBorder.DEFAULT_POSITION,new Font("Calibri", Font.BOLD, 20),Color.black); TitledBorder tb_Timer_Countdown=BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blu e,2), "Timer Remaing for Area1 to Remain OFF", TitledBorder.LEFT,TitledBorder.DEFAULT_POSITION,new Font("Calibri", Font.BOLD, 12),Color.black); TitledBorder tb_Timer_Area1_TextField=BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color blue,2), "Type amount of time to turn Area OFF (hh:mm:ss)", TitledBorder.LEFT,TitledBorder.DEFAULT_POSITION,new Font("Calibri", Font.BOLD, 12),Color.black); static Timer Timer_Area1; static JLabel Label_Countdown_Area1; static JTextField TextField_Countdown_Area1; static JButton Button_Timer_Area1; static int int_Total_Initial_Time_Area1; static String String_Total_Initial_Hours_Area1; static String String_Total_Initial_Minutes_Area1; static String String_Total_Initial_Seconds_Area1; static String String_Updated_Changing_Hours_Area1; static String String_Updated_Changing_Minutes_Area1; static String String_Updated_Changing_Seconds_Area1; static String String_Timer_Textfield_Area1; public panel_tab2_Area1(){ setSize(600,300); setBackground(Color.WHITE); setLayout(new GridLayout(1,3)); setBorder(tb_panel_Area1); Label_Countdown_Area1 =new JLabel("0:0:0"); Label_Countdown_Area1.setFont(new Font("Calibri", Font.PLAIN, 42)); Label_Countdown_Area1.setBorder(tb_Timer_Countdown); TextField_Countdown_Area1 = new JTextField (5); TextField_Countdown_Area1.setText("0:0:0"); TextField_Countdown_Area1.addActionListener (new Listener_Timer_Area1()); TextField_Countdown_Area1.setFont(new Font("Calibri", Font.PLAIN, 42)); TextField_Countdown_Area1.setBorder(tb_Timer_Area1_TextField); 33 Button_Timer_Area1=new JButton("Stop"); Button_Timer_Area1.addActionListener (new TimerStopListener()); Button_Timer_Area1.setFont(new Font("Calibri", Font.PLAIN, 24)); Timer_Area1 = new Timer(1000, new Timer_Area1_Listener()); add(Label_Countdown_Area1); add(TextField_Countdown_Area1); add(Button_Timer_Area1); } private static class Listener_Timer_Area1 implements ActionListener { public void actionPerformed (ActionEvent event) { String_Timer_Textfield_Area1=TextField_Countdown_Area1.getText(); Scanner Scan_TextField_Area1 = new Scanner (String_Timer_Textfield_Area1); Scan_TextField_Area1.useDelimiter(":"); String_Total_Initial_Hours_Area1=Scan_TextField_Area1.next(); String_Total_Initial_Minutes_Area1=Scan_TextField_Area1.next(); String_Total_Initial_Seconds_Area1=Scan_TextField_Area1.next(); Scan_TextField_Area1.close(); int_Total_Initial_Time_Area1 = (Integer.parseInt (String_Total_Initial_Hours_Area1)*3600) +(Integer.parseInt (String_Total_Initial_Minutes_Area1)*60)+(Integer.parseInt (String_Total_Initial_Seconds_Area1))-1; Timer_Area1.start(); panel_Area1.String_Status_Relay_Area1="0"; } } private static class Timer_Area1_Listener implements ActionListener { public void actionPerformed (ActionEvent event) { if(int_Total_Initial_Time_Area1>0){ String_Updated_Changing_Hours_Area1=Integer.toString(int_Total_Initial_Time_Area1/360 0); String_Updated_Changing_Minutes_Area1=Integer.toString((int_Total_Initial_Time_Area1% 3600)/60); String_Updated_Changing_Seconds_Area1=Integer.toString((int_Total_Initial_Time_Area1% 3600)%60); Label_Countdown_Area1.setText(String_Updated_Changing_Hours_Area1+":"+String_Updated_Cha nging_Minutes_Area1 34 +":"+String_Updated_Changing_Seconds_Area1); int_Total_Initial_Time_Area1=int_Total_Initial_Time_Area1-1; } else{ Label_Countdown_Area1.setText("0:0:0"); Timer_Area1.stop(); panel_Area1.String_Status_Relay_Area1="1"; } } } private static class TimerStopListener implements ActionListener { public void actionPerformed (ActionEvent event) { panel_Area1.String_Status_Relay_Area1="1"; Timer_Area1.stop(); Label_Countdown_Area1.setText("0:0:0"); } } } 11) Thread_Socket.java import java.io.*; import java.net.*; import java.util.Scanner; public class Thread_Socket extends Thread { Socket socket; PrintWriter out; BufferedReader in; static public String String_Previous_Status_Relay_Area1="1"; static public double double_Previous_Voltage_Value; public Thread_Socket(){ } public void run(){ //try {Thread.sleep(100); } catch(Exception e) {} try{ Socket socket = new Socket("192.168.1.2", 1634); //BufferedReader input_Area1 = new BufferedReader(new InputStreamReader(socket.getInputStream())); 35 Scanner input_Area1 = new Scanner(socket.getInputStream()); PrintWriter out_Area1=new PrintWriter(socket.getOutputStream(),true); String answer = null; while (true){ try{Thread.sleep(200);}catch(Exception e){} answer = input_Area1.nextLine(); if (Integer.parseInt(answer) != Integer.parseInt(String_Previous_Status_Relay_Area1)) { String_Previous_Status_Relay_Area1=answer; if (Integer.parseInt(answer)==1) { panel_Area1.String_Status_Relay_Area1="1"; panel_Area1.Button_Relay_Area1.setText("Turn OFF"); } else if (Integer.parseInt(answer)==0) { panel_Area1.String_Status_Relay_Area1="0"; panel_Area1.Button_Relay_Area1.setText("Turn ON"); } } out_Area1.println(""+Double.parseDouble(Thread_Receiving_Comm.String_Voltage_Value_ Area1)); out_Area1.flush(); out_Area1.println(""+Double.parseDouble(Thread_Receiving_Comm.String_Current_Value_ Area1)); out_Area1.flush(); out_Area1.println(""+Integer.parseInt(panel_Area1.String_Status_Relay_Area1)); out_Area1.flush(); } } catch (UnknownHostException ex) { System.out.println("UNKNOW HOST EXCEPTION"); } catch (IOException ex) { System.out.println(" IOEXCEPTION"); System.out.println(ex.getMessage()); } 36 } } 12) Thread_Database.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Calendar; import com.mysql.jdbc.PreparedStatement; public class Thread_Database extends Thread { static Connection con; static Statement st; static ResultSet rs; public Thread_Database() { try{ Class.forName("com.mysql.jdbc.Driver"); Thread_Database.con=DriverManager.getConnection("jdbc:mysql://localhost:3306/scada"," root",""); Thread_Database.st=Thread_Database.con.createStatement(); }catch(Exception ex){ System.out.println("Error:"+ex); } Thread_Database.InitializeDatabase(); } public void run(){ while(true) { try {Thread.sleep(1000); } catch(Exception e) {} insertData(); } } static void insertData(){ try{ String query2="INSERT INTO area1(TimeAtMoment,Voltage,Current,StatusOfRelay) VALUES (?,?,?,?)"; PreparedStatement statement2 = (PreparedStatement) con.prepareStatement(query2); 37 String timeStamp = new SimpleDateFormat("dd/MM/yyyy_HH:mm:ss").format(Calendar.getInstance().getTime()); statement2.setString(1,timeStamp); statement2.setString(2,Double.toString(Double.parseDouble(Thread_Receiving_Comm.String_Volta ge_Value_Area1))); statement2.setString(3,Double.toString(Double.parseDouble(Thread_Receiving_Comm.String_Curre nt_Value_Area1))); if (Integer.parseInt(panel_Area1.String_Status_Relay_Area1)==1) { statement2.setString(4,"ON"); } else if (Integer.parseInt(panel_Area1.String_Status_Relay_Area1)==0) { statement2.setString(4,"OFF"); } statement2.executeUpdate(); }catch(Exception ex){ System.out.println(ex); } } static void InitializeDatabase(){ try{ String query1="DROP TABLE IF EXISTS Area1 CASCADE"; PreparedStatement statement1 = (PreparedStatement) con.prepareStatement(query1); statement1.executeUpdate(); String query="CREATE TABLE IF NOT EXISTS Area1(" + "EntryNO int(11) NOT NULL AUTO_INCREMENT," + "TimeAtMoment varchar(20) DEFAULT NULL," + "Voltage varchar(20) DEFAULT NULL," + "Current varchar(20) DEFAULT NULL," + "StatusOfRelay varchar(5) DEFAULT NULL," + "PRIMARY KEY(EntryNo));"; PreparedStatement statement = (PreparedStatement) con.prepareStatement(query); statement.executeUpdate(); }catch(Exception ex){ System.out.println(ex); } } } 38 13) Thread_Diagram_Refreshing.java public class Thread_Diagram_Refreshing extends Thread{ Thread_Diagram_Refreshing (){ } public void run(){ while(true) { try {Thread.sleep(500); } catch(Exception e) {} panel_Diagram.SelectDiagram(); // System.out.println(panel_Area1.String_Status_Relay_Area1); } } } 14) Thread_Receiving_Comm.java import java.awt.Color; import java.util.Scanner; public class Thread_Receiving_Comm extends Thread { static public double Double_Voltage_Value_Area1; static public double Double_Current_Value_Area1; static public String String_Voltage_Value_Area1 ; static public String String_Current_Value_Area1 ; Thread_Receiving_Comm (){ } public void run(){ Scanner Scanner_Input_Data = new Scanner(panel_Serial_Connect.chosenPort.getInputStream()); Scanner_Input_Data.useDelimiter(","); while(Scanner_Input_Data.hasNextLine()){ try{ String_Voltage_Value_Area1=Scanner_Input_Data.next(); String_Current_Value_Area1=Scanner_Input_Data.next(); panel_Area1.Label_Voltage_Value_Area1.setText(String_Voltage_Value_Area1); panel_Area1.Label_Current_Value_Area1.setText(String_Current_Value_Area1); 39 System.out.print(String_Voltage_Value_Area1+','+String_Current_Value_Area1+','); Double_Voltage_Value_Area1=Double.parseDouble(String_Voltage_Value_Area1); Double_Current_Value_Area1=Double.parseDouble(String_Current_Value_Area1); panel_Voltage_Graph.XYSeries_Voltage_Area1.add(panel_Voltage_Graph.x++, Double_Voltage_Value_Area1); panel_Current_Graph.XYSeries_Current_Area1.add(panel_Current_Graph.y++, Double_Current_Value_Area1); panel_Voltage_Graph.panel_Voltage_Graph.repaint(); panel_Current_Graph.panel_Current_Graph.repaint(); //CheckStatus(String_Voltage_Value_Area1,String_Current_Value_Area1); CheckStatus(String_Voltage_Value_Area1); } catch(Exception e) {} } Scanner_Input_Data.close(); } static public void CheckStatus(String Area1){ if(Area1 != null) { if (Double.parseDouble(Area1)0.1 ) { icon = new ImageIcon("Diagram_Panel_Images/1.jpg"); } else if (Thread_Receiving_Comm.Double_Voltage_Value_Area1