INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

serial input displays chinese character

serial input displays chinese character

(OP)
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

RE: serial input displays chinese character

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!

RE: serial input displays chinese character

I haven't used Builder (or C++) for several years now, so the following may be totally wrong:
  • ReadBuffer may be expecting a Byte array
  • Memo1->Lines->Add(Buff) is expecting Buff to use wide characters (as per the array definition).
To confirm (or not) this guess, for a given serial port input, I'd:
  • Display the value of the input bytes in hexadecimal.
  • Display the contents of the Memo line in hexadecimal.
  • Compare the two sets of hexadecimal values.

RE: serial input displays chinese character

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:
  • The quoted . values are both <NUL> (i.e. hex(00))
  • The lines are separated by various combinations of double-byte representations of Carriage Return (0x0D) and Line Feed (0x0A) characters.
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.

RE: serial input displays chinese character

(OP)
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.

RE: serial input displays chinese character

(OP)
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

RE: serial input displays chinese character

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close