using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Sharp7;namespace Sharp7Example{ class Program { static void Main(string[] args) { //-------------- Create and connect the client var client = new S7Client(); int result = client.ConnectTo("127.0.0.1", 0, 1); if (result == 0) { Console.WriteLine("Connected to 127.0.0.1"); } else { Console.WriteLine(client.ErrorText(result)); Console.ReadKey(); return; } //-------------- Read db1 Console.WriteLine("\n---- Read DB 1"); byte[] db1Buffer = new byte[18]; result = client.DBRead(1, 0, 18, db1Buffer); if (result != 0) { Console.WriteLine("Error: " client.ErrorText(result)); } int db1dbw2 = S7.GetIntAt(db1Buffer, 2); Console.WriteLine("DB1.DBW2: " db1dbw2); double db1dbd4 = S7.GetRealAt(db1Buffer, 4); Console.WriteLine("DB1.DBD4: " db1dbd4); double db1dbd8 = S7.GetDIntAt(db1Buffer, 8); Console.WriteLine("DB1.DBD8: " db1dbd8); double db1dbd12 = S7.GetDWordAt(db1Buffer, 12); Console.WriteLine("DB1.DBD12: " db1dbd12); double db1dbw16 = S7.GetWordAt(db1Buffer, 16); Console.WriteLine("DB1.DBD16: " db1dbw16); //-------------- Read DB3 Console.WriteLine("\n---- Read DB 3"); byte[] db3Buffer = new byte[18]; result = client.DBRead(3, 0, 18, db3Buffer); if (result != 0) { Console.WriteLine("Error: " client.ErrorText(result)); } int db3dbw2 = S7.GetIntAt(db3Buffer, 2); Console.WriteLine("DB3.DBW2: " db3dbw2); double db3dbd4 = S7.GetRealAt(db3Buffer, 4); Console.WriteLine("DB3.DBD4: " db3dbd4); double db3dbd8 = S7.GetDIntAt(db3Buffer, 8); Console.WriteLine("DB3.DBD8: " db3dbd8); uint db3dbd12 = S7.GetDWordAt(db3Buffer, 12); Console.WriteLine("DB3.DBD12: " db3dbd12); ushort db3dbd16 = S7.GetWordAt(db3Buffer, 16); Console.WriteLine("DB3.DBD16: " db3dbd16); //-------------- Write Db1 Console.WriteLine("\n---- Write BD 1"); db1Buffer = new byte[12]; const int START_INDEX = 4; S7.SetRealAt(db1Buffer, 4 - START_INDEX, (float)54.36); S7.SetDIntAt(db1Buffer, 8 - START_INDEX, 555666); S7.SetDWordAt(db1Buffer, 12 - START_INDEX, 123456); result = client.DBWrite(1, START_INDEX, db1Buffer.Length, db1Buffer); if (result != 0) { Console.WriteLine("Error: " client.ErrorText(result)); } //-------------- Read multi vars var s7MultiVar = new S7MultiVar(client); byte[] db1 = new byte[18]; s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 1, 0, db1.Length, ref db1); byte[] db3 = new byte[18]; s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 3, 0, db3.Length, ref db3); result = s7MultiVar.Read(); if (result != 0) { Console.WriteLine("Error on s7MultiVar.Read()"); } db1dbw2 = S7.GetIntAt(db1, 2); Console.WriteLine("DB1.DBW2.0 = {0}", db1dbw2); db1dbd4 = S7.GetRealAt(db1, 4); Console.WriteLine("DB1.DBW4.0 = {0}", db1dbd4); db1dbd8 = S7.GetDIntAt(db1, 8); Console.WriteLine("DB1.DBW8.0 = {0}", db1dbd8); db3dbw2 = S7.GetIntAt(db3, 2); Console.WriteLine("DB3.DBW2.0 = {0}", db3dbw2); db3dbd4 = S7.GetRealAt(db3, 4); Console.WriteLine("DB3.DBW4.0 = {0}", db3dbd4); db3dbd8 = S7.GetDIntAt(db3, 8); Console.WriteLine("DB3.DBW8.0 = {0}", db3dbd8); //-------------- Write multi vars s7MultiVar = new S7MultiVar(client); const int DB1_START_INDEX = 2; db1 = new byte[10]; S7.SetIntAt(db1, 2 - DB1_START_INDEX, 50); S7.SetRealAt(db1, 4 - DB1_START_INDEX, (float)36.5); S7.SetDIntAt(db1, 8 - DB1_START_INDEX, 123456); s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 1, DB1_START_INDEX, db1.Length, ref db1); const int DB3_START_INDEX = 2; db3 = new byte[10]; S7.SetIntAt(db3, 2 - DB3_START_INDEX, -50); S7.SetRealAt(db3, 4 - DB3_START_INDEX, (float)-25.36); S7.SetDIntAt(db3, 8 - DB3_START_INDEX, -123456); s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 3, DB3_START_INDEX, db3.Length, ref db3); result = s7MultiVar.Write(); if (result != 0) { Console.WriteLine("Error on s7MultiVar.Read()"); } //-------------- Disconnect the client client.Disconnect(); Console.ReadKey(); } }}
下载C#与西门子S7用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论