Exemplo n.º 1
0
        /// <summary>
        /// 从三菱PLC中读取想要的数据,输入地址,按照字单位读取,返回读取结果
        /// </summary>
        /// <param name="address">读取地址,格式为"M100","D100","W1A0"</param>
        /// <param name="length">读取的数据长度,字最大值960,位最大值7168</param>
        /// <returns>带成功标志的结果数据对象</returns>
        /// <remarks>
        /// 地址支持的列表参考 <seealso cref="MelsecMcNet"/> 的备注说明
        /// </remarks>
        /// <example>
        /// 假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,读取如下:
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="ReadExample2" title="Read示例" />
        /// 以下是读取不同类型数据的示例
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="ReadExample1" title="Read示例" />
        /// </example>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            // 分析地址
            OperateResult <byte[]> coreResult = MelsecHelper.BuildReadMcCoreCommand(address, length, false, McAnalysisAddress);

            if (!coreResult.IsSuccess)
            {
                return(coreResult);
            }

            // 核心交互
            var read = ReadFromCoreServer(PackMcCommand(coreResult.Content, this.NetworkNumber, this.NetworkStationNumber));

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

            // 错误代码验证
            ushort errorCode = BitConverter.ToUInt16(read.Content, 9);

            if (errorCode != 0)
            {
                return(new OperateResult <byte[]>(errorCode, StringResources.Language.MelsecPleaseReferToManulDocument));
            }

            // 数据解析,需要传入是否使用位的参数
            return(ExtractActualData(SoftBasic.BytesArrayRemoveBegin(read.Content, 11), false));
        }
        /// <summary>
        /// 从三菱PLC中批量读取位软元件,返回读取结果
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="length">读取的长度</param>
        /// <returns>带成功标志的结果数据对象</returns>
        /// <remarks>
        /// 地址支持的列表参考 <seealso cref="T:YumpooDrive.Profinet.Melsec.MelsecMcNet" /> 的备注说明
        /// </remarks>
        /// <example>
        /// <code lang="cs" source="ProcessControlService.CommunicationStandard_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="ReadBool" title="Bool类型示例" />
        /// </example>
        public override OperateResult <bool[]> ReadBool(string address, ushort length)
        {
            OperateResult <McAddressData> operateResult = McAnalysisAddress(address, length);

            if (!operateResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(operateResult));
            }
            byte[] mcCore = MelsecHelper.BuildReadMcCoreCommand(operateResult.Content, isBit: true);
            OperateResult <byte[]> operateResult2 = ReadFromCoreServer(PackMcCommand(mcCore, NetworkNumber, NetworkStationNumber));

            if (!operateResult2.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(operateResult2));
            }
            ushort num = BitConverter.ToUInt16(operateResult2.Content, 9);

            if (num != 0)
            {
                return(new OperateResult <bool[]>(num, StringResources.Language.MelsecPleaseReferToManulDocument));
            }
            OperateResult <byte[]> operateResult3 = ExtractActualData(SoftBasic.BytesArrayRemoveBegin(operateResult2.Content, 11), isBit: true);

            if (!operateResult3.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(operateResult3));
            }
            return(OperateResult.CreateSuccessResult(operateResult3.Content.Select((byte m) => m == 1).Take(length).ToArray()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 从PLC中读取想要的数据,返回读取结果
        /// </summary>
        /// <param name="address">读取地址</param>
        /// <param name="length">读取的数据长度</param>
        /// <returns>带成功标志的结果数据对象</returns>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            // 获取指令
            var command = BuildReadCommand(address, length);

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

            // 核心交互
            var read = ReadFromCoreServer(command.Content);

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

            // 错误代码验证
            if (read.Content[1] != 0)
            {
                return(new OperateResult <byte[]>(read.Content[4], StringResources.Language.ToyopucPleaseReferToManulDocument));
            }

            // 数据解析,需要传入是否使用位的参数
            return(ExtractActualData(SoftBasic.BytesArrayRemoveBegin(read.Content, 5), false));
        }
        public void BytesArrayRemoveBeginTest( )
        {
            byte[] b1 = new byte[] { 0x13, 0xA6, 0x15, 0x85, 0x5B, 0x05, 0x12 };
            byte[] b2 = new byte[] { 0x85, 0x5B, 0x05, 0x12 };

            byte[] buffer = SoftBasic.BytesArrayRemoveBegin(b1, 3);
            Assert.IsTrue(SoftBasic.IsTwoBytesEquel(b2, buffer));
        }
        public void BytesArrayRemoveBeginExample( )
        {
            #region BytesArrayRemoveBegin

            byte[] b1 = new byte[] { 0x13, 0xA6, 0x15, 0x85, 0x5B, 0x05, 0x12 };

            byte[] buffer = SoftBasic.BytesArrayRemoveBegin(b1, 3);


            // buffer 的值就是b1移除了前三个字节 new byte[] { 0x85, 0x5B, 0x05, 0x12 };

            #endregion
        }
Exemplo n.º 6
0
 /// <summary>
 /// 当收到mc协议的报文的时候应该触发的方法,允许继承重写,来实现自定义的返回,或是数据监听。
 /// </summary>
 /// <param name="mcCore">mc报文</param>
 /// <returns>返回的报文信息</returns>
 protected virtual byte[] ReadFromMcCore(byte[] mcCore)
 {
     if (mcCore[11] == 0x01 && mcCore[12] == 0x04)
     {
         // 读数据
         return(PackCommand(ReadByCommand(SoftBasic.BytesArrayRemoveBegin(mcCore, 11))));
     }
     else if (mcCore[11] == 0x01 && mcCore[12] == 0x14)
     {
         // 写数据
         return(PackCommand(WriteByMessage(SoftBasic.BytesArrayRemoveBegin(mcCore, 11))));
     }
     else
     {
         return(null);
     }
 }
        private OperateResult <byte[]> ReadAddressData(McAddressData addressData)
        {
            byte[] mcCore = MelsecHelper.BuildReadMcCoreCommand(addressData, isBit: false);
            OperateResult <byte[]> operateResult = ReadFromCoreServer(PackMcCommand(mcCore, NetworkNumber, NetworkStationNumber));

            if (!operateResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(operateResult));
            }
            ushort num = BitConverter.ToUInt16(operateResult.Content, 9);

            if (num != 0)
            {
                return(new OperateResult <byte[]>(num, StringResources.Language.MelsecPleaseReferToManulDocument));
            }
            return(ExtractActualData(SoftBasic.BytesArrayRemoveBegin(operateResult.Content, 11), isBit: false));
        }
