c#串口的十六进制发送转换函数

  private static byte[] strToHexByte(string hexString)
  {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
   }
在串口的发送函数里面,把相应的字符串转为这个16进制的字节数据发送过去就好了。

猜你喜欢

转载自blog.csdn.net/karaysn/article/details/78999297