예제 #1
0
        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="siemens"></param>
        public void SetPlcType(SiemensPLCS siemens)
        {
            switch (siemens)
            {
            case SiemensPLCS.S200:
                plcHead1 = plcHead1_200;
                plcHead2 = plcHead2_200;
                break;

            case SiemensPLCS.S300:
                plcHead1[0x15] = 2;
                break;

            case SiemensPLCS.S400:
                plcHead1[0x11] = 0;
                plcHead1[0x15] = 3;
                break;

            case SiemensPLCS.S200Smart:
                plcHead1 = plcHead1_200smart;
                plcHead2 = plcHead2_200smart;
                break;

            case SiemensPLCS.S1200:
            case SiemensPLCS.S1500:
                plcHead1[0x15] = 0;
                break;
            }
        }
예제 #2
0
        private PLCService()
        {
            string str  = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string type = IniService.Instance.ReadIniData("PLC", "Type", "S1200", str + "Config.ini");

            SiemensPLCS siemensPLCS = 0;

            switch (type)
            {
            case "S1200":
                siemensPLCS = SiemensPLCS.S1200;
                break;

            case "S300":
                siemensPLCS = SiemensPLCS.S300;
                break;

            case "S1500":
                siemensPLCS = SiemensPLCS.S1500;
                break;

            case "S200Smart":
                siemensPLCS = SiemensPLCS.S200Smart;
                break;

            case "S200":
                siemensPLCS = SiemensPLCS.S200;
                break;

            default:
                break;
            }

            siemensTcpNet = new SiemensS7Net(siemensPLCS);
        }
예제 #3
0
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="type"></param>
        /// <param name="host"></param>
        public void Connect(SiemensPLCS type, string host)
        {
            if (start)
            {
                return;
            }
            plcType = type;
            plcIP   = host;
            headData.SetPlcType(type);

            try
            {
                tcpsend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                {
                    NoDelay = true
                };
                tcpsend.ReceiveTimeout = timeOut * 1000;
                tcpsend.SendTimeout    = timeOut * 1000;
                var remotepoint = new IPEndPoint(IPAddress.Parse(host), plcPort);
                tcpsend.Connect(remotepoint);
                response = new SiemensMessage();
                start    = true;
                Active();
            }
            catch (Exception ex)
            {
                Connected = false;
                start     = false;
                ErrorMsg("Connect:" + ex.Message);
            }
        }
 private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     siemensPLCSelected = comboBox1.SelectedItem.ToString( ) == "S1200" ? SiemensPLCS.S1200 :
                          comboBox1.SelectedItem.ToString( ) == "S1500" ? SiemensPLCS.S1500 :
                          comboBox1.SelectedItem.ToString( ) == "S300" ? SiemensPLCS.S300 :
                          comboBox1.SelectedItem.ToString( ) == "S400" ? SiemensPLCS.S400 :
                          comboBox1.SelectedItem.ToString( ) == "S200" ? SiemensPLCS.S200 :
                          SiemensPLCS.S200Smart;
     if (siemensPLCSelected == SiemensPLCS.S400)
     {
         textBox15.Text = "0";
         textBox16.Text = "3";
     }
     else if (siemensPLCSelected == SiemensPLCS.S1200)
     {
         textBox15.Text = "0";
         textBox16.Text = "0";
     }
     else if (siemensPLCSelected == SiemensPLCS.S300)
     {
         textBox15.Text = "0";
         textBox16.Text = "2";
     }
     else if (siemensPLCSelected == SiemensPLCS.S1500)
     {
         textBox15.Text = "0";
         textBox16.Text = "0";
     }
 }
