|
A simple test - put an AfxMessageBox() in the OnTimer() funtion, set the timer period to say 3 seconds, let the message box stay open for say 2 seconds.
Elaine
The tigress is here
|
|
|
|
|
ONTIMER: (this all most application need)
Since the the data received (using Windows serial communication) is received/sent in the background, you can use the SetTimer() function and just check if any new data has arrived in your OnTimer() event function. How often you need to check depends on the size of the receive buffer you specified when you setup communication (See SetupComm()). To detemine if any data has been received just call ClearCommError().
MULTITHREADING:
Of course you could use a seperate thread that uses shared data buffers, protected by critical sections so your threads will not step on each others toes. What I mean by shared buffers is one or more buffers you and the Rx thread both share, when you're reading from a buffer, the RX thread cann't write to it and when the RX thread is writing to it, you cann't read from it. Then all you need to do is calculate approximatly how long it should take to receive X-number of characters and go to sleep for that period of time in your RX thread; when the thread wakes up it then reads the data into the buffer and goes back to sleep (that takes care of the CPU usage problem).
You could use two buffers so that the RX thread could be writing into one while you are reading the other one.
OR
you could use a ring buffer so that you can read from the tail end while the receiver is writing to the head end. (this can be a bit more tricky).
The RX thread can inform you that new data has arrived by posting (not sending) a message back to your main thread or by setting an event flag that you must check for every now and then, either in an OnTimer() event function or in the main message loop.
FINALY:
That should be enough to help solve your problems.
FYI:
There are some articles at CP (CSerial) and in the MSDN library that use multiple threading for serial communication.
GOOD LUCK!
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
thank you for the detailed response. In my original post I failed to mention that I am using shared buffers and as you mentioned it is much better then using one buffer.
I have been doing some debugging of the cpu hogging problem and it has come down to be either the infinite for loop that is causing it or the actual worker thread itself.
I call the worker thread like this:
AfxBeginThread(Read,this);
Read() is a static menber function declared as:
afx_msg static UINT Read(LPVOID param);
As I was told this was how it was supposed to be declared. Here is the Read() function minus the background work:
<br />
UINT CprogramDlg::Read(LPVOID param)<br />
{<br />
for( ;; ) {}<br />
return 0;<br />
}<br />
Reason I am not including the code within the "for" loop is because after testing I have concluded that even with just the code like this when the Read() function is called from AfxBeginThread() the cpu immediately shoots up to 99% and stays there.
So from this I have to conclude that it is either the "infinite for loop" that is causing it or it is the actual thread call that is doing it. It was pointed out to me that an infinite loop in a worker thread "should not" cause the cpu usage to jump up completely like that, but I dont know how true that is. I just want to make sure that it is the infinite loop before I move onto fix the problem by using a timer.
Thanks
|
|
|
|
|
The worker thread is causing the cpu usage problem, it must go to sleep part of the time if you want it to stop doing that. Even just using WaitCommEvent(...,EV_RXCHAR,...) inside your loop might reduce cpu usage (how much depends on baudrate). I would just use SetupComm(...,dwInQueue,dwOutQueue) and go to sleep for a short time, before reading the data from the rx-buffer.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Hi
I have a third party product which exposes an api (dll/type library).
What i want to do is create a dll using some of the functions/classes/interfaces provided by the api for e.g retireve some product related information and use the notification engine to send emails etc.
This newly created dll is then going to be consumed by an application.
I am new to vc++ and have no clue how to go about creating this dll. Can somebody tell me the steps to achieve this?
I need to meet a deadline and have no clue how to go about doing it.
Any help would deeply appreciated
Regards
|
|
|
|
|
What I want to do is take an input from the user to set up a grid. This grid is a multi- dimensional array. I can do this with a simple array (1-D) and create the object as new. But when I try this with a multi- dimensional I get the error "constant expression expected."
If only I had more time!
|
|
|
|
|
bitsNbites wrote:
But when I try this with a multi- dimensional I get the error "constant expression expected."
Without showing the code in error, it's impossible to offer any suggestion(s).
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
The code that has the error is not correct. I think I need to try a different method, but this is it.
In the header I have an int pointer. int* sizeArray;
Then in a function in the class.
int row=22, col=20;
sizeArray = new int[40]; // This is OK
sizeArray = new int[row]; // This is OK
sizeArray = new int[row][col];// This errors.
"non-constant expression as array bound"
and '=' : cannot convert from 'int (*)[1]' to 'int *'
row and col will be values taken as arguments but for testing I just assign a value.
If only I had more time!
|
|
|
|
|
See here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
int _tmain(int argc, _TCHAR* argv[])
{
int** sizeArray;
int row=22, col=20;
sizeArray = new int*[row];
for(int i=0; i<row; i++)
sizeArray[i] = new int[col];
for(int i = 0; i<row; i++)
for (int j=0; j<col; j++) {
sizeArray[i][j] = i*j;
}
for(int i = 0; i<row; i++)
for (int j=0; j<col; j++) {
printf("sizeArray[%d][%d] = %d\n",i, j, sizeArray[i][j]);
}
return 0;
}
http://www.priyank.in/
|
|
|
|
|
Great that helps a lot. I now am using:
int** q = new int*[rows];
for(int i = 0; i < rows; i++)
q[i] = new int [col];
This works.
MSDN states "Multidimensional arrays are not equivalent to arrays of pointers." It seems that here this code is creating just that. I suspect this is why I don't see the elements in the Autos window. I will play around with this for a while and will post new questions later.
Thanks for the help.
If only I had more time!
|
|
|
|
|
You can always create multi dimensional arrays by arrays of pointers pointing to another array. The difference I think is basically in memory locations, creating arrays by this method dynamically may not have continous memory locations, like that by creating arrays like int a[5][5];
http://www.priyank.in/
|
|
|
|
|
Hi ,
Is there any Static address in computers in a local network?
Like the MAC Address But I want to send Information to it.
Cheers,
Thanks.
|
|
|
|
|
dSolariuM wrote:
Is there any Static address in computers in a local network?
IP addresses are either statically or dynamically assigned.
dSolariuM wrote:
Like the MAC Address But I want to send Information to it.
Are you wanting to send information to an IP address or a MAC address?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I want to send information to MAC address.
Thanks.
|
|
|
|
|
Have a win32 console application, when I right click on it the version tab is missing.
How do I add this information?
ThankX in advanced
|
|
|
|
|
Just add a Version resource to it. Click on the ResourceView tab in the Workspace pane. Use Ctrl+R to insert a new resource. Select Version from the list.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Add a resource file (*.RC) to your project.
Add a version information resource to the resource file.
|
|
|
|
|
I add a rich edit control from controls tool box, when I run my project, it appear a message box "Failed to create empty document"
What is this error ?
Could you give me an example how to create a rich edit control ?
Thank you.
|
|
|
|
|
Have you called AfxInitRichEdit() ?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Why do we need to use this function ?
Thank you.
|
|
|
|
|
You might not need to, unless this is an MFC application. It was merely a suggestion. Do you know for certain that it is not needed?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Because it will enable/initialize the richedit support.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Hi everybody,
I have created an PopUp Menu in a CDialog...it works OK.
Now I want to create a PopUp Menu inside an ActiveX every time I click Button RIGHT of the mouse...
I have copied the code inside the function WM_RBUTTONDOWN...The problem is that it shows the Menu but it is DISABLED (it is shown but I can not select an option).
How can I do it to solve it...to make it enable?
Thanks
Javier
|
|
|
|
|
The menu items will be disabled until you write the code for the menu items. When the handlers are written, then the menu items will be enabled.
Artificial intelligence is no match for natural
stupidity.
Never argue with an idiot. They will drag you down to their level where they are an expert.
|
|
|
|