コード例 #1
0
        /// <summary>
        /// 从PLC反馈的数据进行提炼bool数组操作
        /// </summary>
        /// <param name="response">PLC反馈的真实数据</param>
        /// <param name="start">起始提取的点信息</param>
        /// <param name="length">bool数组的长度</param>
        /// <returns>数据提炼后的真实数据</returns>
        public static OperateResult <bool[]> ExtractActualBoolData(byte[] response, int start, int length)
        {
            OperateResult <byte[]> operateResult = ExtractActualData(response);

            if (!operateResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(operateResult));
            }
            try
            {
                bool[] array  = new bool[length];
                bool[] array2 = SoftBasic.ByteToBoolArray(operateResult.Content, operateResult.Content.Length * 8);
                for (int i = 0; i < length; i++)
                {
                    array[i] = array2[i + start];
                }
                return(OperateResult.CreateSuccessResult(array));
            }
            catch (Exception ex)
            {
                OperateResult <bool[]> operateResult2 = new OperateResult <bool[]>();
                operateResult2.Message = "Extract Msg:" + ex.Message + Environment.NewLine + "Data: " + SoftBasic.ByteToHexString(response);
                return(operateResult2);
            }
        }
コード例 #2
0
        /// <summary>
        /// 批量的读取输入点,需要指定起始地址,读取长度
        /// </summary>
        /// <param name="address">起始地址,格式为"1234"</param>
        /// <param name="length">读取长度</param>
        /// <returns>带有成功标志的bool数组对象</returns>
        public OperateResult <bool[]> ReadDiscrete(string address, ushort length)
        {
            var read = ReadModBusBase(ModbusInfo.ReadDiscrete, address, length);

            if (!read.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(read));
            }

            return(OperateResult.CreateSuccessResult(SoftBasic.ByteToBoolArray(read.Content, length)));
        }
コード例 #3
0
        public void ByteToBoolArrayExample( )
        {
            #region ByteToBoolArray

            byte[] buffer = new byte[2] {
                0xB9, 0x28
            };
            bool[] result = SoftBasic.ByteToBoolArray(buffer, 15);

            // 结果如下
            // result = new bool[] { true, false, false, true, true, true, false, true, false, false, false, true, false, true, false };

            #endregion
        }
コード例 #4
0
        /// <summary>
        /// 批量的离散变量,需要指定起始地址,读取长度
        /// </summary>
        /// <param name="address">起始地址,格式为"1234"</param>
        /// <param name="length">读取长度</param>
        /// <returns>带有成功标志的bool数组对象</returns>
        public OperateResult <bool[]> ReadDiscrete(string address, ushort length)
        {
            var result = new OperateResult <bool[]>( );
            var read   = ReadModBusBase(ModbusInfo.ReadDiscrete, address, length);

            if (!read.IsSuccess)
            {
                result.CopyErrorFromOther(read);
                return(result);
            }

            result.Content   = SoftBasic.ByteToBoolArray(read.Content, length);
            result.IsSuccess = true;
            return(result);
        }
コード例 #5
0
        public void ByteToBoolArrayTest( )
        {
            bool[] values = new bool[] { true, false, false, true, true, true, false, true, false, false, false, true, false, true, false };
            byte[] buffer = new byte[2] {
                0xB9, 0x28
            };
            bool[] result = SoftBasic.ByteToBoolArray(buffer, 15);

            for (int i = 0; i < result.Length; i++)
            {
                if (values[i] != result[i])
                {
                    Assert.Fail("值不一致");
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// 批量读取线圈或是离散的数据信息,需要指定地址和长度,具体的结果取决于实现
        /// </summary>
        /// <param name="address">数据地址</param>
        /// <param name="length">数据长度</param>
        /// <returns>带有成功标识的bool[]数组</returns>
        public override OperateResult <bool[]> ReadBool(string address, ushort length)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.ReadCoil);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(analysis));
            }

            var read = ReadModBusBase((byte)analysis.Content.Function, address, length);

            if (!read.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(read));
            }

            return(OperateResult.CreateSuccessResult(SoftBasic.ByteToBoolArray(read.Content, length)));
        }
