Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have developed a application for sending a data through COM port(UART Communication), So for entering the data I have made one edit box ,It will get the text when the buttons are pressed.

So inside there will be timer which will be updating for every 25ms , so my application page also refreshing for every 25ms due to this I can't able to delete the characters in the edit box only when the COM port is open..., Without opening the COM port, If I run my application It is working fine and deleting characters.

The following is the PumpMessage code which is working fine for this issue in Visual Studio V6.0
But in VS 2005 , its not working.

C#
void CUARTDlg::PumpMessages()
{
    ASSERT(m_hWnd != NULL);

    MSG msg;

    while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
        if(!IsDialogMessage(&msg))
        {
            TranslateMessage(&msg);

            DispatchMessage(&msg);
        }
    }
}



COM port.........

void CKB600LRUpreDlg::ClickDasSmartbutton64()
{
	/****************** DCB Settings *****************/
	DCB dcb;
	dcb.BaudRate = 115200;
	dcb.ByteSize = 8;
	dcb.StopBits = 1;
	dcb.Parity = 0;
	dcb.fParity         = 0;
	dcb.fOutxCtsFlow    = 0;
	dcb.fOutxDsrFlow    = 0;
	dcb.fDsrSensitivity = 0;
	dcb.fTXContinueOnXoff = FALSE;
	dcb.fOutX           = 0;
	dcb.fInX            = 0;
	dcb.fNull           = 0;
	dcb.fErrorChar      = 0;
	dcb.fAbortOnError   = 0;
	dcb.fRtsControl     = RTS_CONTROL_DISABLE;
	dcb.fDtrControl     = DTR_CONTROL_DISABLE;
	/****************** DCB Settings *****************/
	openPort(dcb,_T("COM2"));	//calling open port function.
	AfxMessageBox(_T("Port Opened Successfully "));
	tmr = SetTimer(ID_CLOCK_TIMER,20,NULL);
}
/************************************ End of CONNECT BUTTON ****************************/

/************************* OPEN PORT FUNCTION **********************/
BOOL CKB600LRUpreDlg::openPort(DCB dcb, CString portName)
{	
	hPort = CreateFile(_T("\\\\.\\COM2")/*portName*/,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
	if (GetCommState(hPort,&dcb) == 0)
	{
		AfxMessageBox(_T("Get configuration port has problem."));
		return FALSE;
	}
	if (SetCommState(hPort,&dcb) == 0)
	{
		AfxMessageBox(_T("Set configuration port has problem."));
		return FALSE;
	}

	/********Clear the port*****************/
	FlushFileBuffers( hPort );
	PurgeComm (hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
	COMSTAT     comStat;
	DWORD       dwErrorFlags;
	ClearCommError ( hPort, &dwErrorFlags, &comStat );
	/********Clear the port*****************/
	return FALSE;
}




For Clear ......

C#
void CKB600LRUpreDlg::ClickDasSmartbutton7()
{
    // TODO: Add your message handler code here


if(Shift == 0)
{


    int usCurPosS=0;

    int pos = 0;
    int istart = 0;
    int iend = 0;
    int &start = istart;
    int &end = iend;
    UpdateData(true);
    m_edit.GetSel(istart,iend);

    m_edit.GetSel(usCurPos,usCurPosS);
    if(istart == iend)
    {
        if (usCurPos==0)
        {

            AfxMessageBox(_T("No Text Has Selected , Please Select The Text "));
            m_edit.SetFocus();
            return;

        }
        UpdateData(true);
        m_edit.SetFocus();

        m_edit.GetSel(usCurPos,usCurPosS);

        m_edit.SetSel(usCurPos,usCurPos-1);

        UpdateData(false);

    }

    else
    {
        m_edit.SetSel(istart,iend);

    }



    UpdateData(false);


    m_edit.Clear();

    m_edit.SetFocus();


}

else
{
    int istart = 0;
    int iend = 0;
    int &start = istart;
    int &end = iend;
    UpdateData(true);
    m_edit.GetSel(istart,iend);
    m_edit.SetSel(0,iend);
    UpdateData(false);
    m_edit.Clear();
    m_edit.SetFocus();
    Shift=0;

}


}


For Key (Button) A

C#
void CKB600LRUpreDlg::ClickDasSmartbutton2()
{
    // TODO: Add your message handler code here
    variable = 'B';
    function();
}



C#
void CKB600LRUpreDlg::function()
{
    int length;
    m_edit.GetWindowText(m_TxtSamplingFrequency);
    // Check position; append if beyond the end
    if (usCurPos > m_edit.GetSel())
        usCurPos = m_edit.GetSel();
    // Insert character
    m_TxtSamplingFrequency.Insert(usCurPos, variable);
    // Set text
    m_edit.SetWindowText(m_TxtSamplingFrequency);
    // Set selection behind inserted char
    usCurPos++;
    m_edit.SetSel(usCurPos,usCurPos);
    length=m_TxtSamplingFrequency.GetLength();
    if(length>32)
    {
        AfxMessageBox(_T("Maximum Text Limit Reached "));

        m_edit.SetFocus();
        return;
    }


    m_edit.SetFocus();

}
Posted
Updated 1-Jan-14 21:23pm
v2
Comments
AlwaysLearningNewStuff 31-Dec-13 3:55am    
I see an error in your call to IsDialogMessage-it should be something like this:

IsDialogMessage( hwndDialogHandle, &msg ) according to MSDN documentation.

As for the problems you are facing-can you be more specific please?
So far you have provided insufficient data to get proper help, in my opinion.

Hopefully this helps.

Best regards and happy New Year.
ZurdoDev 31-Dec-13 12:05pm    
What's your question?
Baakki 2-Jan-14 3:26am    
before connecting to COM port I can able to delete the characters on edit box.., but after connecting to COM port.., It is not getting delete , cursor is going to 0th position.....Pls refer obove.., I hav updated the question.
AlwaysLearningNewStuff 2-Jan-14 5:57am    
Unfortunately I am unable to help, since I thought that problem was in IsDialogMessage, since you are missing the parameter in your call. I haven't worked with UART in C++, but I can offer you the following advice, which could help you to get answers you seek:

Update your question with all the relevant information that can help the community to reproduce the problem. So far you have said that you use C++ and Visual Studio.

Try to strip as much of the code as you can and only then add it again, but piece by piece-stop re-adding as soon as you bump into the same problem.

Wish I could have helped more :(

Best regards, I hope you will succeed.

1 solution

this concept is broken by design: polling every 25 ms and running the message queue is more a dead lock than bad design (Sorry: crap)

The COM operations should run in a own thread WITHOUT GUI interaction. If you have new data communicate with the GUI via user message and the function

PostThreadMessage http://msdn.microsoft.com/en-us/library/windows/desktop/ms644946(v=vs.85).aspx[^]

to update the GUI.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900