In projects, the host computer often needs to interface with PLC communication. Today, we will introduce the commonly used S7.NET communication for PLCs. In practical applications, the common control methods are as follows: one PLC controls one device, one PLC controls multiple devices, and multiple PLCs control multiple devices. Similarly, the host computer also needs to implement batch connections and batch read/write functions with the PLC.Note: The host computer can establish a maximum of 8 connections with the PLC at one time.1. Reference
using HslCommunication.Profinet.Siemens;using HslCommunication
2. Establishing PLC Connection
public SiemensS7Net siemensTcpNet { get; set; }string devIP = "192.168.20.107";int devPort = 102;bool isOpen;string plcType = "";//Initializationpublic Init(string ip, int port,string type){ devIP = ip; devPort = port; plcType = type;}//Openpublic void TurnOn(){ try { if (isOpen) return; if (string.IsNullOrEmpty(plcType)) { siemensTcpNet = new SiemensS7Net(SiemensPLCS.S400, devIP); } else { if (plcType == "200") { siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200, devIP); siemensTcpNet.Slot = 3; } if (plcType == "1200") { siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200, devIP); siemensTcpNet.Slot = 1; } } siemensTcpNet.Rack = 0; siemensTcpNet.ConnectTimeOut = 5000; OperateResult connect = siemensTcpNet.ConnectServer(); if (connect.IsSuccess == false) { throw new Exception("PLC startup failed" + connect.Message); } isOpen = true; } catch (Exception ex) { isOpen = false; throw new Exception(ex.Message); }}//Closepublic void TurnOff(){ try { isOpen = false; siemensTcpNet.ConnectClose(); siemensTcpNet.Dispose(); } catch (Exception) { }}
3. PLC Point Read/Write<span><span>Write PLC Data</span></span>
//float typepublic void SendPLCDataComm(string address, float value){ try { OperateResult result = siemensTcpNet.Write(address, value); if (result.IsSuccess == false) { throw new Exception("Write value failed!"); } } catch (Exception ex) { throw new Exception(ex.Message); }}//int type public void SendPLCDataCommint(string address, int value){ try { OperateResult result = siemensTcpNet.Write(address, value); if (result.IsSuccess == false) { throw new Exception("Write value failed!"); } } catch (Exception ex) { throw new Exception(ex.Message); }}//bool typepublic void SendPLCDataCommbool(string address, bool value){ try { OperateResult result = siemensTcpNet.Write(address, value); if (result.IsSuccess == false) { throw new Exception("Write value failed!"); } } catch (Exception ex) { throw new Exception(ex.Message); }}
ReadPLC Data
//float typepublic void GetPLCDataComm(string address, out float value){ try { OperateResult<float> result = siemensTcpNet.ReadFloat(address); if (result.IsSuccess == false) throw new Exception(result.Message); value = result.Content; } catch (Exception ex) { throw new Exception(ex.Message); }}//int type public void GetPLCDataCommBool(string address, out bool value){ try { OperateResult<bool> result = siemensTcpNet.ReadBool(address); if (result.IsSuccess == false) throw new Exception(result.Message); value = result.Content; } catch (Exception ex) { throw new Exception(ex.Message); }}//bool typepublic void GetPLCDataCommInt(string address, out int value){ try { OperateResult<int> result = siemensTcpNet.ReadInt32(address); if (result.IsSuccess == false) throw new Exception(result.Message); value = result.Content; } catch (Exception ex) { throw new Exception(ex.Message); }}