コード例 #7
0
        /// <summary>
        /// 写入自定义的数据到数据内存中去
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="value">数据值</param>
        /// <returns>是否写入成功的结果对象</returns>
        public override OperateResult Write(string address, byte[] value)
        {
            // 分析地址
            OperateResult <McAddressData> analysis = McAddressData.ParseMelsecFrom(address, 0);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            if (analysis.Content.McDataType.DataCode == MelsecMcDataType.M.DataCode)
            {
                byte[] buffer = SoftBasic.ByteToBoolArray(value).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                mBuffer.SetBytes(buffer, analysis.Content.AddressStart);
                return(OperateResult.CreateSuccessResult( ));
            }
            else if (analysis.Content.McDataType.DataCode == MelsecMcDataType.X.DataCode)
            {
                byte[] buffer = SoftBasic.ByteToBoolArray(value).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                xBuffer.SetBytes(buffer, analysis.Content.AddressStart);
                return(OperateResult.CreateSuccessResult( ));
            }
            else if (analysis.Content.McDataType.DataCode == MelsecMcDataType.Y.DataCode)
            {
                byte[] buffer = SoftBasic.ByteToBoolArray(value).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                yBuffer.SetBytes(buffer, analysis.Content.AddressStart);
                return(OperateResult.CreateSuccessResult( ));
            }
            else if (analysis.Content.McDataType.DataCode == MelsecMcDataType.D.DataCode)
            {
                dBuffer.SetBytes(value, analysis.Content.AddressStart * 2);
                return(OperateResult.CreateSuccessResult( ));
            }
            else if (analysis.Content.McDataType.DataCode == MelsecMcDataType.W.DataCode)
            {
                wBuffer.SetBytes(value, analysis.Content.AddressStart * 2);
                return(OperateResult.CreateSuccessResult( ));
            }
            else
            {
                return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
コード例 #8
0
 /// <summary>
 /// 从缓存中提取出bool数组结果
 /// </summary>
 /// <param name="buffer">缓存数据</param>
 /// <param name="index">位的索引</param>
 /// <param name="length">bool长度</param>
 /// <returns>bool数组</returns>
 public bool[] TransBool(byte[] buffer, int index, int length)
 {
     byte[] tmp = new byte[length];
     Array.Copy(buffer, index, tmp, 0, length);
     return(SoftBasic.ByteToBoolArray(tmp, length * 8));
 }
コード例 #9
0
        private byte[] WriteByMessage(byte[] command)
        {
            if (command[2] == 0x01)
            {
                // 位写入
                ushort length     = ByteTransform.TransUInt16(command, 8);
                int    startIndex = (command[6] * 65536 + command[5] * 256 + command[4]);

                if (command[7] == MelsecMcDataType.M.DataCode)
                {
                    byte[] buffer = MelsecMcNet.ExtractActualData(SoftBasic.BytesArrayRemoveBegin(command, 10), true).Content;
                    mBuffer.SetBytes(buffer.Take(length).ToArray( ), startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.X.DataCode)
                {
                    byte[] buffer = MelsecMcNet.ExtractActualData(SoftBasic.BytesArrayRemoveBegin(command, 10), true).Content;
                    xBuffer.SetBytes(buffer.Take(length).ToArray( ), startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.Y.DataCode)
                {
                    byte[] buffer = MelsecMcNet.ExtractActualData(SoftBasic.BytesArrayRemoveBegin(command, 10), true).Content;
                    yBuffer.SetBytes(buffer.Take(length).ToArray( ), startIndex);
                    return(new byte[0]);
                }
                else
                {
                    throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
            else
            {
                // 字写入
                ushort length     = ByteTransform.TransUInt16(command, 8);
                int    startIndex = (command[6] * 65536 + command[5] * 256 + command[4]);

                if (command[7] == MelsecMcDataType.M.DataCode)
                {
                    byte[] buffer = SoftBasic.ByteToBoolArray(SoftBasic.BytesArrayRemoveBegin(command, 10)).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                    mBuffer.SetBytes(buffer, startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.X.DataCode)
                {
                    byte[] buffer = SoftBasic.ByteToBoolArray(SoftBasic.BytesArrayRemoveBegin(command, 10)).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                    xBuffer.SetBytes(buffer, startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.Y.DataCode)
                {
                    byte[] buffer = SoftBasic.ByteToBoolArray(SoftBasic.BytesArrayRemoveBegin(command, 10)).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                    yBuffer.SetBytes(buffer, startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.D.DataCode)
                {
                    dBuffer.SetBytes(SoftBasic.BytesArrayRemoveBegin(command, 10), startIndex * 2);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.W.DataCode)
                {
                    wBuffer.SetBytes(SoftBasic.BytesArrayRemoveBegin(command, 10), startIndex * 2);
                    return(new byte[0]);
                }
                else
                {
                    throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
        }
コード例 #10
0
        /// <summary>
        ///     获取当前的数据信息实际值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="byteTransform"></param>
        /// <returns></returns>
        public dynamic GetValue(byte[] data, IByteTransform byteTransform)
        {
            if (RegularCode == RegularNodeTypeItem.Bool.Code)
            {
                if (TypeLength == 1)
                {
                    var tmp = SoftBasic.ByteToBoolArray(data, data.Length * 8);
                    return(tmp[Index]);
                }
                else
                {
                    var tmp   = SoftBasic.ByteToBoolArray(data, data.Length * 8);
                    var array = new bool[TypeLength];
                    for (var i = 0; i < array.Length; i++)
                    {
                        array[i] = tmp[Index + i];
                    }
                    return(array);
                }
            }

            if (RegularCode == RegularNodeTypeItem.Int16.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransInt16(data, Index));
                }
                return(byteTransform.TransInt16(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.UInt16.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransUInt16(data, Index));
                }
                return(byteTransform.TransUInt16(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.Int32.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransInt32(data, Index));
                }
                return(byteTransform.TransInt32(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.UInt32.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransUInt32(data, Index));
                }
                return(byteTransform.TransUInt32(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.Int64.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransInt64(data, Index));
                }
                return(byteTransform.TransInt64(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.UInt64.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransUInt64(data, Index));
                }
                return(byteTransform.TransUInt64(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.Float.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransSingle(data, Index));
                }
                return(byteTransform.TransSingle(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.Double.Code)
            {
                if (TypeLength == 1)
                {
                    return(byteTransform.TransDouble(data, Index));
                }
                return(byteTransform.TransDouble(data, Index, TypeLength));
            }

            if (RegularCode == RegularNodeTypeItem.StringAscii.Code)
            {
                return(Encoding.ASCII.GetString(data, Index, TypeLength));
            }
            throw new Exception("Not Supported Data Type");
        }