Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my MFC dialogue based application, I want to Read data through serial port and whatever data received from that,displayed into Edit control,but somehow it is not working,as I used below methodology:
C#
/*CString msg;
    msg.Format(L"%c",Buff);*/
    //strcpy( (char*)Buff,ThreadStruct->m_handleDlg->m_console);//"Hello World");
    while(1)
    {
        ReadFile(DeviceHandle,&Buff,1,&WriteBytes,NULL);
        //ThreadStruct->m_handleDlg->m_OutConsole.ReplaceSel(L"\r\n\r\n\r"+msg+L"\r\n\r\n\r");
        //ThreadStruct->m_handleDlg->m_OutConsole.SetWindowTextW(Buff);
        //ThreadStruct->m_handleDlg->m_console.Format(L"%uc",Buff);
    }

for above code I used worker thread.So that used ThreadStruct.
above no one is running,gives error! How to do that?
Posted
Updated 18-Jun-13 4:40am
v2
Comments
Nelek 18-Jun-13 10:42am    
Which error? Where is he complaining? It may happen, that the problem is somewhere totally different. Can you use the "improve question" widget and elaborate a bit more?

1 solution

You are reading a single character (byte) into Buff. So you can't pass Buff where a NULL terminated string parameter is expected without appending the NULL terminator. Your code snippet indicates also that you are using a Unicode build. So the characters read from the serial port must be converted to Unicode. Using a CString, this can be done this way (when the received characters are plain ASCII or from the ANSI code page of your system):
C++
CString msg;
// Format single ASCII/ANSI char
msg.Format(L"%hc", Buff[0]);
// Format ASCII/ANSI string after appending a NULL char
Buff[1] = 0;
msg.Format(L"%hs", Buff);

The h prefix tells the format function that the parameter is an ASCII/ANSI char or string and performs the conversion to Unicode.
 
Share this answer
 
Comments
Venkat Raghvan 25-Jun-13 9:45am    
it not gives error,but how to insert that Buff value to edit control?
If I used SetWindowText(),it gives run time error?
Jochen Arndt 25-Jun-13 11:54am    
If you have created the CString like in my example, use
EditCtrl.SetWindowText(msg.GetString());

Your problem is not passing unsigned char[*] to char[*) (you can just pass such as is using casting if necessary), but passing char[*] to WCHAR[*]. Your received data are unsigned char, but SetWindowText() requires a LPCWSTR (const WCHAR *) with Unicode builds. So the data must be converted from char to WCHAR.

Regarding the unsigned:
You may declare your Buff variable as char rather than unsigned char. There is no need to use unsigned char in the code shown by you.
Venkat Raghvan 26-Jun-13 2:50am    
I do whatever you said,but it does not run safely,it breaks at run time.
I tried with typecasting also,but it failed at run time.
Jochen Arndt 26-Jun-13 3:32am    
Sorry, I missed one thing when writing the above comment:
You are trying to update the text of a control which belongs to another thread. Then you must send a WM_SETTEXT message rather than using SetWindowText().

If you need more complex operations like inserting text, you must use some kind of signaling. One option is using user defined messages:
- The worker thread sends a user defined message when new data are available.
- The main thread handles this message and performs the operation on the edit control.

When passing a single char, this can be done using the LPARAM or WPARAM parameter of SendMessage(). When passing a string, this can be done passing a pointer to dynamically allocated memory which must be freed by the recipient of the message or storing the string in a member variable of the worker thread and providing a function that copies the data from the member variable to a destination buffer (which requires some kind of locking).

This link may be helpful: http://www.codeproject.com/Articles/552/Using-Worker-Threads (see the 'Worker threads and the GUI II: Don't touch the GUI' section).
Venkat Raghvan 26-Jun-13 6:07am    
ok,but how to pass WM_SETTEXT message to control? I declared in message map section;it gives error!

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



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