ArrayIndexOutOfBoundsException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ivannavi
    New Member
    • Jan 2007
    • 24

    #1

    ArrayIndexOutOfBoundsException


    private void jButton1ActionP erformed(java.a wt.event.Action Event evt) {

    String Temp = jComboBox1.getS electedItem().t oString();

    testcomm.TestFo rm.WriteParamToPic("c"+Temp);

    }


    public static void WriteParamToPic (String ParamIn)
    {
    char [] WriteParam = new char[4];
    byte [] FinalWriteParam = new byte[4];
    WriteParam = ParamIn.toCharA rray();
    for(int i=0; i<4; i++){
    FinalWriteParam[i] = (byte) WriteParam[i];
    Comm.PutPointer Data(FinalWrite Param,4);
    }
    }







    I have:
    jComboBox1.setM odel(new javax.swing.Def aultComboBoxMod el(new String[] {"30", "50" }));
    i get nex error:
    java.lang.Array IndexOutOfBound sException


    Hove must i configure argument for WriteParamToPic ?
    I have trayed to change number of arrays in WriteParamToPic, but it returns me other errors.

    Thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ivannavi

    private void jButton1ActionP erformed(java.a wt.event.Action Event evt) {



    String Temp = jComboBox1.getS electedItem().t oString();



    testcomm.TestFo rm.WriteParamToPic(\"c\"+Temp);



    }



    public static void WriteParamToPic (String ParamIn)

    {

    char [] WriteParam = new char[4];

    byte [] FinalWriteParam = new byte[4];

    WriteParam = ParamIn.toCharA rray();

    for(int i=0; i<4; i++){

    FinalWriteParam[i] = (byte) WriteParam[i];

    Comm.PutPointer Data(FinalWrite Param,4);

    }

    }













    I have:

    jComboBox1.setM odel(new javax.swing.Def aultComboBoxMod el(new String[] {\"30\", \"50\" }));

    i get nex error:

    java.lang.Array IndexOutOfBound sException





    Hove must i configure argument for WriteParamToPic ?

    I have trayed to change number of arrays in WriteParamToPic, but it returns me other errors.



    Thank you


    FinalWriteParam[i] = (byte) WriteParam[i];
    At this point WriteParam is longer than FinalWriteParam because

    Code:
    WriteParam = ParamIn.toCharArray();


    is returning an Array os size bigger than 4. You have to decide what you want to do with the surplus bits first to solve that problem

    Comment

    • ivannavi
      New Member
      • Jan 2007
      • 24

      #3
      Thank you wery much.I don't get error any more.

      But now, I’ll gone to tray to explain next problem. And if we well resolve it, I’ll
      be much closer to my final faculty work.

      I'm sending data (parameters) to the PIC microcontroller . I have made application for regulation of temperature. And to control and supervise it i need to use a graphic interface. So, from Hyper terminal i can do it wery well.
      My PIC understands characters sent from keyboard one by one.

      But in my Java work:...
      my code doesn’t accurate me valid transmission. I have to press (activate transmission) on button several time to get result and much time the transferred data isn’t correct. I'll appreciate every suggestions because i assume that them well lead me to conclusion have to resolve communication problem.

      Now when I think. I have programmed microcontroller to activate interrupt itch time it gets data (one character)on him input. So it could be resolution to include a delay between sending of
      characters to give microcontroller time to elaborate data.
      Yes , I think this could be wright way. So if I resume I have to send character by character and to include minimum delay between itch transmission.Wr ight?

      To continue before sent code:
      Code:
       void PutPointerData(byte [] ChIn, int ChNumb) 
      {
      try 
      {
      OutStream.write(ChIn,0,ChNumb);
      OutStream.flush();
      } catch (IOException ex) 
      {
      ex.printStackTrace();
      }
      }

      Comment

      • ivannavi
        New Member
        • Jan 2007
        • 24

        #4
        And now i have a concrete problem:

        Code:
        public static void WriteParamToPic(String ParamIn)
            {
                char [] WriteParam = new char[4];       
                WriteParam =ParamIn.toCharArray();
                for(int i=0; i<4; i++){            
                  Comm.PutData((byte) WriteParam[i]) ;
                }
            }
        
         //  to send induvidual character
         void PutData (byte ChIn)	    
                    {
                        try {
                            OutStream.write((byte) ChIn);
                            OutStream.flush();
                            } catch (IOException ex) 
                            {
                            ex.printStackTrace();
                            }
                    }
        I can't test my code because i need a delay between itch sending.
        So I have tray at next way:
        I think that from the code can be ungderstud that i would like to make a delay betwen induidual sending, but like you my see i realy need help.


        Code:
        public static void WriteParamToPic(String ParamIn)
            {
                char [] WriteParam = new char[4];       
                WriteParam =ParamIn.toCharArray();
                for(int i=0; i<4; i++){
                 Comm.charTimer();        //eror:create a metod charTimer() in     testcomm.TwoWaySerialComm
                 Comm.PutData((byte)  WriteParam[i]) ;
                }
               
            }
           
            private  void charTimer() 
            {
        	 charTmr = new Timer();
                charTmr.schedule(doNothing,1000);  //error:create fild doNothing
            }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by ivannavi
          And now i have a concrete problem:

          Code:
          public static void WriteParamToPic(String ParamIn)
          {
          char [] WriteParam = new char[4]; 
          WriteParam =ParamIn.toCharArray();
          for(int i=0; i<4; i++){ 
          Comm.PutData((byte) WriteParam[i]) ;
          }
          }
           
          // to send induvidual character
          void PutData (byte ChIn)	 
          {
          try {
          OutStream.write((byte) ChIn);
          OutStream.flush();
          } catch (IOException ex) 
          {
          ex.printStackTrace();
          }
          }
          I can't test my code because i need a delay between itch sending.
          So I have tray at next way:
          I think that from the code can be ungderstud that i would like to make a delay betwen induidual sending, but like you my see i realy need help.


          Code:
          public static void WriteParamToPic(String ParamIn)
          {
          char [] WriteParam = new char[4]; 
          WriteParam =ParamIn.toCharArray();
          for(int i=0; i<4; i++){
          Comm.charTimer(); //eror:create a metod charTimer() in testcomm.TwoWaySerialComm
          Comm.PutData((byte) WriteParam[i]) ;
          }
           
          }
           
          private void charTimer() 
          {
          	 charTmr = new Timer();
          charTmr.schedule(doNothing,1000); //error:create fild doNothing
          }
          You can use the Thread.sleep method

          Comment

          • ivannavi
            New Member
            • Jan 2007
            • 24

            #6
            Code:
            public static void WriteParamToPic(String ParamIn)
                {
                    char [] WriteParam = new char[4];       
                    WriteParam =ParamIn.toCharArray();
                    for(int i=0; i<4; i++){       
                     Comm.PutData((byte) WriteParam[i]) ;
                      try {
                          Thread.sleep (3);
                          }
                      catch ( Exception ex ) {
                                 ex.printStackTrace();
                           }
                    }
                   
                }


            In this way it works.
            Thank you wery much.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by ivannavi
              Code:
              public static void WriteParamToPic(String ParamIn)
              {
              char [] WriteParam = new char[4]; 
              WriteParam =ParamIn.toCharArray();
              for(int i=0; i<4; i++){ 
              Comm.PutData((byte) WriteParam[i]) ;
              try {
              Thread.sleep (3);
              }
              catch ( Exception ex ) {
              ex.printStackTrace();
              }
              }
               
              }


              In this way it works.
              Thank you wery much.
              Anytime. Notice also that I did not really give you much help here. Just a few comments and you managed to fix the problem yourself. It's always a pleasure assisting those who prefer to write their own code themselves.

              Comment

              • ivannavi
                New Member
                • Jan 2007
                • 24

                #8
                Code:
                   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                    
                        
                        if(testcomm.TestForm.mainTmr != null)
                           testcomm.TestForm.mainTmr.cancel();      
                        
                     //other code  
                
                        testcomm.TestForm.startTimer() ;
                        // non static metod cannot be referenced from static contrxt
                    }

                And timer:
                Code:
                public  void startTimer() 
                    {
                	mainTmr = new Timer();
                        mainTmr.schedule((new TimerChhRunCls()),1000,timeSampleLong);
                    }
                    class TimerChhRunCls extends TimerTask 
                    {
                        public void run() 
                        {
                            ReadTemperatureFromRS232();
                        }
                        
                    }
                Sure, that with yours help it can do more.



                My school work is done, but I wont to improve(make beater) it, that it can be used in technical application

                for real. So, there is some more work to do.

                I have to stop timer and activate it again after other code (data transmission) has been done.



                But I get error (comment). What must i do to resolve it? A must admit that theory about static and non static isn't steal clear to me.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by ivannavi
                  Code:
                   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
                   
                   
                  if(testcomm.TestForm.mainTmr != null)
                  testcomm.TestForm.mainTmr.cancel(); 
                   
                  //other code 
                   
                  testcomm.TestForm.startTimer() ;
                  // non static metod cannot be referenced from static contrxt
                  }

                  And timer:
                  Code:
                  public void startTimer() 
                  {
                  	mainTmr = new Timer();
                  mainTmr.schedule((new TimerChhRunCls()),1000,timeSampleLong);
                  }
                  class TimerChhRunCls extends TimerTask 
                  {
                  public void run() 
                  {
                  ReadTemperatureFromRS232();
                  }
                   
                  }
                  Sure, that with yours help it can do more.



                  My school work is done, but I wont to improve(make beater) it, that it can be used in technical application

                  for real. So, there is some more work to do.

                  I have to stop timer and activate it again after other code (data transmission) has been done.



                  But I get error (comment). What must i do to resolve it? A must admit that theory about static and non static isn't steal clear to me.
                  What error(comment) are you getting.

                  Comment

                  • ivannavi
                    New Member
                    • Jan 2007
                    • 24

                    #10
                    // non static metod cannot be referenced from static context

                    and it referes to next line of code:
                    testcomm.TestFo rm.startTimer() ;


                    it is part of first block of code in previous post.

                    Thank you

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by ivannavi
                      // non static metod cannot be referenced from static context

                      and it referes to next line of code:
                      testcomm.TestFo rm.startTimer() ;


                      it is part of first block of code in previous post.

                      Thank you
                      This may really depend on how you designed your classes, but
                      what happens if you make the startTimer method static?

                      Comment

                      • ivannavi
                        New Member
                        • Jan 2007
                        • 24

                        #12
                        First i have changed metod to static and then variable timeSampleLong too.
                        But i could not resolve next error :
                        "non-static variable this couldnot be referenced from a static context"
                        which refers to:
                        mainTmr.schedul e((new TimerChhRunCls( )),1000,timeSam pleLong);

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by ivannavi
                          First i have changed metod to static and then variable timeSampleLong too.
                          But i could not resolve next error :
                          "non-static variable this couldnot be referenced from a static context"
                          which refers to:
                          mainTmr.schedul e((new TimerChhRunCls( )),1000,timeSam pleLong);
                          Can you post the code for the TestForm class?

                          Comment

                          • ivannavi
                            New Member
                            • Jan 2007
                            • 24

                            #14
                            It's split in two:
                            First:

                            Code:
                            package testcomm;
                            import java.awt.BorderLayout;
                            import java.util.Date;
                            import java.util.concurrent.locks.Lock;
                            import java.util.concurrent.locks.ReentrantLock;
                            import java.util.Timer;
                            import java.util.TimerTask;
                            import javax.swing.JButton;
                            import javax.swing.JFrame;
                            import javax.swing.JPanel;
                            import org.jfree.chart.ChartPanel;
                            import org.jfree.chart.JFreeChart;
                            import org.jfree.chart.JFreeChart;
                            import org.jfree.chart.demo.TimeSeriesChartDemo1;
                            import org.jfree.data.time.Second;
                            import org.jfree.data.time.TimeSeries;
                            import org.jfree.data.time.TimeSeriesCollection;
                            import org.jfree.ui.RefineryUtilities;
                            
                            
                            public class TestForm extends javax.swing.JFrame {
                            
                                private Lock SendLock = new ReentrantLock();
                                private static String ComPort = "COM4";
                                
                                
                                TChartWrite TCh;
                                JFreeChart MainChrt;
                                ChartPanel MainChartPnl;
                                TimeSeriesCollection dataset;
                                TimeSeries tseries;
                                
                              public static   Timer mainTmr;    
                            String timeSample;
                            static long timeSampleLong ;
                                public static TwoWaySerialComm Comm;
                                public static int Jajca = 0;
                                public static byte [] GenBuffer = new byte[1024];
                                public static int GenBuffCnt = 0;
                                public int GenCount=0;
                                
                                /** Creates new form TestForm */
                                public TestForm() {
                                    initComponents();
                                    DrawGraph();
                                }
                                
                                // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
                                private void initComponents() {
                                    jFrame1 = new javax.swing.JFrame();
                                    jButton1 = new javax.swing.JButton();
                                    jButton2 = new javax.swing.JButton();
                                    jPanel1 = new javax.swing.JPanel();
                                    jLabel1 = new javax.swing.JLabel();
                                    jTextField1 = new javax.swing.JTextField();
                                    jLabel2 = new javax.swing.JLabel();
                                    jTextField2 = new javax.swing.JTextField();
                                    jButton4 = new javax.swing.JButton();
                                    jButton5 = new javax.swing.JButton();
                                    jButton6 = new javax.swing.JButton();
                                    jTextField3 = new javax.swing.JTextField();
                                    jTextField4 = new javax.swing.JTextField();
                                    jTextField5 = new javax.swing.JTextField();
                                    jComboBox1 = new javax.swing.JComboBox();
                            
                                    javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
                                    jFrame1.getContentPane().setLayout(jFrame1Layout);
                                    jFrame1Layout.setHorizontalGroup(
                                        jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGap(0, 400, Short.MAX_VALUE)
                                    );
                                    jFrame1Layout.setVerticalGroup(
                                        jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGap(0, 300, Short.MAX_VALUE)
                                    );
                            
                                    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                                    setMinimumSize(new java.awt.Dimension(900, 600));
                                    addWindowListener(new java.awt.event.WindowAdapter() {
                                        public void windowActivated(java.awt.event.WindowEvent evt) {
                                            formWindowActivated(evt);
                                        }
                                    });
                            
                                    jButton1.setText("OPEN");
                                    jButton1.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jButton1ActionPerformed(evt);
                                        }
                                    });
                            
                                    jButton2.setText("Run Timer");
                                    jButton2.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jButton2ActionPerformed(evt);
                                        }
                                    });
                            
                                    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                                    jPanel1.setLayout(jPanel1Layout);
                                    jPanel1Layout.setHorizontalGroup(
                                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGap(0, 404, Short.MAX_VALUE)
                                    );
                                    jPanel1Layout.setVerticalGroup(
                                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGap(0, 246, Short.MAX_VALUE)
                                    );
                            
                                    jLabel1.setText("Temperatura");
                            
                                    jTextField1.setText("25.55");
                            
                                    jLabel2.setText("\u0160tevec");
                            
                                    jTextField2.setText("10000");
                            
                                    jButton4.setText("Stop Timer");
                                    jButton4.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jButton4ActionPerformed(evt);
                                        }
                                    });
                            
                                    jButton5.setText("Params");
                                    jButton5.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jButton5ActionPerformed(evt);
                                        }
                                    });
                            
                                    jButton6.setText("Read");
                                    jButton6.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jButton6ActionPerformed(evt);
                                        }
                                    });
                            
                                    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1000", "10000", "100000", "1000000" }));
                                    jComboBox1.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jComboBox1ActionPerformed(evt);
                                        }
                                    });
                            
                                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                                    getContentPane().setLayout(layout);
                                    layout.setHorizontalGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addContainerGap()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(jLabel1)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(jLabel2)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addGap(22, 22, 22)
                                                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(jButton6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jTextField3)
                                                        .addComponent(jTextField4)
                                                        .addComponent(jTextField5)))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jButton1)
                                                    .addGap(119, 119, 119)
                                                    .addComponent(jButton2)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jButton4)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jButton5)
                                                    .addContainerGap())))
                                    );
                                    layout.setVerticalGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addContainerGap()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton1)
                                                .addComponent(jButton2)
                                                .addComponent(jButton4)
                                                .addComponent(jButton5))

                            Comment

                            • ivannavi
                              New Member
                              • Jan 2007
                              • 24

                              #15
                              and second:
                              Code:
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                  .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                  .addComponent(jLabel1)
                                                  .addComponent(jLabel2)
                                                  .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                  .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                  .addGroup(layout.createSequentialGroup()
                                                      .addComponent(jButton6)
                                                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                      .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                      .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                      .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                              .addContainerGap())
                                      );
                                      pack();
                                  }// </editor-fold>                        
                              
                                  private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
                                  
                                    timeSample = jComboBox1.getSelectedItem().toString();
                                    timeSampleLong= Long.parseLong(timeSample);
                                  }                                          
                              
                                  private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                                      String ZelTemp = ReadParamFromPIC('W');
                                      String Histereza = ReadParamFromPIC('H');
                                      String CritTemp = ReadParamFromPIC('C');
                                      
                                      jTextField3.setText(ZelTemp);
                                      jTextField4.setText(Histereza);
                                      jTextField5.setText(CritTemp);
                                  }                                        
                              
                                  private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                                    //  if(mainTmr != null)
                                    //      mainTmr.cancel();             
                                      Setting Stng = new Setting();
                                      Stng.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ;
                                      Stng.setLocation ( 300 , 300 ) ;
                                      Stng.setVisible ( true ) ;
                                  }                                        
                              
                                  private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                                          mainTmr.cancel();
                                  }                                        
                              
                                  private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
                              // TODO add your handling code here:
                                  }                                    
                                  private void DrawGraph()
                                  {
                                      dataset = createDataset();
                                      TCh = new TChartWrite();
                                      MainChrt = TCh.createChart(dataset);
                                      MainChartPnl = new ChartPanel(MainChrt);
                                      MainChartPnl.setBounds(0,0,640,480);
                                      jPanel1.add(MainChartPnl);
                                      jPanel1.setVisible( true );
                                      MainChartPnl.validate();
                                      tseries = dataset.getSeries(0);
                                  }
                                  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                              // TODO add your handling code here:
                                      startTimer();
                                  }                                        
                              
                                  //Branje in pisanje ComPorta!
                                  public String ReadParamFromPIC(char ParamIn)
                                  {
                                      GenBuffCnt = 0;
                                      Comm.PutData((byte)ParamIn);
                                      if(GenBuffCnt > 5)
                                      {
                                          GenBuffCnt = 0;
                                          return null;
                                      }
                                      while(GenBuffCnt!=5);
                                      byte [] WriteBuff = ReturnNewBytes(GenBuffer);
                                      String Data = new String(WriteBuff,0,4);        
                                      GenBuffCnt = 0;
                                      return Data;
                                  }
                                  public static void WriteParamToPic(String ParamIn)
                                  {
                                      char [] WriteParam = new char[4];       
                                      WriteParam =ParamIn.toCharArray();
                                      for(int i=0; i<4; i++){       
                                       Comm.PutData((byte) WriteParam[i]) ;
                                        try {
                                            Thread.sleep (3);
                                            }
                                        catch ( Exception ex ) {
                                                   ex.printStackTrace();
                                             }
                                      }
                                     
                                  }
                                  private void ReadTemperatureFromRS232()
                                  {
                                      String Data = ReadParamFromPIC('R');
                                      jTextField1.setText(Data);
                                      GenCount++;
                                      jTextField2.setText(Integer.toString(GenCount));
                                      float Dat = Float.parseFloat(Data);
                                      tseries.addOrUpdate(new Second(new Date()),Dat);
                                  }
                                  private byte [] ReturnNewBytes(byte [] DataIn)
                                  {
                                      byte [] DataOut = new byte[5];
                                      DataOut[0] = DataIn[1];
                                      DataOut[1] = DataIn[2];
                                      DataOut[2] = '.';
                                      DataOut[3] = DataIn[4];
                                      
                                      return DataOut;
                                  }
                                  public static void startTimer() 
                                  {
                              	mainTmr = new Timer();
                                      mainTmr.schedule((new TimerChhRunCls()),1000,timeSampleLong);
                                  }
                                  class TimerChhRunCls extends TimerTask 
                                  {
                                      public void run() 
                                      {
                                          ReadTemperatureFromRS232();
                                      }
                                      
                                  }
                                 
                                  class TimerTestRun extends TimerTask 
                                  {
                                      public void run() 
                                      {
                                          UpdateBoxAndGraph();
                                      }
                                      
                                  }
                                  private void UpdateBoxAndGraph()
                                  {
                                      GenCount++;
                                      jTextField2.setText(Integer.toString(GenCount));
                                      float Dat = Float.parseFloat("13.4");
                                      Dat = Dat + GenCount;
                                      tseries.addOrUpdate(new Second(new Date()),Dat);
                                      
                                     
                                  }
                                  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                                              
                                              try
                                              {
                              	            Comm = new TwoWaySerialComm();
                                                  Comm.connect(ComPort);
                              	        }
                              	        catch ( Exception e )
                              	        {
                              	         
                              	            e.printStackTrace();
                              	        }
                                              
                                  }                                        
                                  private TimeSeriesCollection createDataset() 
                                  {
                                      TimeSeries ts = new TimeSeries("T", Second.class);
                                      TimeSeriesCollection dataset = new TimeSeriesCollection();
                                      dataset.addSeries(ts);
                                      ts.setMaximumItemAge(30*60);
                                      return dataset;
                                  }
                                 
                                  public static void main(String args[]) {
                                      java.awt.EventQueue.invokeLater(new Runnable() {
                                          public void run() {
                                              new TestForm().setVisible(true);
                                          }
                                      });
                                  }
                                  
                                  // Variables declaration - do not modify                     
                                  private javax.swing.JButton jButton1;
                                  private javax.swing.JButton jButton2;
                                  private javax.swing.JButton jButton4;
                                  private javax.swing.JButton jButton5;
                                  private javax.swing.JButton jButton6;
                                  private javax.swing.JComboBox jComboBox1;
                                  private javax.swing.JFrame jFrame1;
                                  private javax.swing.JLabel jLabel1;
                                  private javax.swing.JLabel jLabel2;
                                  private javax.swing.JPanel jPanel1;
                                  public static javax.swing.JTextField jTextField1;
                                  private javax.swing.JTextField jTextField2;
                                  private javax.swing.JTextField jTextField3;
                                  private javax.swing.JTextField jTextField4;
                                  private javax.swing.JTextField jTextField5;
                                  // End of variables declaration                   
                                  
                                  
                              }

                              Comment

                              Working...