Fast response on first Socket I/O request but slow every other time when communicating with remote serial port

Posted by GreenGodot on Stack Overflow See other posts from Stack Overflow or by GreenGodot
Published on 2013-07-01T15:36:16Z Indexed on 2013/07/01 16:21 UTC
Read the original article Hit count: 195

Filed under:
|
|
|

I'm using sockets to pass Serial commands to a remote device. And the response to that request is sent back and printed out. However, I am having a problem in that the first time it is instant but the rest of the time it can take up to 20 seconds to receive a reply.

I think the problem is with my attempt at threading but I am not entirely sure.

                    new Thread() { 
                        @Override
                        public void run() {
                            System.out.println("opened");
                            try {
                                isSocketRetrieving.setText("Opening Socket");
                                socket = new Socket(getAddress(), getRemotePort()));

                                DataOutput = new DataOutputStream(socket
                                        .getOutputStream());

                                inFromServer = new BufferedReader(
                                        new InputStreamReader(socket
                                                .getInputStream()));

                                String line = "";

                                isSocketRetrieving.setText("Reading Stream......");

                                while ((line = inFromServer.readLine()) != null) {

                                    System.out.println(line);
                                    if (line.contains(getHandshakeRequest())) {


                                        DataOutput.write((getHandshakeResponse()toString() + "\r").getBytes());
                                        DataOutput.flush();
                                        DataOutput
                                                .write((getCommand().toString() + "\r").getBytes());
                                        DataOutput.flush();
                                        int pause = (line.length()*8*1000)/getBaud();
                                        sleep(pause);

                                    } else if (line.contains(readingObject
                                            .getExpected())) {

                                        System.out.println(line);
                                        textArea.append("value = " + line
                                                + "\n");
                                        textAreaScroll.revalidate();
                                        System.out.println("Got Value");
                                        break;
                                    }
                                }

                                System.out.println("Ended");
                                try {
                                    inFromServer.close();
                                    DataOutput.close();
                                    socket.close();
                                    isSocketRetrieving.setText("Socket is inactive...");
                                    rs232Table.addMouseListener(listener);
                                    interrupt();
                                    join();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } catch (InterruptedException e) {
                                    System.out.println("Thread exited");
                                }
                            } catch (NumberFormatException e1) {
                                e1.printStackTrace();
                            } catch (UnknownHostException e1) {
                                e1.printStackTrace();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }.start();

© Stack Overflow or respective owner

Related posts about java

Related posts about serial-port