GreenGodot
GreenGodot

Reputation: 6753

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

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();

Upvotes: 0

Views: 201

Answers (2)

Knight of Ni
Knight of Ni

Reputation: 1830

Because your socket connection is relatively fast comparing to serial connection, and you send relatively small chunks of data I would play with these methods:

  • socket.setTcpNoDelay(true)
  • socket.getReceiveBufferSize() / socket.setReceiveBufferSize() - (try to change size)
  • socket.getSendBufferSize() / socket.setSendBufferSize() - (try to change size)

Try to setup this once after you open the Socket. Maybe this will help.

Upvotes: 1

neoeahit
neoeahit

Reputation: 1799

   int pause = (line.length()*8*1000)/getBaud();
   sleep(pause);

Because of this perhaps?

Upvotes: 1

Related Questions