|
Add the ampersand [&] symbol before the desired letter in the button caption. For e.g for &Click, if you press Alt + C will run the code for Click button.
|
|
|
|
|
|
Hello,
I write the command to my device, the device responses immediately...
----
the code:
int begin;
While(1)
{
begin=GetTickCount();
WriteFile(...);
ReadFile(...);
TRACE(_T("The Tx-Rx Time is %d millisecond \r\n"), GetTickCount()-begin);
}
----
the output:
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 15 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 16 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 15 millisecond
------
It's good when spend 0 millisecond one loop...
But, as you can see above, every 3rd or 4th loop, the time spend is about 15 MS, it should be 0 MS, the ReadFile can't not get buffer data immediately...
Anyone met this problem before??
I need no delay in the loop...
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
The Tx-Rx Time is 0 millisecond
|
|
|
|
|
That's simply because windows is not a real-time operating system, so, it is possible that inside your loop another thread becomes active (which means that your thread won't be processed for a while, ending up with the delays).
The only way to "fix the problem" (which is not a problem in fact) is to use a real-time operating system (e.g. windows CE).
|
|
|
|
|
Dear Cédric Moonen:
It seems you are right, but the delay is in ReadFile (I tested it)... to wait buffer coming. If you are right, the delay will be in WriteFile or ReadFile. The context switch doesn't spend 15 MS (too long).
Thanks for you reply
|
|
|
|
|
|
Dear Cédric Moonen:
Good job. You are right. I write a test program in window XP, it actually spends about 15 milliseconds when context-switch occurs.
Thank you very much!
------
UINT beginTicks,testData=0,MsTimePerLoop;
while(1)
{
beginTicks=GetTickCount();
for(int i=0;i<10000;i++)
testData= (testData+100)*20-300*20+20;
MsTimePerLoop=GetTickCount()-beginTicks;
if(MsTimePerLoop>=5)
{
printf("MsTimePerLoop=%d\n",MsTimePerLoop);
Sleep(1000);
}
else
printf("MsTimePerLoop=%d\r\n",MsTimePerLoop);
}
|
|
|
|
|
yctsai wrote: The context switch doesn't spend 15 MS (too long).
Maybe not, but the context switch PLUS the time slice given to a different process will easily take 15ms. Remember, you're not the only code running on your PC.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
It may be that your thread is preempted as Cédric suggested.
But, it may also be that your call to ::ReadFile() doesn't return immediately due to how you have set up your port and whether you're using overlapped I/O or not.
So; I have two questions:
- What values have you assigned the members of the
COMMTIMEOUTS struct when calling ::SetCommTimeouts() ?
This is essential since your call may block until it receives something on the port. - Are you using overlapped I/O?
My point is that the delay you're experiencing may be caused by the device connected to your port as well as any context switches.
I also suggest you have a look at this article[^] about serial communication.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
While you can do nothing about the 15ms delay, see if this article is of any help with serial ports in general.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello everyone,
i am trying to compile my program where i am using the functions like sqrt pow and fabs. I do have math.h included but for some reason i get errors like:
error C2668: 'fabs' : ambiguous call to overloaded function
same for the rest of the functions
i have this included:
#include "stdafx.h"
#include "math.h"
i tried including <cmath> but still same errors.
Does anyone know why they are not being recognized?
my file is .cpp not .c but it is an MFC project.
thanks
|
|
|
|
|
It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (multiply what you pass in by 1.0), this should fix the problem.
int result = (int)fabs(value*1.0);
|
|
|
|
|
Cedric Moonen wrote: int result = (int)fabs(value*1.0);
Better could be
int result = (int)fabs((double)value);
Because it avoids a multiplication. Am I right?
- ns ami -
|
|
|
|
|
Yes you're right. It's just because I am used to multiply by 1.0 because in general I use that in calculation (where I don't want to put a cast inside because it looks ugly)
|
|
|
|
|
ns ami wrote: Am I right?
Only if the code were in a huge loop would the difference in time even be noticeable.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.
|
|
|
|
|
nevertheless it's a bad style as many people won't understand it.
Don't try it, just do it!
|
|
|
|
|
the compiler will optimize that multiplication away.
int value = rand();
int result1 = (int)fabs(value*1.0);
printf("%d, %d\n", result1, result1);
VC6, release mode gives the following assembly:
; 10 : int value = rand();
call _rand
; 11 : int result1 = (int)fabs(value*1.0);
mov DWORD PTR -4+[esp+4], eax
fild DWORD PTR -4+[esp+4]
fabs
call __ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax
push eax
push OFFSET FLAT:??_C@_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; `string'
call _printf
no fmul.
if you do this:
int value = rand();
int result1 = (int)fabs((double)value);
printf("%d, %d\n", result1, result1);
you get this:
; 10 : int value = rand();
call _rand
; 11 : int result1 = (int)fabs((double)value);
mov DWORD PTR -4+[esp+4], eax
fild DWORD PTR -4+[esp+4]
fabs
call __ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax
push eax
push OFFSET FLAT:??_C@_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; `string'
call _printf
exactly the same.
|
|
|
|
|
Chris Losinger wrote: the compiler will optimize that multiplication away.
Thank you for pointing out that...
- ns ami -
|
|
|
|
|
i need alternative code for CMap is it possible to implement my own CMap wrapper Class using the STL::map
Hi this Ravinder
|
|
|
|
|
I don't really understand your question but a std::map is an alternative for the MFC CMap (in fact, I much prefer the STL containers than the MFC containers).
|
|
|
|
|
... in addition, perhaps the biggest difference is that CMap uses hashing for its keys, and std::map uses key comparisons to store them in some sorted order.
--
Arman
|
|
|
|
|
Need the wrapper class for CMap using the STL map(Because i have some problems using CMap i want the same functionality of CMap but internally i have to use map)
Hi this Ravinder
|
|
|
|
|
So you need the interface of CMap and the implementation of std::map..? If so, then you need to declare your own CMyMap with an std::map member variable declared in it, and try to implement CMap similar functions in your CMyMap. However, you cannot simulate hash code related functions of CMap via a std::map..
Did I understand your problem correctly?
--
Arman
|
|
|
|
|
Hi All,
How to get the available ports for serial port communcation.
Thanks & Regards,
Anitha
|
|
|
|