예제 #5
0
 //连接PLC  SiemensPLCS.S1200
 public void SiemensS7NetConnect(SiemensPLCS siemens, out string strMsg)
 {
     strMsg                       = "";
     siemensTcpNet                = new SiemensS7Net(siemens);                                 // 实例化西门子的对象
     siemensTcpNet.IpAddress      = ConfigurationManager.AppSettings["S7IpAdress"].ToString(); // 设置IP地址
     siemensTcpNet.ConnectTimeOut = 1000;                                                      // 超时时间为1秒
     try
     {
         OperateResult connect = siemensTcpNet.ConnectServer();                             //切换为长连接
         if (connect.IsSuccess)
         {
             //成功添加处理,日志?
             Log.loginfo.Info("PLC连接成功!");
             strMsg = "PLC连接成功";
         }
         else
         {
             strMsg = HslCommunication.StringResources.Language.ConnectedFailed + connect.Message;
             Log.logerror.Error("PLC连接失败!", new Exception("失败"));
         }
     }
     catch (Exception ex)
     {
         strMsg = ex.Message;
     }
 }
예제 #6
0
        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="siemens">指定西门子的型号</param>
        /// <param name="ipAddress">Ip地址</param>
        private void Initialization(SiemensPLCS siemens, string ipAddress)
        {
            WordLength = 2;
            IpAddress  = ipAddress;
            Port       = 102;
            CurrentPlc = siemens;

            switch (siemens)
            {
            case SiemensPLCS.S1200: plcHead1[21] = 0; break;

            case SiemensPLCS.S300: plcHead1[21] = 2; break;

            case SiemensPLCS.S1500: plcHead1[21] = 0; break;

            case SiemensPLCS.S200Smart:
            {
                plcHead1 = plcHead1_200smart;
                plcHead2 = plcHead2_200smart;
                break;
            }

            default: plcHead1[18] = 0; break;
            }
        }
예제 #7
0
 //带参构造函数
 /// <summary>
 /// 创建与PLC的通信连接对象,需要手动建立长连接
 /// </summary>
 /// <param name="siemens"></param>
 /// <param name="ipAddress"></param>
 public SiemensPlc(SiemensPLCS siemens, string ipAddress)
 {
     this.siemensTcpNet = new SiemensS7Net_New(siemens, ipAddress)
     {
         ConnectTimeOut = 1000
     };
     this.IpAddress = ipAddress;
 }
예제 #8
0
        public override void DoConnect(string UsedConnectionStr)
        {
            // "192.168.1.10:102:SiemensS7Net:s71200" 、 "192.168.1.10:102:MelsecMcNet"
            string[] arr = UsedConnectionStr.Split(':');
            if (arr.Count() < 3)
            {
                throw new Exception("参数字符串无效!");
            }
            switch (arr[2])
            {
            case "SiemensS7Net":
                if (HslDevice == null)
                {
                    SiemensPLCS SiemensPLCS = (SiemensPLCS)Enum.Parse(typeof(SiemensPLCS), arr[3], true);
                    HslDevice      = new SiemensS7Net(SiemensPLCS);
                    HslDevice.Rack = 0;
                    HslDevice.Slot = 0;
                }
                break;

            default:
                if (HslDevice == null)
                {
                    //创建指定类型的实例
                    //如果创建时需要参数,就和SiemensS7Net一样单独创建就好
                    HslDevice = SPlugIn.CreateObj(arr[2]);
                }
                break;
            }
            string useIP   = arr[0];
            int    usePort = Convert.ToInt32(arr[1]);

            System.Net.IPAddress address;
            if (!System.Net.IPAddress.TryParse(useIP, out address))
            {
                throw new Exception("无效IP");
            }
            HslDevice.IpAddress = useIP;
            HslDevice.Port      = usePort;
            HslDevice.ConnectClose();
            try
            {
                HslDevice.ConnectTimeOut = ConnectTimeOut;
                HslDevice.ReceiveTimeOut = ReadTimeOut;
                OperateResult r = HslDevice.ConnectServer();
                if (!r.IsSuccess)
                {
                    throw new Exception("连接失败。");
                }
            }
            catch
            {
                HslDevice.ConnectClose();
                throw;
            }
        }
