Print Page | Close Window

How to send UNICODE characters to serial port

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: General Discussion
Forum Description: Topics Related to Visual C++ MFC Development in General
URL: http://forum.codejock.com/forum_posts.asp?TID=21800
Printed Date: 03 May 2024 at 10:15am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: How to send UNICODE characters to serial port
Posted By: VIKASS SAHARAN
Subject: How to send UNICODE characters to serial port
Date 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



Replies:
Posted By: sharky
Date Posted: 17 July 2013 at 4:11pm
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.


Posted By: mgampi
Date Posted: 18 July 2013 at 3:05am
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 19.0.0, new Projects v 19.1.0
Platform: Windows 10 v 1909 (64bit)
Language: VC++ 2017


Posted By: VIKASS SAHARAN
Date Posted: 19 July 2013 at 1:21am
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


Posted By: sharky
Date Posted: 21 July 2013 at 8:07am
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?


Posted By: VIKASS SAHARAN
Date Posted: 23 July 2013 at 8:32am
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



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net