zoukankan      html  css  js  c++  java
  • 串口发送16进制字节数组

            /// <summary>
            /// 字符串转16进制字节数组
            /// </summary>
            /// <param name="hexString">例如20 1A</param>
            /// <returns></returns>
            public 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;
            }

            /// <summary>
            /// 字节数组转16进制字符串
            /// </summary>
            /// <param name="bytes"></param>
            /// <returns></returns>
            public static string byteToHexStr(byte[] bytes)
            {
                string returnStr = "";
                if (bytes != null)
                {
                    //for (int i = 0; i < bytes.Length; i++)
                    //{
                    //    returnStr += bytes[i].ToString("X2") + " ";
                    //}
                    foreach (byte byteData in bytes)
                    {
                        returnStr += byteData.ToString("X2") + " ";//X为 十六进制 2为 每次都是两位数 比如0x0A
                    }
                }
                return returnStr.Trim();
            }

    串口字节发送,

                byte[] SendBuf = strToHexByte(textBox3.Text);
               
                _serialPort.Write(SendBuf, 0, SendBuf.Length);

    串口字符串发送

              _serialPort.Write(textBox2.Text + " ");        

  • 相关阅读:
    C++复制构造函数,类型转换构造函数,析构函数,引用,指针常量和常量指针
    POJ1611(The Suspects)--简单并查集
    最小生成树-Kruskal算法
    POJ1861(Network)-Kruskal
    POJ1979(Red and Black)--FloodFill
    [转]全网最!详!细!tarjan算法讲解
    POJ1573(Robot Motion)--简单模拟+简单dfs
    最小生成树-Prim算法
    POJ3368(Frequent values)--线段树
    POJ3255(Roadblocks)--次短路径
  • 原文地址:https://www.cnblogs.com/liujicai/p/3568678.html
Copyright © 2011-2022 走看看