Sign in to follow this  

c/c++ parallel port interfacing, simulating a time interval

This topic is 4694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've programmed in Java ever since. I study C/C++ for some time, but I'm not really proficient with it. I've programmed mostly on games, but this time I had to program in C/C++ for an ECE project. For a newbie C++ programmer, a main loop looks something like this...
while( notOver )
{
    //routines here
}
my problem is that I don't how to simulate a time interval where the application should update its components (or do routines) in C++. like in Java, I could do this...
class JustAClass implements Runnable
{
    int delay = 10000; //delay for every 10 seconds

    public void run()
    {
        long tm = System.currentTimeMillis();
        tm += delay;

        //routines here

        try
        {
            Thread.sleep( Math.max( 0, tm - System.currentTimeMillis() ) );
        }
        catch( InterruptedException ie )
        {
            ie.printStackTrace();
        }
    }
}

//time interval of 10 seconds
How do I do this in C++? That in every 10 seconds, it should do some functions?

Share this post


Link to post
Share on other sites
Suffering from violent finger spasms on your mouse there? [wink]

Anyway, if you're working in Windows you can use Sleep much like you would use Thread.Sleep in Java. However if you don't have such a luxury, I don't believe the C runtime has any sort of analogous function. The best way to implement the delay would be to run an empty loop until a certain time period has elapsed. You can use the time function like this then:

time_t start = time(NULL);
while(time(NULL) - start < 10){}

Which is a ten second delay.

Share this post


Link to post
Share on other sites
IMHO, I would use a timer created from the QueryPerformanceCounter and QueryPerformanceFrequency functions, then do tests from them. As a rule I stay away from sleep calls in games (not saying you can't keep them) and instead I run a set of functions based on how much time has elapsed.

Share this post


Link to post
Share on other sites
For an ECE project I don't imagine that it would matter much either way, however using either Sleep or time would be easier to deal with than the high-precision timer, which may not even be present depending on how old the development machines are.

Share this post


Link to post
Share on other sites
Please try to avoid posting the same question over and over. If you find that you've posted the same question twice (or three times), you can delete one of the extra posts by opening the topic, hitting the edit button on the first post and then checking the "delete" box ("Delete? To delete this post, check this box.") before hitting the "Make Modifications" button.

Share this post


Link to post
Share on other sites
Thanks for those who answered. The thing is, this question is not involved in any game development. It is for parallel port interfacing. We'll send data into the CPU through our circuit which inputs the current temperature in an incubator so we could log its temperature every 10 seconds and make necessary statistics report from it. I need to make the program that reads the data sent into the CPU (through register 378h or 888). Probably, we need to graph the logs.

Thanks anyway.

Share this post


Link to post
Share on other sites

This topic is 4694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this