Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am placing the code for my query, where the data I am sending to the antenna is not converted to ASCII and I am getting error in the communication the bolded values are not converted to ASCII equivalent and I am unable to communicate with the antenna so if I use UTF8 as a converter can I send and recieve the data perfectly?

C#
private void btn_Refresh_Click(object sender, EventArgs e)
       {
           try
           {
               //Reading Command Password
               serialPort1.Write(Hex2Ascii("3A08007A00564953494F4E33363DC20D0A"));
              Thread.Sleep(400);
               string resp = serialPort1.ReadExisting();
               resp = resp.Substring(5, 2);
               if (resp == "04")
               {                   serialPort1.Write(Hex2Ascii("3A08000300313233343536373851AE0D0A"));
....
}

public string Hex2Ascii(string str)
        {
            string x2 = "";
            for (int i = 0; i < str.Length; i = i + 2)
            {
                string m = str.Substring(i, 2);  
                int temp = Int32.Parse(m, NumberStyles.HexNumber);   
                char temp1 = converthex((char)temp);
                string x1 = Convert.ToString(Convert.ToChar(temp1)); /
                x2 = x2 + x1;       
               STRING RETRUNS
            }
            return (x2);
        }


public static char converthex(char c)
        {
            if (c == 0x80) return (char)0x20AC;
            if (c == 0x81) return (char)0xfffd;
            if (c == 0x82) return (char)0x201A;
            if (c == 0x83) return (char)0x0192;
            if (c == 0x84) return (char)0x201E;
            if (c == 0x85) return (char)0x2026;
            if (c == 0x86) return (char)0x2020;
            if (c == 0x87) return (char)0x2021;
            if (c == 0x88) return (char)0x02C6;
            if (c == 0x89) return (char)0x2030;
            if (c == 0x8A) return (char)0x0160;
            if (c == 0x8B) return (char)0x2039;
            if (c == 0x8C) return (char)0x0152;
            //            )  return (char)0x8D;
            if (c == 0x8E) return (char)0x017D;
            //            )  return (char)0x8F;
            //            )  return (char)0x90;
            if (c == 0x91) return (char)0x2018;
            if (c == 0x92) return (char)0x2019;
            if (c == 0x93) return (char)0x201C;
            if (c == 0x94) return (char)0x201D;
            if (c == 0x95) return (char)0x2022;
            if (c == 0x96) return (char)0x2013;
            if (c == 0x97) return (char)0x2014;
            if (c == 0x98) return (char)0x02DC;
            if (c == 0x99) return (char)0x2122;
            if (c == 0x9A) return (char)0x0161;
            if (c == 0x9B) return (char)0x203A;
            if (c == 0x9C) return (char)0x0153;
            //            )  return (char)0x9D;
            if (c == 0x9E) return (char)0x017E;
            if (c == 0x9F) return (char)0x0178;
            return c;
        }


This is the code for which I need your help.
Posted
Updated 14-Nov-10 19:25pm
v2

1 solution

Are you sure you need to be using ASCII over this serial port connection? Take the first highlighted example C2 this is 194 decimal which falls outside the ascii range but is also not a valid UTF8 character by itself, it is part of a multibyte char.

Where are you getting the hex strings in the first place and is there any reason you cant just convert the hex to a byte array and send that over the serial port?

C#
string hex = "3A08007A00564953494F4E33363DC20D0A";
byte[] hexbytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length; i += 2)
{
    hexbytes[i/2] = byte.Parse(hex.Substring(i, 2), NumberStyles.HexNumber);
}
serialPort.Write(hexbytes, 0, hexbytes.Length);
 
Share this answer
 
v2

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