Hi there im wondering how to use a time delay in my code. What i have done is searched through a saved file and picked out infomation the send it to the rs232 port. what i want to do is create a time delay of 100 micro seconds for every 18 charecters sent. Heres my code so far.

Recommended Answers

All 10 Replies

Well you could state your OS and compiler.

There is no standard way to achieve sub-second delays.

Well you could state your OS and compiler.

There is no standard way to achieve sub-second delays.

Im using windows xp home and visual C++

hint: look for function Sleep() which uses milliseconds. And 100 is about as accurate as it can get, depending on what other processes are running on the computer.

yea ive seen the sleep function but it doesnt work with microseconds only miliseconds

MS-Windows is not a real-time operating system, so you can't get any better accuracy then a couple hundred milliseconds. If you want microseconds, then you have to use a different real-time os. One reason is that it takes the os thread scheduler a few milliseconds just to switch threads and do other os maintenance. Then of course other programs may not play nice and hog more cpu time than they probably should, and there are some operations that cannot be interrupted, such as disk i/o -- you have no control over any of those events.

I saw an add-on a few years ago that would emulate real-time, but it cost quite a few $$$.

> what i want to do is create a time delay of 100 micro seconds for every 18 charecters sent
Do this calculation:
Your port is set to 19200 baud, and framing is set to 8-N-1.
So each character has a start bit, 8 data bits, a stop bit and no parity (10 in all).
That gives you a line speed of 1920 characters per second.
At that speed, sending each character takes a little over 500 microseconds.

So a 100uS delay is basically the time taken to send a couple of bits of data. There's no way to guarantee that the OS won't insert much bigger delays due to scheduling of other tasks, and it certainly isn't enough time for the receiver to do much about it either.

Microscopic programmed delays are usually the wrong approach. There is simply too much variability in the average OS to make it anything like reliable.
You should be looking to use some other method of flow control, say xon-xoff.

Perhaps you need to explain what a delay of 100uS is supposed to get you, then perhaps we could suggest a better answer.

> what i want to do is create a time delay of 100 micro seconds for every 18 charecters sent
Do this calculation:
Your port is set to 19200 baud, and framing is set to 8-N-1.
So each character has a start bit, 8 data bits, a stop bit and no parity (10 in all).
That gives you a line speed of 1920 characters per second.
At that speed, sending each character takes a little over 500 microseconds.

So a 100uS delay is basically the time taken to send a couple of bits of data. There's no way to guarantee that the OS won't insert much bigger delays due to scheduling of other tasks, and it certainly isn't enough time for the receiver to do much about it either.

Microscopic programmed delays are usually the wrong approach. There is simply too much variability in the average OS to make it anything like reliable.
You should be looking to use some other method of flow control, say xon-xoff.

Perhaps you need to explain what a delay of 100uS is supposed to get you, then perhaps we could suggest a better answer.

I found this via google because it was EXACTLY what I need in my program - a delay of 100uS. The idea was to write a program to simulate the output of a microcontroller, which sends data byes in 100uS intervals. But based off the replies regarding OS timing, I don't think this will be possible anymore...

you won't be able to simulate it on either *nix, MS-Windows or MAC. But you might be able to do it in MS-DOS 6.X and earlier because it doesn't have the multi-process activitiy that the other os have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.