Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using serial port communication in my project, wherein I am sending a 4k of data using serial port.

The last but 2 bytes are the sum of all bytes I send to the antenna, which is more than (greater than) 7F.
The value is not recognisible by the ASCII code, so I want to implement the transfer using UTF8 encoding.
I need the code for converting hex to UTF8 and vice versa.

Please Help me out
Posted
Updated 10-Nov-10 21:53pm
v2
Comments
David Ewen 11-Nov-10 3:24am    
hex is a subset of utf8 its the chars 0-9 and A-F so it isnt exactly clear what you want. A small code sample might help.
Dalek Dave 11-Nov-10 3:53am    
Edited for Grammar, Spelling, Syntax and Readability.

Why not just use 8 bit ASCII? If you are having problems transferring values greater that 0x7F via the serial port, then check you are using 8 bit data, rather than 7. If you are not, then changing encoding from ASCII to UTF8 will not help in the least. What is the antenna expecting?
 
Share this answer
 
Comments
Dalek Dave 11-Nov-10 3:53am    
Good Call Mr Griff.
Rajesh Anuhya 15-Nov-10 1:08am    
Good Answer
Thank u for the reply but the problem is i will send the data as

Ex: 3A 54 31 32 33 ..... 9B 82 0D 0A

here the underlined data is nothing but the sum of all the bytes iam sending to the antenna (excluding 3A, 0D, 0A: those are start and end of communication)
the last but 2 bytes are the checksum by which the authentication is done, so it must be sent as it is to the antenna.
I don't know about 8 bit of ASCII just let me know about it.
SerialPort.Write(hex2ascii(3A5431323334353637383942545695965835679BA50D0A));
Thread.sleep(500);
string resp = SerialPort.ReadExisting;
response = resp.Length;
string token = resp.substring(14,1);
if(token == 04)
{
.....
} 

the resp is the response from the antenna here it gives the data and i have to split and find the checksum and validate the data and start transfer of the data after the response from the antenna is correct
 
Share this answer
 
Continue to OriginalGriff Answer

Here is the code for you to change the encoding from ascii to utf
SerialPort port = new SerialPort();
port.Encoding  = System.Text.Encoding.GetEncoding(1252);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900