예제 #9
0
        /// <summary>
        /// 实例化一个数据通信的对象,需要指定访问哪种Plc
        /// </summary>
        /// <param name="siemens"></param>
        public SiemensTcpNet(SiemensPLCS siemens)
        {
            PortRead   = 102;
            PortWrite  = 102;
            CurrentPlc = siemens;

            switch (siemens)
            {
            case SiemensPLCS.S1200: plcHead1[18] = 1; break;

            case SiemensPLCS.S300: plcHead1[18] = 2; break;

            default: plcHead1[18] = 3; break;
            }
        }
예제 #10
0
        public void ConnectToPlc()
        {
            siemensPLCS = SiemensPLCS.S1200;
            switch (_device.Type)
            {
            case PLCType.Simens1200:
                siemensPLCS = SiemensPLCS.S1200;
                break;

            case PLCType.Simens1500:
                siemensPLCS = SiemensPLCS.S1500;
                break;

            case PLCType.Simens300:
                siemensPLCS = SiemensPLCS.S300;
                break;

            case PLCType.Simens200Smart:
                siemensPLCS = SiemensPLCS.S200Smart;
                break;
            }
            deviceDriver?.ConnectClose();
            deviceDriver?.Dispose();
            deviceDriver = new SiemensS7Net(siemensPLCS, _device.Ip)
            {
                ConnectTimeOut = 2000
            };

            //siemensTcpNet.LogNet = LogNet;
            deviceDriver.SetPersistentConnection();   // 设置长连接
            OperateResult res = deviceDriver.ConnectServer();

            if (res.IsSuccess)
            {
                _log.WriteLog($"{_device.Name}{_device.Ip}连接成功");
            }
            else
            {
                _log.WriteLog($"{_device.Name}{_device.Ip}连接失败");
            }
            _device.IsConnected = res.IsSuccess;
        }
예제 #11
0
 public DoraSiemensS7Net(SiemensPLCS siemens) : base(siemens)
 {
 }
예제 #12
0
 /// <summary>
 /// 创建与PLC的通信连接对象,并自动建立长连接
 /// </summary>
 /// <param name="siemens"></param>
 /// <param name="ipAddress"></param>
 /// <param name="plcLog"></param>
 public SiemensPlc(SiemensPLCS siemens, string ipAddress, ILogNet plcLog) : this(siemens, ipAddress)
 {
     this.Plc_Commu_Log = plcLog;
     //建立与PLC的长连接
     Initialization();
 }
예제 #13
0
 /// <summary>
 /// 实例化一个西门子的S7协议的通讯对象
 /// </summary>
 /// <param name="siemens">指定西门子的型号</param>
 public SiemensS7Net(SiemensPLCS siemens)
 {
     Initialization(siemens, string.Empty);
 }
예제 #14
0
 public SiemensS7Net_New(SiemensPLCS siemens, string ipAddress) : base(siemens, ipAddress)
 {
     this.CurrentPlc = siemens;
 }
예제 #15
0
 public SiemensS7Net_New(SiemensPLCS siemens) : base(siemens)
 {
 }
예제 #16
0
 public Siemens(SiemensPLCS siemensPLCS)
 {
     InitializeComponent();
     siemensTcpNet = new SiemensS7Net(siemensPLCS);
 }
