十六进制到UTF 8传输的串行端口通信 [英] serial port communication for hex to UTF 8 transfer

查看:79
本文介绍了十六进制到UTF 8传输的串行端口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在放置查询代码,其中发送到天线的数据未转换为ASCII,并且在通信中出现错误,黑体值未转换为等效的ASCII,并且我无法与之通信天线,因此,如果我将UTF8用作转换器,是否可以完美地发送和接收数据?

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?

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.

推荐答案

确定要在此串行端口连接上使用ASCII吗?以第一个突出显示的示例C2为例,这是194个十进制,它不在ascii范围内,但它本身也不是有效的UTF8字符,它是多字节char的一部分.

首先在哪里获取十六进制字符串,有什么理由不能将十六进制转换为字节数组并通过串行端口发送?

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?

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);


这篇关于十六进制到UTF 8传输的串行端口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