Plugins/Drivers/DriverSiemensS7/S7.Net/Types/Int.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;

namespace S7.Net.Types
{
    /// <summary>
    /// Contains the conversion methods to convert Int from S7 plc to C#.
    /// </summary>
    public static class Int
    {
        /// <summary>
        /// Converts a S7 Int (2 bytes) to short (Int16)
        /// </summary>
        public static short FromByteArray(byte[] bytes)
        {
            if (bytes.Length != 2)
            {
                throw new ArgumentException("Wrong number of bytes. Bytes array must contain 2 bytes.");
            }
            // bytes[0] -> HighByte
            // bytes[1] -> LowByte
            return (short)((int)(bytes[1]) | ((int)(bytes[0]) << 8));
        }


        /// <summary>
        /// Converts a short (Int16) to a S7 Int byte array (2 bytes)
        /// </summary>
        public static byte[] ToByteArray(Int16 value)
        {
            byte[] bytes = new byte[2];

            bytes[0] = (byte) (value >> 8 & 0xFF);
            bytes[1] = (byte)(value & 0xFF);

            return bytes;
        }

        /// <summary>
        /// Converts an array of short (Int16) to a S7 Int byte array (2 bytes)
        /// </summary>
        public static byte[] ToByteArray(Int16[] value)
        {
            byte[] bytes = new byte[value.Length * 2];
            int bytesPos = 0;

            for(int i=0; i< value.Length; i++)
            {
                bytes[bytesPos++] = (byte)((value[i] >> 8) & 0xFF);
                bytes[bytesPos++] = (byte) (value[i] & 0xFF);
            }
            return bytes;
        }

        /// <summary>
        /// Converts an array of S7 Int to an array of short (Int16)
        /// </summary>
        public static Int16[] ToArray(byte[] bytes)
        {
            int shortsCount = bytes.Length / 2;

后面还有 27 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。