コード例 #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);
        }
コード例 #2
0
        public void Test()
        {
            TcpClientComPort tcpClientComPort = new TcpClientComPort(new TcpClientComPortConfigInfo()
            {
                RemoteIPAddress = "192.168.88.90",
                RemotePort      = 8501,
                SendTimeout     = 500,
                ReceiveTimeout  = 500
            });
            PlcKeyenceUpperLink protocolHostLink = new PlcKeyenceUpperLink(tcpClientComPort, 0);

            try
            {
                tcpClientComPort.Open();

                Thread.Sleep(500);

                var v1 = protocolHostLink.Read <Boolean>(new DataAddress()
                {
                    Value  = "MR1100",
                    Type   = DataAddressType.Boolean,
                    Offset = 0
                });
                protocolHostLink.Write <Boolean>(new DataAddress()
                {
                    Value  = "MR1100",
                    Type   = DataAddressType.Boolean,
                    Offset = 0
                }, new Boolean[] { !v1.Single() });

                var v2 = protocolHostLink.Read <short>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Short,
                    Offset = 0
                });
                protocolHostLink.Write <short>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Short,
                    Offset = 0
                }, new short[] { 100 });

                var v3 = protocolHostLink.Read <ushort>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Ushort,
                    Offset = 0
                });
                protocolHostLink.Write <ushort>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Ushort,
                    Offset = 0
                }, new ushort[] { 100 });

                var v4 = protocolHostLink.Read <int>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Int,
                    Offset = 0
                });
                protocolHostLink.Write <int>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Int,
                    Offset = 0
                }, new int[] { 100 });

                var v5 = protocolHostLink.Read <float>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Float,
                    Offset = 0
                });
                protocolHostLink.Write <float>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.Float,
                    Offset = 0
                }, new float[] { 0.3f });

                var v6 = protocolHostLink.Read <String>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.String,
                    Offset = 5
                });
                protocolHostLink.Write <String>(new DataAddress()
                {
                    Value  = "DM1180",
                    Type   = DataAddressType.String,
                    Offset = 5
                }, new string[] { "DM11800" });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                tcpClientComPort?.Close();
            }
        }