TCP data analysis class of upper computer module

The upper computer usually communicates data with the PLC, and generally uses hexadecimal and ACS2 codes to send data during communication, so the tool class provides 4 methods to analyze data.
Provide 4 methods for TCP initial data
1. Convert the string collection to a byte array, and the collection is a number (decimal)
2. Convert the string collection to a byte array, and the collection is all letters
3. Convert the string to the specified area Convert to ACS2, and other areas to numbers
4. Convert the specified area of ​​​​the byte array to a string
How to use, copy and paste all, create an object and use it

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

Guess you like

Origin blog.csdn.net/m0_51559565/article/details/129834080