2011-04-30 73 views
1

我有一个与串口通信的程序。
此串口程序涉及线程。加载所有组件后执行代码块

我正在显示一个摆动框架来显示串行端口的状态。
问题是我打电话给setVisible(true)窗口没有完全加载。
我尝试使用isValid()在窗口完成加载后启动串口脚本,但它从未工作。

我想在窗口加载完成后启动串口脚本。
我该如何解决这个问题?

//Code used to call setVisible() 

form_objects.cardPayment.setVisible(true); 
if (form_objects.cardPayment.isValid()) { 
    openPort.sendMessage(global_variables.port, "@PL\r"); 
    String readMessage = openPort.readMessage(global_variables.port); 
    System.out.println(readMessage); 
    String check_bit[] = readMessage.split(","); 
    System.out.println(check_bit[2]); 
    if (check_bit[0].equalsIgnoreCase("@PL") &&check_bit[2].trim().equals("0")) { 
     card_payment.card_text.setText("Swipe Card"); 
     openPort.sendMessage(global_variables.port, "@PU," + amount + ",,,,1\r"); 
     boolean loop = true; 
     while (loop) { 
      openPort.sendMessage(global_variables.port, "@SR,1,131\r"); 
      readMessage = openPort.readMessage(global_variables.port); 
      if (readMessage.equalsIgnoreCase("Timeout")) { 
       card_payment.card_text.setText("Enter Pin"); 
      } 
      if (!readMessage.equals("") && !readMessage.equals("Timeout")) { 
       loop = false; 
      } 
     } 
     String sr[] = readMessage.split(","); 
     if (sr[1].equals("1") && sr[5].equals("")) { 
      System.out.println("Cancelled"); 
      card_payment.card_text.setText("Payment Cancelled"); 
      form_objects.cardPayment.dispose(); 
     } else if (sr[1].equals("1") && sr[5].equals("T")) { 
      System.out.println("Accepted"); 
      card_payment.card_text.setText("Accepted"); 
      long ptime = Calendar.getInstance().getTimeInMillis(); 
      while(ptime+10000>=Calendar.getInstance().getTimeInMillis()){ 
       //do nothing just stay thhere for 10 seconds 
      } 
      form_objects.cardPayment.dispose(); 
     } else if (sr[1].equals("1") && sr[5].equals("F")) { 
      System.out.println("Declined"); 
      card_payment.card_text.setText("Payment Declined"); 
     } 
    } else { 
     System.out.println("terminal offline"); 
    } 
} 

--Code用于读取和写入它使用thread.sleep()港口---

public static void sendMessage(SerialPort port, String msg) { 

    if (port != null) { 

     System.out.println(msg); 

     try { 

      byte[] bytes = msg.getBytes("US-ASCII"); 

      try { 

       global_variables.outputStream.write(bytes); 

       System.out.println(bytes.length); 

       global_variables.outputStream.flush(); 

      } catch (IOException ex) { 

       Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex); 

      } 

     } catch (UnsupportedEncodingException ex) { 

      Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex); 

     } 

     System.out.println("Opened successfully:" + msg.getBytes()); 

     try { 
      Thread.sleep(2000); // Be sure data is xferred before closing 
      System.out.println("read called"); 
      //byte b[] = new byte[1024]; 
      //global_variables.inputStream.read(b); 

      //System.out.println("available" + global_variables.inputStream.available()); 

      //SimpleRead read = new SimpleRead(); 
      //int read = global_variables.inputStream.read(); 
      //System.out.println("read call ended"+read); 
     } catch (Exception e) { 
     } 

    } 
} 

public static String readMessage(SerialPort port) { 
    byte[] buffer = new byte[1024]; 
    int count=0;      
     try { 
      Thread.sleep(1000);     
      if (global_variables.inputStream.available() > 0) { 
       /*assigning it to count variable makes the read uniform. if we use available() each time the string are not processed fully. 
       * so assign it to count and use the count for rest of the buffer read operation 
       */ 

       count = global_variables.inputStream.available(); 
       System.out.println("Data Available:" + count); 
       for (int i = 0; i < count; i++) { 
        buffer[i] = (byte) global_variables.inputStream.read(); 
       } 
       String response = new String(buffer, 0, count);      
       return response; 
      } else { 
       return "Timeout"; 
      } 

     } catch (InterruptedException ex) { 
      Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex); 
     }   
    return "timeout"; 
} 

当我使用的form_objects.cardPayment.setVisible(true)的地方下面的代码我无法得到串口方法调用。
你们可以解释一下为什么会发生?

java.awt.EventQueue.invokeLater(new Runnable() { 

       public void run() { 
        form_objects.cardPayment.setVisible(true); 
       } 
      }); 
+1

我不得不怀疑你的问题是否真的是捆绑主要的Swing线程,EDT,以及你需要使用后台线程以便你的串口代码不会阻塞Swing。另外,在顶层窗口调用pack()之前是否将所有组件添加到了GUI中?您是否使用SwingUtilities.invokeLater(...)在EDT上启动Swing代码?你能告诉我们更多关于你的程序和显示关键代码吗? – 2011-04-30 18:23:13

+0

检查我的编辑...抱歉我的可怜的格式。我无法得到它的权利:( – Deepak 2011-04-30 18:31:52

+0

@奥古斯托:谢谢你verymuch !!! – Deepak 2011-04-30 18:37:48

回答

4

在你打电话form_objects.cardPayment.setVisible(true);同一个线程,你还呼吁while (true) {,直到该端口的代码是做尽自己的事情,这将占用你的Swing应用程序。

您需要仔细阅读使用后台线程,因为您似乎在Swing线程中执行所有操作。看看使用SwingWorker对象。例如,Lesson: Concurrency in Swing

编辑1:

@Deepak:如果你仍然坚持,考虑创建和发布,甚至在这个线程的答案或增编你的问题,一个SSCCE(请看链接)。这可能要比典型的SSCCE稍长一点,因为你必须模拟一些后台进程 - 足以重现你的问题 - 但可以帮助我们为你解决问题。

+0

检查我的编辑。当我包括我的编辑的最后一部分我的应用程序不执行串行端口部分。你能解释我为什么发生? – Deepak 2011-04-30 18:56:33

+0

谢谢你的我现在已经学会了一个新概念:)但是我会很高兴向你们学习我的编辑工作的最后部分:) – Deepak 2011-04-30 19:31:30

+0

@Deepak:如果仍然卡住,请参阅我的**编辑1:** ab OVE。 – 2011-04-30 20:53:39