예제 #17
0
 /// <summary>
 /// 连接
 /// </summary>
 /// <param name="type"></param>
 /// <param name="host"></param>
 public void Connect(SiemensPLCS type, string host)
 {
     plc.Connect(type, host);
 }
        public void InitializeService(Channel ch)
        {
            try
            {
                //=================================================================

                if (Channels == null)
                {
                    return;
                }

                Channels.Add(ch);


                IPLCS7Adapter DriverAdapter = null;
                foreach (Device dv in ch.Devices)
                {
                    try
                    {
                        switch (ch.ConnectionType)
                        {
                        case "SerialPort":
                            DISerialPort dis = (DISerialPort)ch;
                            SerialPort   sp  = new SerialPort(dis.PortName, dis.BaudRate, dis.Parity, dis.DataBits, dis.StopBits)
                            {
                                Handshake = dis.Handshake
                            };

                            DriverAdapter = new SiemensComPPI(dv.SlaveId, sp);
                            _PLCPPI.Add(ch.ChannelName, (SiemensComPPI)DriverAdapter);
                            break;

                        case "Ethernet":
                            DIEthernet  die = (DIEthernet)ch;
                            SiemensPLCS cpu = (SiemensPLCS)Enum.Parse(typeof(SiemensPLCS), die.CPU);
                            DriverAdapter = new SiemensNet(cpu, die.IPAddress, (short)die.Rack, (short)die.Slot);
                            _PLCS7.Add(ch.ChannelName, (SiemensNet)DriverAdapter);

                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        EventscadaException?.Invoke(GetType().Name, ex.Message);
                    }
                    foreach (DataBlock db in dv.DataBlocks)
                    {
                        DataBlockCollection.DataBlocks.Add($"{ch.ChannelName}.{dv.DeviceName}.{db.DataBlockName}", db);
                        foreach (Tag tg in db.Tags)
                        {
                            TagCollection.Tags.Add(
                                $"{ch.ChannelName}.{dv.DeviceName}.{db.DataBlockName}.{tg.TagName}", tg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                EventscadaException?.Invoke(GetType().Name, ex.Message);
            }
        }
예제 #19
0
 /// <summary>
 /// 实例化一个西门子的S7协议的通讯对象并指定Ip地址
 /// </summary>
 /// <param name="siemens">指定西门子的型号</param>
 /// <param name="ipAddress">Ip地址</param>
 public SiemensS7Net(SiemensPLCS siemens, string ipAddress)
 {
     Initialization(siemens, ipAddress);
 }
예제 #20
0
 public FormSiemens(SiemensPLCS siemensPLCS)
 {
     InitializeComponent();
     siemensTcpNet = new SiemensS7Net(siemensPLCS);
     CheckForIllegalCrossThreadCalls = false;
 }
예제 #21
0
 public FormSiemens(SiemensPLCS siemensPLCS)
 {
     InitializeComponent( );
     siemensPLCSelected = siemensPLCS;
     siemensTcpNet      = new SiemensS7Net(siemensPLCS);
 }
예제 #22
0
        public int Connect()
        {
            SiemensPLCS siemensPLCS;

            switch (DriveConfig.PLCModel.ToUpper())
            {
            case "S1200":
                siemensPLCS = SiemensPLCS.S1200;
                rack        = 0;
                slot        = 0;
                break;

            case "S1500":
                siemensPLCS = SiemensPLCS.S1500;
                rack        = 0;
                slot        = 0;
                break;

            case "S200":
                siemensPLCS = SiemensPLCS.S200;
                break;

            case "S200SMART":
                siemensPLCS = SiemensPLCS.S200Smart;
                break;

            case "S300":
                siemensPLCS = SiemensPLCS.S300;
                rack        = 0;
                slot        = 2;
                break;

            case "S400":
                siemensPLCS = SiemensPLCS.S400;
                rack        = 0;
                slot        = 3;
                break;

            default:
                siemensPLCS = SiemensPLCS.S1200;
                rack        = 0;
                slot        = 0;
                break;
            }
            siemensPLCSelected = siemensPLCS;
            siemensTcpNet      = new SiemensS7Net(siemensPLCS);

            if (!System.Net.IPAddress.TryParse(DriveConfig.IP, out System.Net.IPAddress address))
            {
                throw new Exception("IpAddress input wrong");
            }

            siemensTcpNet.IpAddress = DriveConfig.IP;
            siemensTcpNet.Port      = DriveConfig.Port;
            try
            {
                if (siemensPLCSelected != SiemensPLCS.S200Smart)
                {
                    siemensTcpNet.Rack = (byte)rack;
                    siemensTcpNet.Slot = (byte)slot;
                }
                OperateResult connect = siemensTcpNet.ConnectServer();
                if (!connect.IsSuccess)
                {
                    throw new Exception("Connect Failed");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(0);
        }