当前位置:   article > 正文

上位机模块之TCP数据解析类_上位机解析16进制

上位机解析16进制

上位机通常会与PLC进行数据通讯在通讯时一般使用16进制与ACS2码进行发送,所以工具类中提供4个方法进行解析数据。
对TCP首发数据提供4个方法
1.将字符串集合转换为字节数组,集合为数字(十进制)
2.将字符串集合转换为字节数组,集合全为字母
3.将字符串指定区域的转换为ACS2,其他区域为数字
4.将字节数组的指定区域转换为字符串
使用方法,全部复制粘贴,创建对象即可使用

public class TCPsock
    {
        /// <summary>
        /// 将字符串集合解析为字节数组,用于TCP的发送,字符串应为全部数字
        /// </summary>
        /// <param name="data">输入的字符串集合,全部是数字</param>
        public byte[] TCPSendInt16(List<string> data)
        {
            int i = 0;
            byte[] sendByetcenter = null, sendByetcenter1 = System.BitConverter.GetBytes(Int16.Parse(Convert.ToString(data[0])));
            Array.Reverse(sendByetcenter1);
            foreach (var item in data)
            {
                if (i == 0) { i++; continue; }
                sendByetcenter = null;
                sendByetcenter = System.BitConverter.GetBytes(Int16.Parse(Convert.ToString(item)));
                Array.Reverse(sendByetcenter);
                sendByetcenter1 = sendByetcenter1.Concat(sendByetcenter).ToArray();
            }
            return sendByetcenter1;
        }
        /// <summary>
        /// 将字符串集合解析为ACS2的形式,返回字节数组
        /// </summary>
        /// <param name="data">字符串集合</param>
        public byte[] TCPSendACS(List<string> data)
        {
            byte[] sendByetcenter = System.Text.Encoding.ASCII.GetBytes(data[0]), byteArray = null;
            int i = 0;
            foreach (var item in data)
            {
                if (i == 0) { i++; continue; }
                byteArray = System.Text.Encoding.ASCII.GetBytes(item);
                sendByetcenter = sendByetcenter.Concat(byteArray).ToArray();
            }
            return sendByetcenter;
        }
        /// <summary>
        /// 将指定区域的集合转换为ACS2,其他转换为数字16进制。
        /// </summary>
        /// <param name="data">输入集合</param>
        /// <param name="begin">集合ACS区域的初始索引</param>
        /// <param name="end">集合ACS区域的最后索引</param>
        public byte[] TCPSendInt16_ACS(List<string> data, int begin, int end)
        {
            byte[] sendByetcenter = null, sendByetcenter1 = null;
            if (begin == 0)
            {
                sendByetcenter1 = System.Text.Encoding.ASCII.GetBytes(data[0]);
            }
            else
            {
                sendByetcenter1 = System.BitConverter.GetBytes(Int16.Parse(Convert.ToString(data[0])));
                Array.Reverse(sendByetcenter1);
            }
            for (int i = 1; i < begin && i < data.Count; i++)
            {
                sendByetcenter = null;
                sendByetcenter = System.BitConverter.GetBytes(Int16.Parse(Convert.ToString(data[i])));
                Array.Reverse(sendByetcenter);
                sendByetcenter1 = sendByetcenter1.Concat(sendByetcenter).ToArray();
            }
            for (int i = begin; i <= end && i < data.Count; i++)
            {
                sendByetcenter = System.Text.Encoding.ASCII.GetBytes(data[i]);
                sendByetcenter1 = sendByetcenter1.Concat(sendByetcenter).ToArray();
            }
            for (int i = end + 1; i < data.Count; i++)
            {
                sendByetcenter = null;
                sendByetcenter = System.BitConverter.GetBytes(Int16.Parse(Convert.ToString(data[i])));
                Array.Reverse(sendByetcenter);
                sendByetcenter1 = sendByetcenter1.Concat(sendByetcenter).ToArray();
            }
            return sendByetcenter1;
        }
        /// <summary>
        /// 将字节数组中的区域字节转换为字符串,注意区域字节需要为同一个类型的区域,ACS2与HEX需要被区分
        /// </summary>
        /// <param name="begin">开始的字节</param>
        /// <param name="end">结束的字节</param>
        /// <param name="data">字节数组</param>
        /// <returns>返回字符串</returns>
        public string TCPAccept(int begin, int end, byte[] data)
        {
            string hex = null;
            bool HEX = true;
            if ((int)data[begin] > 16) HEX = false;
            if (HEX)
            {
                for (int i = begin; i < data.Length && i < end; i++)
                {
                    hex = hex + data[i];
                }
            }
            else
            {
                hex = Encoding.Default.GetString(data);
            }
            return hex;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/293923
推荐阅读
相关标签
  

闽ICP备14008679号