How to send UNICODE characters to serial port |
Post Reply |
Author | |
VIKASS SAHARAN
Newbie Joined: 27 June 2013 Location: CHANDIGARH Status: Offline Points: 4 |
Post Options
Thanks(0)
Posted: 17 July 2013 at 7:14am |
I am trying to write data in Russian language to the serial (RS-232) port. My display device is already set to that character code page.
But output on the device is not exactly what I require. My code snippet is like this below CString pBuffer = L"английский"; //Russian Language LPBYTE pByte = new BYTE[pBuffer.GetLength() + 1]; memcpy(pByte, (VOID*)LPCTSTR(pBuffer), pBuffer.GetLength()); long nBuffer=pBuffer.GetLength()+1; DWORD dwWritten=0; WriteFile(pHandle , pByte, nBuffer ,&dwWritten , NULL); pHandle is a valid handle. Waiting for valuable response of yours. Welcome in case of any further information. |
|
VIKAS SAHARAN
|
|
sharky
Newbie Joined: 07 January 2009 Location: Czech Republic Status: Offline Points: 9 |
Post Options
Thanks(0)
|
I do not know do you compile your app as unicode or not but right code must be: LPBYTE pByte = new BYTE[(pBuffer.GetLength() + 1)*sizeof(TCHAR)] memcpy(pByte, (VOID*)LPCTSTR(pBuffer), (pBuffer.GetLength() + 1)* sizeof(TCHAR))); and pBuffer content strongly depends on whether your application is Unicode or not.
|
|
mgampi
Senior Member Joined: 14 July 2003 Status: Offline Points: 1201 |
Post Options
Thanks(0)
|
Hi;
UNICODE stores two bytes for each character. So what you send is half of the string. pBuffer.GetLength() returns the length of the string in characters not in bytes! Use the debugger and check the content of your byte buffer and you'll see it. |
|
Martin Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0 Platform: Windows 10 v 22H2 (64bit) Language: VC++ 2022 |
|
VIKASS SAHARAN
Newbie Joined: 27 June 2013 Location: CHANDIGARH Status: Offline Points: 4 |
Post Options
Thanks(0)
|
Dear Martin
According to your answer, i tried to store two bytes for each character. My program snippet is like this: CString pBuffer = L"английский"; //Russian Language int nbytes = (pBuffer.GetLength()+1) * sizeof(TCHAR); LPBYTE pByte = new BYTE[nbytes]; memcpy(pByte, (VOID*)LPCTSTR(pBuffer), nbytes-1); long nBuffer=nbytes; DWORD dwWritten=0; returnval=WriteFile(pHandle , pByte, nBuffer ,&dwWritten , NULL); But I am still facing same problem. If till now I am wrong, please guide me in appropriate way.
|
|
VIKAS SAHARAN
|
|
sharky
Newbie Joined: 07 January 2009 Location: Czech Republic Status: Offline Points: 9 |
Post Options
Thanks(0)
|
Did your program compiled as UNICODE?
1. Code is not correct if you compile as Multibyte and default codepage is not 1251 because string will be converted from unicode to multibyte with wrong codepage. CString pBuffer = L"английский"; //Russian Language 2. Why you allocate space for NULL character if you not use it? // alloc bytes array for string length and NULL character (NULL has 2 bytes size in unicode build!!) int nbytes = (pBuffer.GetLength()+1) * sizeof(TCHAR); // (copy and truncate NULL from 2 bytes to 1, pByte now contain garbage at the end) memcpy(pByte, (VOID*)LPCTSTR(pBuffer), nbytes-1); // and write bytes array with garbage to file? WriteFile(pHandle , pByte, nBuffer ,&dwWritten , NULL) Your code can be written in short and correct form for Unicode and Multibyte builds: CString pBuffer = _T("английский"); //Russian Language int nBuffer = (pBuffer.GetLength()+1) * sizeof(TCHAR); DWORD dwWritten=0; returnval=WriteFile(pHandle , (void*)pBuffer.GetString(), nBuffer ,&dwWritten , NULL); 3. You send Unicode string to RS-232 port. The other side receive unicode string correctly?
|
|
VIKASS SAHARAN
Newbie Joined: 27 June 2013 Location: CHANDIGARH Status: Offline Points: 4 |
Post Options
Thanks(0)
|
Thanks to all the experts for your guidance. With your guidance I am progressing slowly but not as expected... :-)
Now, the problem is that when I pass these Russian Characters from edit control. It is again mis-behaving as faced during starting of this discussion. But the same is working fine if I pass the data statically (hard code). Code snippet used by me is like this one... CString pBuffer; m_text.GetWindowText(pBuffer); \\m_text is edit control variable. int nBuffer = (pBuffer.GetLength()+1) * sizeof(TCHAR); DWORD dwWritten=0; returnval=WriteFile(pHandle , (void*)pBuffer.GetString(), nBuffer ,&dwWritten , NULL); Thanks in advance !! |
|
VIKAS SAHARAN
|
|
Post Reply | |
Tweet
|
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |