使用S7.net通信库,可以不使用任何功能块,直接用C# 访问西门子PLC

配置文件: 记得用ANSI格式。因为微软的库默认ANSI

[配置信息]
IP地址=192.168.1.198
CPU类型=S71500
存储周期=10
自动存储=0

读取配置文件的工具类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace IniHelper
{public class IniConfigHelper{#region API函数声明[DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key,string val, string filePath);//需要调用GetPrivateProfileString的重载[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]private static extern long GetPrivateProfileString(string section, string key,string def, StringBuilder retVal, int size, string filePath);[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]private static extern uint GetPrivateProfileStringA(string section, string key,string def, Byte[] retVal, int size, string filePath);#endregion#region 读Ini文件public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath){if (File.Exists(iniFilePath)){StringBuilder temp = new StringBuilder(1024);GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);return temp.ToString();}else return String.Empty;}#endregion#region 写Ini文件public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath){long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);if (OpStation == 0)return false;else return true;}#endregion}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace IniHelper
{public class SettingManager{public SettingManager(string path)//ctor 构造方法快捷键{this.Path = path;}private string _path;SysSettings sysSettings = new SysSettings();public string Path { get => _path; set => _path = value; }//cirl+R+Epublic SysSettings LoadSysSettings(){sysSettings.IpAdress = IniConfigHelper.ReadIniData("配置信息", "IP地址", "1", _path);sysSettings.CpuType = IniConfigHelper.ReadIniData("配置信息", "CPU类型", "S71200", _path);sysSettings.StoreTime = IniConfigHelper.ReadIniData("配置信息", "存储周期", "10", _path);sysSettings.AutoSore = IniConfigHelper.ReadIniData("配置信息", "自动存储", "0", _path);return sysSettings;//try//{//}//catch (Exception)//{//    return null;//}//ConfigFile}//SysSettings sysSettings = new SysSettings();public bool SaveSysSettings(SysSettings sysSettings){bool result = true;result &= IniConfigHelper.WriteIniData("配置信息","IP地址",sysSettings.IpAdress, _path);result &= IniConfigHelper.WriteIniData("配置信息", "CPU类型", sysSettings.CpuType, _path);result &= IniConfigHelper.WriteIniData("配置信息", "存储周期", sysSettings.StoreTime.ToString(), _path);result &= IniConfigHelper.WriteIniData("配置信息", "自动存储", sysSettings.AutoSore.ToString(), _path);return result;}}
}

PLC的信息类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace IniHelper
{public class SysSettings{public string IpAdress { get; set; }public string CpuType { get; set; }public string StoreTime { get; set; }public string AutoSore { get; set; }}
}

定义15个ushort变量,对应1500里15个word变量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace IniPractice
{public class CommunicationState{//建立15个变量,1500PLC使用word发送public ushort SatusValue { get; set; }public ushort LimitLeft { get; set; }public ushort RightLeft { get; set; }public ushort LimitOriginal { get; set; }public ushort RunState { get; set; }public ushort CurrentSpeed { get; set; }public ushort CurrentPosition { get; set; }public ushort SpeedSet1 { get; set; }public ushort SpeedSet2 { get; set; }public ushort SpeedSet3 { get; set; }public ushort SpeedSet4 { get; set; }public ushort SpeedSet5 { get; set; }public ushort SpeedSet6 { get; set; }public ushort SpeedSet7 { get; set; }public ushort SpeedSet8 { get; set; }}
}

建立通信类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using IniHelper;
using S7.Net;
using thinger.cn.DataConvertHelper;namespace IniPractice
{public class CoreLogicManager{private Plc simensS7 = null;//public int MyProperty { get; set; }public CommunicationState CurentState { get; set; } = new CommunicationState();private CancellationTokenSource cts = new CancellationTokenSource();//连接public bool ConnectPLC(SysSettings sysSettings){try{simensS7 = new Plc((CpuType)Enum.Parse(typeof(CpuType), sysSettings.CpuType), sysSettings.IpAdress, 0,0);simensS7.Open();}catch (Exception){return false;}//采集 多线程执行Task.Run(() =>{PLCCommucation();}, cts.Token);return true;}private void PLCCommucation(){while (!cts.IsCancellationRequested){byte[] result = simensS7.ReadBytes(S7.Net.DataType.DataBlock, 1, 0,30);//起始地址,数量30//数据解析 截取字节数组,转换为需要的值if (result!=null && result.Length==30){CurentState.SatusValue = UShortLib.GetUShortFromByteArray(result, 0);//状态值 偏移量CurentState.LimitLeft = UShortLib.GetUShortFromByteArray(result, 2);//状态值CurentState.RightLeft = UShortLib.GetUShortFromByteArray(result, 4);//状态值CurentState.LimitOriginal = UShortLib.GetUShortFromByteArray(result, 6);//状态值CurentState.RunState = UShortLib.GetUShortFromByteArray(result, 8);//状态值//CurentState.SatusValue = ByteLib.GetByteFromByteArray(result,0);//状态值//CurentState.LimitLeft = BitLib.GetBitFromByteArray(result, 20, 5);//20个字节第5个位}}}//断开private void PLCClose(){if (cts!=null){cts.Cancel();}if(simensS7!=null){simensS7.Close();}}}
}

主窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IniHelper;
using S7.Net;namespace IniPractice
{public partial class FrmMain : Form{public FrmMain(){InitializeComponent();this.SetsManager = new SettingManager(Application.StartupPath + "\\config.ini");this.SysSets = SetsManager.LoadSysSettings();}//声明2个对象 private SysSettings _sysSets;private SettingManager _setsManager;public SysSettings SysSets { get => _sysSets; set => _sysSets = value; }public SettingManager SetsManager { get => _setsManager; set => _setsManager = value; }private void button1_Click(object sender, EventArgs e){FrmCommonSet frmCommonSet = new FrmCommonSet(_setsManager, _sysSets);//对外传值用属性frmCommonSet.Show();}}
}

通信窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IniHelper;namespace IniPractice
{public partial class FrmCommonSet : Form{public FrmCommonSet(SettingManager settingManager, SysSettings sysSettings){InitializeComponent();this.SetsManager = settingManager;this.SysSets = sysSettings;if (sysSettings!=null){this.txtAutoSore.Text = SysSets.AutoSore.Trim();this.txtIpAdress.Text = SysSets.IpAdress.Trim();this.txtStoreTime.Text = SysSets.StoreTime.Trim();this.cmbConfig.Text = SysSets.CpuType.Trim();}}//声明2个对象private SysSettings _sysSets;private SettingManager _setsManager;private CoreLogicManager PlcConnect = new CoreLogicManager();public SysSettings SysSets { get => _sysSets; set => _sysSets = value; }public SettingManager SetsManager { get => _setsManager; set => _setsManager = value; }private void btnSave_Click(object sender, EventArgs e){}private void btnSet_Click(object sender, EventArgs e){SysSets.IpAdress = this.txtIpAdress.Text.Trim();SysSets.CpuType = this.cmbConfig.Text.ToString();           this.cmbConfig.Text.Trim();SysSets.StoreTime = this.txtStoreTime.Text.Trim();SysSets.AutoSore = this.txtAutoSore.Text.Trim();bool result = SetsManager.SaveSysSettings(SysSets);if (result){MessageBox.Show("配置信息存储成功!","配置存储");}else{MessageBox.Show("配置信息存储失败!", "配置存储");}}private void FrmCommonSet_Load(object sender, EventArgs e){//cmbConfig.Items.Add("S7200");//0//cmbConfig.Items.Add("Logo0BA8");//1//cmbConfig.Items.Add("S7200Smart");//2//cmbConfig.Items.Add("S7300");//10//cmbConfig.Items.Add("S7400");//20//cmbConfig.Items.Add("S71200");//30//cmbConfig.Items.Add("S71500");//40cmbConfig.Items.AddRange(new string[] {"S7200", "Logo0BA8", "S7200Smart","S7300","S7400","S71200","S71500"});}private void btnConnect_Click(object sender, EventArgs e){bool res = PlcConnect.ConnectPLC(_sysSets);if (res){MessageBox.Show("PLC连接成功");this.label1.Text = PlcConnect.CurentState.SatusValue.ToString();this.label2.Text = PlcConnect.CurentState.LimitLeft.ToString();this.label3.Text = PlcConnect.CurentState.RightLeft.ToString();}else{MessageBox.Show("PLC连接失败");}}}
}


使用S7.net读取西门子1500PLC相关推荐

  1. C#通过S7.net读取西门子300PLC的数据

    这里写自定义目录标题 C#通过S7.net读取西门子300PLC的数据 一.连接PLC 二.读取数据 三.无实物PLC,离线模拟读取 四.注意事项 1.NetToPLCsim软件 2.S7-PLCSI ...

  2. PLC实验—西门子S7 1200读取旋转编码器数据并计算电机转速

    PLC实验-西门子S7 1200读取旋转编码器数据并计算电机转速 注意PTO控制步进电机实验博途软件需要V14版本,不然没有PTO功能块 软件的下载请点击下方百度网盘的链接 链接:https://pa ...

  3. C# 读取西门子S7系列PLC教程及源码

    创建 PLC 实例,连接和断开连接 若要创建驱动程序的实例,需要使用此构造函数: public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot) ...

  4. 基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示

    基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示 测试环境:  TIA portal V17(已安装PLCSIM Advanced V3.0)  VisualS ...

  5. 隔离型串口服务器和西门子1500PLC和通讯案例

    一.设备搭建 起始搭建环境为无锡安泰起重量限制器.隔离型串口服务器ZLAN5143I.西门子1513 485 在工业历史长河中扮演的重要的角色,随着工业体系的加快,485 慢慢的被其他的通讯接口所替代 ...

  6. php读取西门子plc_AB PLC和西门子PLC之间需要交换数据

    场景:一个工控人,他接到一个项目,在微信群里交流起来,AB1769的PLC和西门子1500的PLC进行数据,让我帮他出个方案: 应用难点:通讯协议不同,处于不同IP段,PLC无源程序修改 方案:使用G ...

  7. JAVA采用S7通信协议访问西门子PLC

    简介 采用java的方式实现西门子S7协议 链接地址:iot-communication github: https://github.com/xingshuangs/iot-communicatio ...

  8. Java使用S7协议连接西门子PLC1200、1500

    Java使用S7协议连接西门子PLC1200.1500 1.引入s7包 2.测试代码(可参考使用) 1.引入s7包 使用 https://github.com/s7connector/s7connec ...

  9. 西门子1500PLC大型项目程序 ,气缸,通讯,机械手,模拟量等,各种FB块

    西门子1500PLC大型项目程序 ,气缸,通讯,机械手,模拟量等,各种FB块,可用来参考和学习 软件博图,威纶通触摸屏,网络结构可参考图一,PTO控制20多个轴,100多个气缸,控制2台机器人. 5台 ...

最新文章

  1. 听完411头猪的哼哼,他们找到了理解“猪语”的算法 | Scientific Reports
  2. Wonder 1.0 正式版发布,WebGL 3D引擎和编辑器
  3. Exchange2003管理
  4. LeetCode Golang 9.回文数
  5. C#经典算法实践,回顾往生,更是致敬《算法导论》
  6. eclipse - 自动换行
  7. Win8 Style App 播放Smooth Streaming
  8. VS2013环境下GSL数学库的使用说明(亲测)
  9. 泊松分布、二项分布与正态分布
  10. 高级计算机器,高级计算器最新版
  11. DMZ区域的作用与原理
  12. 互联网晚报 |10/12 |中国汽车出口量跃居全球第二;统一充电接口或让苹果每年损失百亿;《财富》杂志公布“改变世界的公司”榜单...
  13. SQL Server 中“dbo”到底是什么
  14. CSS实现兼容浏览器的文字阴影效果
  15. javaScript 对象大全 (javascript code al 2)(转转)
  16. python(25)- 面向对象补充Ⅰ
  17. 聊一聊搭建一个网站到底有几步?
  18. 用Java实现一个简易的植物大战僵尸游戏
  19. Ubuntu Unable to locate package bulld-essential
  20. 我是怎么和SAP结缘的 - Jerry的SAP校园招聘之路

热门文章

  1. 未来土地利用模拟FLUS模型
  2. 程序员从一线城市撤回到老家之后,不妨考虑一下做IT培训
  3. 经纬度坐标转为度分秒
  4. 《现代控制理论》刘豹 第一章重要知识点
  5. 211130-Python谱图(Spectogram)分析Demo
  6. 连上wifi却无法上网
  7. smali语法添加弹窗
  8. C/C++ 相关低耦合代码的设计
  9. php实现mysql分表,php实现的mysql分表方案(水平切分)
  10. 苹果拍照怎么显示地点和时间_内部秘密中医体质辨识与调理师证报名时间怎么报考考试地点...