当前位置:网站首页>C#西门子S7 协议通过偏移量的方式读写PLC DB块
C#西门子S7 协议通过偏移量的方式读写PLC DB块
2022-07-30 15:33:00 【小丫头呀】
1.例如,当前plc 有一个db块,需要我们对该db块进行读写操作,博图要连接Plc编译后才会显示偏移量的,所以这里就没有显示偏移量。
正常编译后是这样的,有显示偏移量的
1.1 根据偏移量读第一个bool 数据类型
上一章节讲过,是通过安装s7驱动包来处理的,这里就不再累述了
首先要注意一点,只要是通过偏移量的形式进行读写,无论是什么数据类型,都是要进行转换的。例如:读取bool 数据类型,要这样子进行转换,才能读取的到值。
Convert.ToBoolean(plc.Read("DB20.DBX1.7"))
- DB20 就是西门子建立的DB块
- DBX 是读写bool类型固定的写法,例如bool类型就是DBX,int 类型就是DBW,这个s7的文档里面就有介绍的。
- 1.7 就是第一个bool类型设置的偏移量了
以上就实现了读指定Bool 类型的偏移量
1.2 根据偏移量写一个bool 数据类型
写也是同样的道理,因为bool 类型无非就是false或true,所以直接写0或1过去, plc会自动转换成对应的false 或true了。例如:
plcs.Write("DB20.DBX1.7", 1);
- 这样就实现了把DB20偏移量是1.7 这个bool类型的值改成true了
1.3 根据偏移量 读一个 int 数据类型
首先,读int类型也要进行转换。例如,当前int 类型在plc里面的偏移量是10,那么程序里读的时候就要这样处理,把它强转成ushort 类型,
short num=((ushort)plcs.Read("DB20.DBW10")).ConvertToShort();
- 这样进行转换后,就能读取到指定 int 类型的偏移量了
- DBW 是读写 int 数据类型固定的写法
1.4 根据偏移量 写一个 int 数据类型
写的数据也要转换成ushort,首先要定义一个short变量,赋值 ,然后转换成ushort。
short PickNums = 20;
plc.Write("DB20.DBW10", PickNums .ConvertToUshort());
- 要转换成ushort才能最终写成功的了
其他类型例如byte,可以通过s7 plc 驱动包 提供的内置方法,根据文档进行读写操作了。方式都大同小异 ,认真看文档就会了,特别简单。
2. plc 必须开启这些设置,才能最终读写成功。
2.1 取消优化块访问的设置
弹出警告框时,点击确认
2.2 访问级别,设置成允许
2.3 连接机制,勾选允许来自远程对象的通讯访问
边栏推荐
- Flask之路由(app.route)详解
- Flask入门学习教程
- Shell脚本的概念
- [HMS core] [FAQ] A collection of typical questions about push kit, analysis services, and video editing services 3
- 一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
- 70 lines of code, a desktop automatic translation artifact
- 几种常见的存储器
- LeetCode-283-移动零
- flask获取post请求参数
- Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
猜你喜欢
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
【C语言】指针和数组的深入理解(第二期)
动态规划 --- 状态压缩DP 详细解释
php how to query string occurrence position
How to intercept the first few digits of a string in php
路遇又一个流量风口,民宿长期向好的逻辑选对了吗
LeetCode-283-移动零
Flask之路由(app.route)详解
How to remove last character from string in php
【HMS core】【FAQ】push kit、分析服务、视频编辑服务典型问题合集3
随机推荐
70 lines of code, a desktop automatic translation artifact
Introduction to golang image processing library image
Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
工具| execsnoop 短时进程追踪工具
Databases - create databases, tables, functions, etc.
LeetCode-283-移动零
How to intercept the first few digits of a string in php
数组元素逆置
Redis 复习计划 - Redis 数据结构和持久化机制
Image information extraction DEM
ISELED---the new choice of ambient lighting scheme
Recent learning defragmentation (24)
Store Limit usage documentation
围绕用户思维,木鸟与途家如何实现乡村民宿下的用户运营
L2-007 家庭房产(vector、set、map的使用)
rhce笔记1
Pytorch 训练技巧
RobotStudio实现喷漆、打磨等功能(曲面路径生成与仿真)
TiDB tool download
481-82 (105, 24, 82, 34, 153),