Exemplo n.º 8
0
        private void SocketAsyncCallBack(IAsyncResult ar)
        {
            if (ar.AsyncState is AppSession session)
            {
                try
                {
                    int receiveCount = session.WorkSocket.EndReceive(ar);

                    ModbusTcpMessage       mdMessage = new ModbusTcpMessage( );
                    OperateResult <byte[]> read1     = ReceiveByMessage(session.WorkSocket, 5000, mdMessage);
                    if (!read1.IsSuccess)
                    {
                        LogNet?.WriteDebug(ToString( ), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint));
                        RemoveClient(session);
                        return;
                    }
                    ;

                    ushort id   = (ushort)(read1.Content[0] * 256 + read1.Content[1]);
                    byte[] back = ModbusInfo.PackCommandToTcp(ReadFromModbusCore(SoftBasic.BytesArrayRemoveBegin(read1.Content, 6)), id);
                    if (back != null)
                    {
                        session.WorkSocket.Send(back);
                    }
                    else
                    {
                        session.WorkSocket.Close( );
                        RemoveClient(session);
                        return;
                    }

                    RaiseDataReceived(read1.Content);
                    session.WorkSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(SocketAsyncCallBack), session);
                }
                catch
                {
                    // 关闭连接,记录日志
                    session.WorkSocket?.Close( );
                    LogNet?.WriteDebug(ToString( ), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint));
                    RemoveClient(session);
                    return;
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 从三菱PLC中批量读取位软元件,返回读取结果
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="length">读取的长度</param>
        /// <returns>带成功标志的结果数据对象</returns>
        /// <remarks>
        /// 地址支持的列表参考 <seealso cref="MelsecMcNet"/> 的备注说明
        /// </remarks>
        /// <example>
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="ReadBool" title="Bool类型示例" />
        /// </example>
        public override OperateResult <bool[]> ReadBool(string address, ushort length)
        {
            // 分析地址
            OperateResult <McAddressData> addressResult = McAnalysisAddress(address, length);

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

            // 获取指令
            byte[] coreResult = MelsecHelper.BuildReadMcCoreCommand(addressResult.Content, true);

            // 核心交互
            var read = ReadFromCoreServer(PackMcCommand(coreResult, NetworkNumber, NetworkStationNumber));

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

            // 错误代码验证
            ushort errorCode = BitConverter.ToUInt16(read.Content, 9);

            if (errorCode != 0)
            {
                return(new OperateResult <bool[]>(errorCode, StringResources.Language.MelsecPleaseReferToManulDocument));
            }

            // 数据解析,需要传入是否使用位的参数
            var extract = ExtractActualData(SoftBasic.BytesArrayRemoveBegin(read.Content, 11), true);

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

            // 转化bool数组
            return(OperateResult.CreateSuccessResult(extract.Content.Select(m => m == 0x01).Take(length).ToArray( )));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 从PLC批量读取位软元件,返回读取结果
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="length">读取的长度</param>
        /// <returns>带成功标志的结果数据对象</returns>
        public override OperateResult <bool[]> ReadBool(string address, ushort length)
        {
            // 获取指令
            var command = BuildReadCommand(address, length);

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

            // 核心交互
            var read = ReadFromCoreServer(command.Content);

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

            // 错误代码验证
            if (read.Content[1] != 0)
            {
                return(new OperateResult <bool[]>(read.Content[4], StringResources.Language.ToyopucPleaseReferToManulDocument));
            }


            // 数据解析,需要传入是否使用位的参数
            var extract = ExtractActualData(SoftBasic.BytesArrayRemoveBegin(read.Content, 5), true);

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

            // 转化bool数组
            return(OperateResult.CreateSuccessResult(extract.Content.Select(m => m == 0x01).Take(length).ToArray()));
        }
Exemplo n.º 11
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);
                }
            }
        }