开发者

Executing a block of code after all components are loaded

I have a program which talks to serial port.

Threading is involved in this serial port program.

I am showing a swing frame to display the status of the serial port.

The problem is when I call setVisible(true) the window is not loading fully.

I tried using isValid() to start the serial port script after the window finished loading but it never worked.

I want to initiate the serial port script after the window finished loading.

How do i get over this problem ?

//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 used to read and write the port which uses 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";
}

When I used the following code in the place of form_objects.cardPayment.setVisible(true) I couldn't get the serial port methods called.

Can you guys explain me why is it happening?

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

                public void run() {
                    form_objects.cardPayment.setVisible(true);开发者_如何学C
                }
            });


In the same thread you call form_objects.cardPayment.setVisible(true);, you're also calling while (true) { and this will tie up your Swing app until the port code is done doing its thing.

You need to read up on using background threads as you appear to be doing all on the Swing thread. Look into using a SwingWorker object. e.g., Lesson: Concurrency in Swing

Edit 1:

@Deepak: if you're still stuck, consider creating and posting, perhaps even as an answer in this thread or an addendum to your question, an SSCCE (please look at the link). This may have to be a little longer than the typical SSCCE and would be a little tricky as you'll have to simulate some of the background processes -- enough to reproduce your problem -- but could help us get you a solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