Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > General Discussion
  New Posts New Posts RSS Feed - How to send UNICODE characters to serial port
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

How to send UNICODE characters to serial port

 Post Reply Post Reply
Author
Message
VIKASS SAHARAN View Drop Down
Newbie
Newbie
Avatar

Joined: 27 June 2013
Location: CHANDIGARH
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote VIKASS SAHARAN Quote  Post ReplyReply Direct Link To This Post Topic: How to send UNICODE characters to serial port
    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
Back to Top
sharky View Drop Down
Newbie
Newbie


Joined: 07 January 2009
Location: Czech Republic
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote sharky Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
mgampi View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14 July 2003
Status: Offline
Points: 1201
Post Options Post Options   Thanks (0) Thanks(0)   Quote mgampi Quote  Post ReplyReply Direct Link To This Post 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 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022
Back to Top
VIKASS SAHARAN View Drop Down
Newbie
Newbie
Avatar

Joined: 27 June 2013
Location: CHANDIGARH
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote VIKASS SAHARAN Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
sharky View Drop Down
Newbie
Newbie


Joined: 07 January 2009
Location: Czech Republic
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote sharky Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
VIKASS SAHARAN View Drop Down
Newbie
Newbie
Avatar

Joined: 27 June 2013
Location: CHANDIGARH
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote VIKASS SAHARAN Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.156 seconds.