Re: Still trying
SEE BELOW:
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:i1npr3he7a9mhm139bmvng81odi99s0uop@4ax.com...
On Wed, 20 Feb 2008 15:45:55 GMT, "Kahlua" <kahlua@right.here> wrote:
It looks liek I am almost there.
The program builds with no errors.
When I execute it the following happens:
Before doing anything I opened the Task Manager and I see Martin1.exe
CPU=0
When I send a char into the serial port the CPU=50 and nothing happens.
Any char, even the 0x31 causes cpu=50 and nothing happens on the dialog.
The MessageBox doesnt appear (for testing purposes ONLY).
I can still click buttons on the dialog that do work so it's not hung.
I know for sure I am doing something wrong.
Thanks for the help so far.
Below I think are the pertinent parts of my app.
==============================================================
In Martin1Dlg.h I added near the top:
#define MY_WM_MESSAGE1 (WM_APP + 1)
In "class CMartin1Dlg : public CDialog" I added just above protected:
afx_msg LRESULT OnMyMessage1(UINT wParam, LONG lParam);
==============================================================
Near the top of Martin1Dlg.cpp I have:
UINT WorkerThreadProc( LPVOID Param );
==============================================================
In the message map I added the line before "END_MESSAGE_MAP()"
BEGIN_MESSAGE_MAP(CMartin1Dlg, CDialog)
//{{AFX_MSG_MAP(CMartin1Dlg)
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
ON_MESSAGE(MY_WM_MESSAGE1, OnMyMessage1)
END_MESSAGE_MAP()
==============================================================
In OnInitDialog() before the "return TRUE" I added:
AfxBeginThread(WorkerThreadProc,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL);
==============================================================
Routine to handle the message:
LRESULT CMartin1Dlg::OnMyMessage1(UINT wParam, LONG lParam)
{
AfxMessageBox("Got here");
return 0;
}
==============================================================
The worker thread:
UINT WorkerThreadProc( LPVOID Param ) //Sample function for using in
AfxBeginThread
{
int DataByte=0x00;
int Count=0;
loop:
****
Replace the previous line with
while(TRUE)
{ /* read loop */
****
while(Count == 0){
Count = Port.BytesInReceiveBuffer();
Sleep(100);
}
DataByte=Port.GetByte(18);
if (DataByte == 0x31){
::PostMessage(HWND_BROADCAST, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
****
HWND_BROADCAST is completely wrong here. This sends the message to EVERY
top-level window
in the system, which increases system load tremendously, and introduces
the possibility
that if any one of these apps is expecting its own private WM_APP+1
message, you will
probably crash it.
You are still polling instead of blocking, introducing unnecessary delays
in your
processing.
What is the meaning of 18 in GetByte? Why is it you are only reading one
byte when you
want to read 34 bytes. What happens when the next byte overwrites
DataByte? How will the
receiving thread know what data byte it has received?
Did you read the example code I posted?
joe
****
}
goto loop;
****
Replace the above line with
} /* read loop */
goto should be used in very rare and exotic circumstances, which do not
occur here.
Also, you are still polling.
****
return TRUE;
}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
=======================================================================
Hi Joe,
In the statement DataByte=Port.GetByte(18); the 18 is a timeout in 1/18th
seconds.
I just use 1 second as a max.
The reason I get only 33 bytes is that the first byte tells me what the next
group is for.
EX: if 1st byte is 0xfe I get 33 more for bargraph, if 1st byte is 0xfd I
get 12 bytes for another purpose.
This is the actual worker thread I am using so far:
UINT WorkerThreadProc( LPVOID Param ) //function for reading data from
serial port
{
HWND hDlg = (HWND)Param;
int DataByte=0x00;
int Count=0;
int i;
while(TRUE){
Count=0;
while(Count == 0){
Count = Port.BytesInReceiveBuffer();
Sleep(50);
}
DataByte=Port.GetByte(18); //the 18 is just a max timeout
if (DataByte == 0xfe){
for (i=0; i<33; i++){
DataByte=Port.GetByte(18);
Bar[i]=(DataByte & 0xff);
}
}
::PostMessage(hDlg, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
}
return TRUE;
}
I am still using COMDrv++ because I dont wish to change in mid-stream.
I will look further into readfile for serial access.
I really appreciate all the help I get in this newsgroup.
Thank you all,
Ed