Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:51:16+00:00 2026-06-05T07:51:16+00:00

I am trying to build a timeout for a call/response ping. This is to

  • 0

I am trying to build a timeout for a call/response ping. This is to determine which CommPort to use in an auto-connect sequence. When I send the CALL_SIGNAL byte[], I expect to get “FU” in reply. This is how I will determine which is my device. The SSCCE is below (sorry for the length, but this is the shortened version)

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import javax.comm.CommPortIdentifier;
import javax.comm.CommPort;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;

public class PingTest {
    public static final long PING_TIMEOUT = 1000; // in millis
    public static final byte[] CALL_SIGNAL = {70, 68, 73, 68};

    public boolean ping(CommPortIdentifier cpi)  {
        System.out.println("Pinging " + cpi.getName());
        CommPort port = null;
        InputStream input = null;
        OutputStream output = null;

        //Initialization test
        try {
            port = cpi.open("pingTest", 50);
            input = port.getInputStream();
            output = port.getOutputStream();
        } catch (PortInUseException ex) {
            if (cpi.getCurrentOwner().startsWith("pingTest")) {
                System.out.println("Port already owned by this application.");
                return true;
            }
            return false;
        } catch (Exception ex) {
            try {
            port.close();
            } catch (NullPointerException e) {
            }
            System.out.println("Failed initialization test.");
            System.err.println(ex);
            return false;
        }
        System.out.println("Passed initialization test."); 

        //Call and response test
        final Stop stop = new Stop();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                System.out.println("Stop timer triggered.");
                stop.shouldStop = true;
            }
        }, PING_TIMEOUT);
        try {
            System.out.println("Writing...");
            output.write(CALL_SIGNAL);
            System.out.println("Reading...");
            boolean waitForF = true;
            while (!stop.shouldStop && !stop.didPass) {
                if (waitForF) {
                    if ((byte) 'F' == input.read()) {
                        waitForF = false;
                    }
                } else {
                    System.out.println('F');
                    if ((byte) 'U' == input.read()) {
                        System.out.println('U');
                        stop.didPass = true;
                    } else {
                        System.out.println();
                        waitForF = true;
                    }
                }
            }
        } catch (IOException ex) {
            System.out.println("Failed I/O test.");
            return false;
        } finally {
            port.close();
        }

        if (stop.didPass) {
            System.out.println("Successful ping.");
            return true;
        }
        System.out.println("Failed call and response test.");
        return false;
    }

    public static void main(String[] args) {
        PingTest pinger = new PingTest();
        Enumeration<CommPortIdentifier> ports = CommPortIdentifier.getPortIdentifiers();
        boolean trigger = true;
        String name = null;
        while (trigger && ports.hasMoreElements) {
            CommPortIdentifier cpi = ports.nextElement();
            name = cpi.getName();
            trigger = !ping(cpi);
        }
        if (trigger) {
            System.out.println("Found it. It is " + name);
        } else {
            System.out.println("Not Found");
        }
    }
}

class Stop {
    boolean shouldStop = false;
    boolean didPass = false;
}

My output is:

Pinging COM1
Passed initialization test.
Writing...
Reading...
Stop timer triggered.

And then the application freezes. Is there a problem with trying to stop the while loop with a Timer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:51:18+00:00Added an answer on June 5, 2026 at 7:51 am

    Your reads are blocking. You need to use Timeouts

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying build a method which returns the shortest path from one node to
I am trying to build a platform to auto-deploy applications I build in NodeJS.
I'm trying to use httplib to send credit card information to authorize.net. When i
So I am trying to build a range pinger which job will be to
I am trying to build mysql-5.0.33 client library with SSL support. For which I
I'm trying build an App Engine connected Android application and am having some problems
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I
Trying to build a Metro app using Javascript and having issues with IndexedDb. I
Trying to build Wireshark from source as there is no Linux installer and I
Trying to build a plot with an exponent x-axis. It should start with 0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.