Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ComInt1;
- import ComInt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.*;
- /**
- *
- * @author Abd Khamim Ismail (Department of Physics, Faculty of Science, UTM)
- * Last updated 17 Sept 2018
- */
- public class ComIntUI1 extends javax.swing.JFrame implements ActionListener{
- static Parameters param;
- static Com com;
- static PrintStream temps,plTemps;
- Timer timer;
- int onADC,mSec;
- double t;
- /** Creates new form CoolingCurveUI */
- public ComIntUI1() {
- initComponents();
- initGnuplot();
- mSec=100; // sampling interval in milisecond
- timer = new Timer(mSec, this);
- //timer.start();
- onADC=0;
- t = 0.0;
- try{
- param = new Parameters(); //create object param
- param.setPort("COM3"); //set com port in Windows
- param.setBaudRate("115200"); //set Baud rate
- com = new Com(param); //assign com port
- System.out.println(com.receiveToString('\n', 0)); //acknowledge com port connection
- }catch (Exception e){
- }
- OffTemp.setEnabled(false);
- PlotTemp.setEnabled(false);
- }
- private void initGnuplot() {
- try {
- //plotting temperature(oC)
- plTemps = new PrintStream(new FileOutputStream("plottemp.plt"));
- plTemps.printf("set xlabel \"Time (sec)\n");
- plTemps.printf("set ylabel \"Temperature (oC)\n");
- plTemps.printf("plot \"temp.dat\" using 1:2 title \"Temperature (oC)\" with line\n");
- plTemps.printf("pause 0.2\n");
- plTemps.printf("reread");
- plTemps.close();
- } catch (IOException e) {
- System.err.println ("Error writing to file");
- }
- }
- //Called when the Timer fires.
- @SuppressWarnings("empty-statement")
- public void actionPerformed(ActionEvent ee) {
- try{
- if (onADC==1) {
- int datPoint = 4;// number of data point
- double ADCSum = 0.0;
- for (int i=1; i<=datPoint; i++)
- {
- com.sendString("rd1\r",1);// read ADC A1
- String ADC = com.receiveToString('\n', 0);//receive ADC value in string
- double ADCR = Double.parseDouble(ADC);//convert ADC value from String to double
- ADCSum = ADCSum + ADCR;//summation of ADC data points
- }
- t = t + mSec/1000.0;//calculate elapsed time
- double ADCAve = ADCSum/datPoint; //averaging
- double volVal = (ADCAve/1023.0)*5.0;//convert ADC value into analogue voltage
- double tempVal = volVal*100.0; //convert volVal to deg Celsius, from LM35 datasheet(10mV/degC or 100degC/V)
- String tempVolValF = String.format("%.4f", volVal); // formatting voltage value to decimal places
- String tempValF = String.format("%.2f", tempVal); //formatting temperature value to decimal places
- TempVolValue.setText(tempVolValF);//display analogue voltage value
- TempValue.setText(tempValF);//display temperature value
- //------------------------------ Optional bar graph------------------------------------
- double barTemp = tempVal*(100.0/100.0);//scaling to 0-100 deg Celcius
- long tempGraph = Math.round(barTemp); //rounding voltage value into integer (0-100)
- TempScale.setValue((int) tempGraph); //Display temperature value to progress bar (0-100)
- //------------------------------------------------------------------------------------
- temps.printf("%.2f %.2f\n",t,tempVal);//record data into file
- System.out.printf("%.2f %.2f\n",t,tempVal);
- Thread.sleep(5);//delay
- }
- }catch (Exception e){System.out.println("Device not found");}
- }
- /** This method is called from within the constructor to
- * initialise the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable1 = new javax.swing.JTable();
- buttonGroup1 = new javax.swing.ButtonGroup();
- buttonGroup2 = new javax.swing.ButtonGroup();
- buttonGroup3 = new javax.swing.ButtonGroup();
- buttonGroup4 = new javax.swing.ButtonGroup();
- buttonGroup5 = new javax.swing.ButtonGroup();
- buttonGroup6 = new javax.swing.ButtonGroup();
- buttonGroup7 = new javax.swing.ButtonGroup();
- buttonGroup8 = new javax.swing.ButtonGroup();
- canvas1 = new java.awt.Canvas();
- jPanel3 = new javax.swing.JPanel();
- jLabel9 = new javax.swing.JLabel();
- OffTemp = new javax.swing.JButton();
- OnTemp = new javax.swing.JButton();
- TempValue = new javax.swing.JTextField();
- jLabel19 = new javax.swing.JLabel();
- TempVolValue = new javax.swing.JTextField();
- jLabel20 = new javax.swing.JLabel();
- PlotTemp = new javax.swing.JButton();
- TempScale = new javax.swing.JSlider();
- ExitButton = new javax.swing.JButton();
- jLabel16 = new javax.swing.JLabel();
- jTable1.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null}
- },
- new String [] {
- "Title 1", "Title 2", "Title 3", "Title 4"
- }
- ));
- jScrollPane1.setViewportView(jTable1);
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setForeground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
- jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
- jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
- jLabel9.setText("<html><sup>o</sup> C");
- OffTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- OffTemp.setText("OFF/HOLD");
- OffTemp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
- OffTemp.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- OffTempActionPerformed(evt);
- }
- });
- OnTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- OnTemp.setText("ON");
- OnTemp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
- OnTemp.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- OnTempActionPerformed(evt);
- }
- });
- TempValue.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- TempValue.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- TempValueActionPerformed(evt);
- }
- });
- jLabel19.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
- jLabel19.setText("<html><sup>o</sup>C");
- TempVolValue.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- TempVolValue.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- TempVolValueActionPerformed(evt);
- }
- });
- jLabel20.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
- jLabel20.setText("V");
- PlotTemp.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- PlotTemp.setText("PLOT/SAVE");
- PlotTemp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
- PlotTemp.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PlotTempActionPerformed(evt);
- }
- });
- TempScale.setForeground(new java.awt.Color(0, 0, 255));
- TempScale.setMajorTickSpacing(10);
- TempScale.setMinorTickSpacing(5);
- TempScale.setPaintLabels(true);
- TempScale.setPaintTicks(true);
- TempScale.setValue(0);
- javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
- jPanel3.setLayout(jPanel3Layout);
- jPanel3Layout.setHorizontalGroup(
- jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel3Layout.createSequentialGroup()
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel3Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(TempScale, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(13, 13, 13)
- .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(jPanel3Layout.createSequentialGroup()
- .addGap(19, 19, 19)
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel3Layout.createSequentialGroup()
- .addComponent(TempVolValue, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jLabel20))
- .addGroup(jPanel3Layout.createSequentialGroup()
- .addComponent(TempValue, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))))
- .addGap(25, 39, Short.MAX_VALUE)
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(PlotTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(OffTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(OnTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap())
- );
- jPanel3Layout.setVerticalGroup(
- jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel3Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(OnTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(TempVolValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel20))
- .addGap(8, 8, 8)
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(OffTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(TempValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(PlotTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addComponent(TempScale, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(34, Short.MAX_VALUE))
- );
- ExitButton.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- ExitButton.setForeground(new java.awt.Color(255, 0, 51));
- ExitButton.setText("EXIT");
- ExitButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
- ExitButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- ExitButtonActionPerformed(evt);
- }
- });
- jLabel16.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jLabel16.setForeground(new java.awt.Color(0, 0, 204));
- jLabel16.setText("<html>SENSOR-TEMPERATURE (<sup>o</sup>C)");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(134, 134, 134))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(593, 593, 593)
- .addComponent(ExitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(218, 218, 218)
- .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(9, 9, 9)
- .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(35, 35, 35)
- .addComponent(ExitButton)
- .addGap(0, 0, 0)
- .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- System.exit(0);
- }
- private void OffTempActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- onADC=0;
- OffTemp.setEnabled(false);
- OnTemp.setEnabled(true);
- temps.close();
- }
- private void OnTempActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- timer.start();
- onADC=1;
- try{
- temps = new PrintStream(new FileOutputStream("temp.dat"));//open the file
- } catch(Exception e){System.out.println("ERROR FILE WRITTING");}
- OnTemp.setEnabled(false);
- OffTemp.setEnabled(true);
- PlotTemp.setEnabled(true);
- }
- private void TempValueActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void TempVolValueActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void PlotTempActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Runtime cgp = Runtime.getRuntime();
- try {
- Process prcs = cgp.exec("wgnuplot plottemp.plt");
- } catch (IOException e) {
- System.err.println ("Error calling gnuplot");
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new ComIntUI1().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton ExitButton;
- private javax.swing.JButton OffTemp;
- private javax.swing.JButton OnTemp;
- private javax.swing.JButton PlotTemp;
- private javax.swing.JSlider TempScale;
- private javax.swing.JTextField TempValue;
- private javax.swing.JTextField TempVolValue;
- private javax.swing.ButtonGroup buttonGroup1;
- private javax.swing.ButtonGroup buttonGroup2;
- private javax.swing.ButtonGroup buttonGroup3;
- private javax.swing.ButtonGroup buttonGroup4;
- private javax.swing.ButtonGroup buttonGroup5;
- private javax.swing.ButtonGroup buttonGroup6;
- private javax.swing.ButtonGroup buttonGroup7;
- private javax.swing.ButtonGroup buttonGroup8;
- private java.awt.Canvas canvas1;
- private javax.swing.JLabel jLabel16;
- private javax.swing.JLabel jLabel19;
- private javax.swing.JLabel jLabel20;
- private javax.swing.JLabel jLabel9;
- private javax.swing.JPanel jPanel3;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTable jTable1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement