Beispiel #1
0
        /// <summary>
        /// 创建Plc
        /// </summary>
        /// <param name="plcProtocolName">协议名称</param>
        /// <param name="tcpClientComPortConfigInfo">TcpClient端口配置信息</param>
        /// <param name="readTimeoutSeconds">读取超时</param>
        /// <param name="loggerFactory">日志工厂</param>
        /// <returns>Plc对象</returns>
        public static IPlc Create(PlcProtocolName plcProtocolName, TcpClientComPortConfigInfo tcpClientComPortConfigInfo, int readTimeoutSeconds = 3, ILoggerFactory loggerFactory = null)
        {
            IPlc     result  = null;
            IComPort comPort = new TcpClientComPort(tcpClientComPortConfigInfo, loggerFactory);

            if (plcProtocolName == PlcProtocolName.PlcOmronFinsTcp)
            {
                result = new PlcOmronFins(comPort, readTimeoutSeconds);
            }

            if (plcProtocolName == PlcProtocolName.PlcKeyenceUpperLink)
            {
                result = new PlcKeyenceUpperLink(comPort, readTimeoutSeconds);
            }

            if (plcProtocolName == PlcProtocolName.PlcMelsecMcBinary)
            {
                result = new PlcMelsecMcBinary(tcpClientComPortConfigInfo.RemoteIPAddress, (ushort)tcpClientComPortConfigInfo.RemotePort);
            }

            if (plcProtocolName == PlcProtocolName.PlcManualSimulator)
            {
                result = new PlcManualSimulator();
            }

            return(result);
        }
        public void ReadIntTest()
        {
            var comPort         = Substitute.For <IComPort>();
            var protocolFinsTcp = new PlcOmronFins(comPort, 0);
            var values          = new int[] { };

            comPort.IsConnected.Returns(true);
            comPort.BytesToRead.Returns(8, 26);
            comPort.Read(Arg.Any <byte[]>(), Arg.Any <int>(), Arg.Any <int>()).Returns(
                ci =>
            {
                Array.Copy(new byte[] { 0x46, 0x49, 0x4E, 0x53, 0x00, 0x00, 0x00, 0x1A }, ci.ArgAt <byte[]>(0), 8);

                return(ci.ArgAt <byte[]>(0).Count());
            },
                ci =>
            {
                Array.Copy(new byte[] { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x02, 0x00, 0xC0, 0x00, 0x00, 0x21, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44 }, ci.ArgAt <byte[]>(0), 26);

                return(ci.ArgAt <byte[]>(0).Count());
            }
                );

            values = protocolFinsTcp.Read <int>(new DataAddress()
            {
                Value  = "D1234",
                Type   = DataAddressType.Int,
                Offset = 0
            }).ToArray();

            Assert.AreEqual(BitConverter.ToInt32(new byte[] { 0x22, 0x11, 0x44, 0x33 }, 0), values[0]);
        }
        public void WriteBoolTest()
        {
            var comPort         = Substitute.For <IComPort>();
            var protocolFinsTcp = new PlcOmronFins(comPort, 0);
            var excepted        = new byte[] {
                0x46, 0x49, 0x4E, 0x53,
                0x00, 0x00, 0x00, 0x1B,
                0x00, 0x00, 0x00, 0x02,
                0x00, 0x00, 0x00, 0x00,
                0x80, 0x00, 0x02,
                0x00, 0x00, 0x00,
                0x00, 0x00, 0x00,
                0x00,
                0x01, 0x02,
                0x02,
                0x00, 0x64, 0x01,
                0x00, 0x01,
                0x00
            };

            try
            {
                protocolFinsTcp.Write(new DataAddress()
                {
                    Value  = "D100",
                    Type   = DataAddressType.Boolean,
                    Offset = 1
                }, new bool[] { false });
            }
            catch
            {
            }

            comPort.Received().Write(Arg.Is <byte[]>(e => !e.Except(excepted).Any() && !excepted.Except(e).Any()), 0, excepted.Length);
        }
        public void ReadBoolTest()
        {
            var comPort         = Substitute.For <IComPort>();
            var protocolFinsTcp = new PlcOmronFins(comPort, 0);
            var values          = new bool[] { };

            comPort.IsConnected.Returns(true);
            comPort.BytesToRead.Returns(8, 23);
            comPort.Read(Arg.Any <byte[]>(), Arg.Any <int>(), Arg.Any <int>()).Returns(
                ci =>
            {
                Array.Copy(new byte[] { 0x46, 0x49, 0x4E, 0x53, 0x00, 0x00, 0x00, 0x17 }, ci.ArgAt <byte[]>(0), 8);

                return(ci.ArgAt <byte[]>(0).Count());
            },
                ci =>
            {
                Array.Copy(new byte[] { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x02, 0x00, 0xC0, 0x00, 0x00, 0x21, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01 }, ci.ArgAt <byte[]>(0), 23);

                return(ci.ArgAt <byte[]>(0).Count());
            }
                );

            values = protocolFinsTcp.Read <bool>(new DataAddress()
            {
                Value  = "D1234",
                Type   = DataAddressType.Boolean,
                Offset = 1
            }).ToArray();

            Assert.AreEqual(true, values[0]);
        }
        public void ConnectTest()
        {
            var comPort1 = new TcpClientComPort(new TcpClientComPortConfigInfo()
            {
                LocalIPAddress  = "192.168.88.1",
                LocalPort       = 8001,
                RemoteIPAddress = "192.168.88.120",
                RemotePort      = 9600,
                ReceiveTimeout  = 500,
                SendTimeout     = 500
            });
            var protocolFinsTcp1 = new PlcOmronFins(comPort1, 0);
            var comPort2         = new TcpClientComPort(new TcpClientComPortConfigInfo()
            {
                LocalIPAddress  = "192.168.88.2",
                LocalPort       = 8002,
                RemoteIPAddress = "192.168.88.120",
                RemotePort      = 9600,
                ReceiveTimeout  = 500,
                SendTimeout     = 500
            });
            var protocolFinsTcp2 = new PlcOmronFins(comPort2, 0);

            comPort1.Open();
            comPort2.Open();

            protocolFinsTcp1.Initialize();
            protocolFinsTcp2.Initialize();

            protocolFinsTcp1.Write <short>(new DataAddress()
            {
                Type   = DataAddressType.Short,
                Offset = 0,
                Value  = "D8816"
            }, new short[] { 2 });

            protocolFinsTcp2.Write <short>(new DataAddress()
            {
                Type   = DataAddressType.Short,
                Offset = 0,
                Value  = "D8816"
            }, new short[] { 3 });
        }
        public void WriteFloatTest()
        {
            var comPort         = Substitute.For <IComPort>();
            var protocolFinsTcp = new PlcOmronFins(comPort, 0);
            var excepted        = new byte[] {
                0x46, 0x49, 0x4E, 0x53,
                0x00, 0x00, 0x00, 0x1E,
                0x00, 0x00, 0x00, 0x02,
                0x00, 0x00, 0x00, 0x00,
                0x80, 0x00, 0x02,
                0x00, 0x00, 0x00,
                0x00, 0x00, 0x00,
                0x00,
                0x01, 0x02,
                0x82,
                0x00, 0x64, 0x00,
                0x00, 0x01,
                0x3f, 0x80, 0x00, 0x00
            };

            try
            {
                comPort.BytesToRead.Returns(0);
                protocolFinsTcp.Write(new DataAddress()
                {
                    Value  = "D100",
                    Type   = DataAddressType.Float,
                    Offset = 0
                }, new float[] { 1 });
            }
            catch
            {
            }

            comPort.Received().Write(Arg.Is <byte[]>(e => !e.Except(excepted).Any() && !excepted.Except(e).Any()), 0, excepted.Length);
        }