Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serial input displays chinese character

Status
Not open for further replies.

itsmetomc

Programmer
Dec 30, 2012
3
hello every one
I used to visit the tek tips forum regularly but got distracted for a while.
I have since picked up were i left off. I very quickly discovered that my
builder software would not work on windows 7 so I went to the builder site to
look for a compatible upgrade. I have the trial version of emabarcadero cuilder
and discovered that my older projects wont compile due to the unicode conversion.
I have a serial comunication project that i would like to work on but have run into
a converion problem. it would seem that my serial port is reciving ascii and i am
displaying chinese characters when i add the recieved input to a TMemo.

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
wchar_t Buff [100] = {0};

if (hCom){
Sleep (10);
ReadBuffer (Buff, hCom);

if (StrLen (Buff)) {
Memo1->Lines->Add(Buff);
}
}
}

this is what is being dispalyed in the memo
畐獬⁥‽""
栍汥潬
畐獬⁥‽"ഢ敨汬o

I am sure there is a simple conversion technique but i cant seem to ask the right
question in the search utility. you guys helped a lot in the past and it looks as if
will be visiting a lot in the future..

thanks in advance for your help
 
My first thought is that the two serial ports are not correctly synched. I would make certain that both the sending port and the receiving port are using the same speed, number of bits, number of stop bits, and parity, e.g., 9600, 8, 1, none.



James P. Cottingham
I'm number 1,229!
I'm number 1,229!
 
I haven't used Builder (or C++) for several years now, so the following may be totally wrong:
[ul]
[li]ReadBuffer may be expecting a Byte array[/li]
[li]Memo1->Lines->Add(Buff) is expecting Buff to use wide characters (as per the array definition).[/li]
[/ul]

To confirm (or not) this guess, for a given serial port input, I'd:
[ul]
[li]Display the value of the input bytes in hexadecimal.[/li]
[li]Display the contents of the Memo line in hexadecimal.[/li]
[li]Compare the two sets of hexadecimal values.[/li]
[/ul]
 
The supplied 'Chinese' characters, viewed in hexadecimal, appear to be:

50756C7365203D20220022000D000A000D68656C6C6F0D000A0050756C7365203D202200220D68656C6C6F00

Treating this a a byte array, this is something like:

Pulse = "."
hello
Pulse = "."
hello


where:
[ul]
[li]The quoted . values are both <NUL> (i.e. hex(00))[/li]
[li]The lines are separated by various combinations of double-byte representations of Carriage Return (0x0D) and Line Feed (0x0A) characters.[/li]
[/ul]

This would seem to confirm that the input is several lines of Byte arrays, but the buffer is then being interpreted/displayed (in the Memo) as lines of wide (16-bit) characters.
 
this is my recieving function

int ReadBuffer (wchar_t *str, HANDLE hCom)
{
int ReceiveInterval=1;
unsigned long AvailableBytes;
unsigned long ReadBytes = 0;

Sleep(ReceiveInterval);
AvailableBytes=GetInQue (hCom);
if(AvailableBytes > 0)
{
if (ReadFile(hCom, str, AvailableBytes, &ReadBytes, &oOverlaped) == true)
return ReadBytes;
else
return -1;
}
else
{
return 0;
}
}

when i write buff to a text file using FileWrite () the text is correct
when I use fputws () i get junk. this unicode thing is throwing me for a loop.
I'm communicating with a parallax basic stamp pic controller and the serial port is correct
as dans dad seems to confirm this by interpeting my test string correctly.
I have been a few years away and builder seems to have "fixed" their string
handling. how do i get Tmemo to display the byte array istead of the unicode array.
I'm looking into some kind of "UNICODETOASCII ()" function. or vice versa.
I will ultimately have to handle the inputs and manipulating the data from the basic stamp.
ie math and so forth. I will be recieving numerical data in a combined string, such as
"24,32,1006,1675,1,1,0,0" so I will need to be able to parse this string.

 
changed the functions

ReadBuffer (wchar_t *str, HANDLE hCom)
to
ReadBuffer (char *str, HANDLE hCom)

earlier I was having problems and i changed everything to wchar in an effort to
solve the problems. all the documentation I read was telling me I had to change
all my code to be unicode compliant. the responses I recieved confirmed my suspicions.
the code is reading the serial port properly now. as usual it turned out to be a simple
fix.

thanks for your support
 
I think that's more or less what I said originally ("ReadBuffer may be expecting a Byte array").

In general you should try to use wide characters (Unicode) by default (as many functions probably now expect that) and only use single-byte encodings (ASCII, etc.) where absolutely necessary, and do the translation at the interface.

I no longer use Builder (I'm retired and can't afford it), so I don't know how Builder C++ now defines the various types.
I instead use the (free) Visual Studio Express editions with C#, where all Char and String types are 16-bit; Byte type is used for 8-bit values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top