java Gui is blocked, serial communication - graphic jfreechart

0

I have a problem with the GUI that freezes, in my application I use the library rxtx 2.1-7 and the library jfreechart 1.0.19. Basically communication is established and port events are read when receiving data. the data is added to a list and this list is passed as a parameter to a DefaultBoxAndWhiskerCategoryDataset. after graphing the GUI it is blocked in a random way it can be in two or three seconds or in 10 minutes. always after graphing. it never gets blocked when establishing communication. I modify Jpanel so that the graphics appear and Jlabel to put the state of the connected equipment by serial port. there is no error report from the IDE netbeans. I show you the spartes of or code that I think are relevant.

public synchronized void serialEvent(SerialPortEvent oEvent) {

    while (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) 
    {

        try {
            int data; //Se declaran las variables
            datoa = RecibeDatos(); //Se lee los datos en el puerto serie

            if (data > 0) { 
                Mensaje = Mensaje + (char) data; //acumulamos el  mensaje caracter por caracter
                System.out.println(Mensaje);                    
                if ((Mensaje.charAt(Mensaje.length() - 1) == '&')) 
                { 
                    if(Mensaje.contains("#$%"))
                    {
                        publicador(Mensaje);
                        Mensaje="";
                        break;
                    }
                         Publicadorresultados(Mensaje);
                         Mensaje="";


                }
            }
        } catch (Exception e) {
            System.err.println(e.toString()+ "      error  mensaje");
        }
    }
}

to modify the JLabel use a switch:

switch(sel3)   /// evalua el   codigo del  error  para la celda 
                        {
                            case 1: 
                                    Celda1.removeAll();
                                    Celda1.setText("Reset por Panel");
                                    Celda1.setForeground(Color.BLACK);
                                    Celda1.validate();
                                    Celda1.repaint();  
                            break;        
                            case 2: 
                                    Celda1.removeAll();
                                    Celda1.setText("Error, limpiar celda");
                                    Celda1.setForeground(Color.RED);
                                    Celda1.validate();
                                    Celda1.repaint(); 
                            break;
                            case 3:
                                    Celda1.removeAll();
                                    Celda1.setText("Inicia Test");
                                    Celda1.setForeground(Color.ORANGE);
                                    Celda1.validate();
                                    Celda1.repaint(); 
                            break; 

to graph:

public   void graficador ()
{


    BoxAndWhiskerItem item = BoxAndWhiskerCalculator
    .calculateBoxAndWhiskerStatistics(lista);
    DefaultBoxAndWhiskerCategoryDataset data = new DefaultBoxAndWhiskerCategoryDataset();
    data.add(item, "Serie 1", "Test");
    BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
    renderer.setMeanVisible(false);
    renderer.setFillBox(false);
    JFreeChart chart = ChartFactory.createBoxAndWhiskerChart(
    "Diagrama de Cajas","Prueba","Tiempo (s)", data, false);
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setRenderer(renderer);
    plot.setOrientation(PlotOrientation.HORIZONTAL);
    ChartPanel p1= new ChartPanel(chart);
    p1.setMouseWheelEnabled(true);
    CgraficoCaja.removeAll();
    CgraficoCaja.setLayout(new java.awt.BorderLayout());
    CgraficoCaja.add(p1,BorderLayout.CENTER);
    CgraficoCaja.validate();

    //this.validate();

}

I thought it was a problem with the libraries and I changed them, however the interface keeps freezing. I saw in the network something about not modifying the SWING elements from EDT and instead using SwingWorker or in other cases using invokelater (). but it does not work. the gui is blocked and the only option is to close from the ide or the task manager.

    
asked by Roockdrigo 27.09.2017 в 20:56
source

1 answer

0

You have to run the processes on different threads.

  

Thread and Runnable

Here is an example:

new Thread(new Runnable() {

       @Override
       public void run() {
            //Aqui va el codigo
           //rm.procesar_morosos(IP_Servidor, Procesado, Resumen);
       }
   }).start();
    
answered by 05.12.2017 в 04:57