springboot 通过博途获取plc点位的数据代码实现
作者:小果子^_^
这篇文章主要介绍了springboot 通过博途获取plc点位的数据的代码实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
springboot 通过博途获取plc点位的数据
1.maven依赖
<dependency> <groupId>com.github.dathlin</groupId> <artifactId>HslCommunication</artifactId> <version>3.6.0</version> </dependency>
这个版本尽量是新版本,不然有的返回的值对应的类型不存在
2.代码实现
public void sendHttpRequest() { System.out.println("==================================执行命令==================================" + System.currentTimeMillis()); List<SysPlcIpEntity> ipList = plcIpDao.getIpList(); if (ipList != null && ipList.size() > 0) { for (SysPlcIpEntity plcIp:ipList) { SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, plcIp.getIpAddress()); siemensS7Net.SetPersistentConnection( ); // 根据ip查询对应的命令 List<SysPlcMlEntity> plcMlList = plcMlDao.getListByIpId(plcIp.getId()); if (plcMlList != null && plcMlList.size() > 0) { for (int i = 0; i < plcMlList.size(); i++) { if (plcMlList.get(i).getMlContent().contains("DBW")) { OperateResultExOne<Short> dbwVal = siemensS7Net.ReadInt16(plcMlList.get(i).getMlContent()); if (dbwVal.IsSuccess){ plcMlList.get(i).setMlValue(String.valueOf(dbwVal.Content)); } } else if (plcMlList.get(i).getMlContent().contains("DBD")){ OperateResultExOne<Float> dbdVal = siemensS7Net.ReadFloat(plcMlList.get(i).getMlContent()); if (dbdVal.IsSuccess) { plcMlList.get(i).setMlValue(String.valueOf(dbdVal.Content)); } } else if (plcMlList.get(i).getMlContent().contains("DBX")) { OperateResultExOne<Boolean> dbxVal = siemensS7Net.ReadBool(plcMlList.get(i).getMlContent()); if (dbxVal.IsSuccess) { plcMlList.get(i).setMlValue(String.valueOf(dbxVal.Content)); } } } plcMlDao.batchUpdateMlList(plcMlList); } } } System.out.println("==================================结束命令==================================" + System.currentTimeMillis()); }
因为需求,DBW是整型,DBD是浮点数,DBX是布尔值DBW/D/X后面对应的是偏移量,
3. 测试方法
public static void main(String[] args) { // DB5.DBX8.5 SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, "192.168.1.105"); siemensS7Net.SetPersistentConnection( ); //DBD OperateResultExOne<float[]> read = siemensS7Net.ReadFloat( "DB3.DBD10", (short) 4); if (read.IsSuccess) { for (float f: read.Content) { System.out.println("dbd:" + f); } } //DBW Short content = siemensS7Net.ReadInt16("DB3.DBW0").Content; System.out.println("content" + content); OperateResultExOne<Short> long_array = siemensS7Net.ReadInt16("DB3.DBW0"); if (long_array.IsSuccess){ System.out.println("dbw:" + long_array.Content); } // DBX /* OperateResultExOne<boolean[]> operateResultExOne = siemensS7Net.ReadBool("DB5.0.1", (short) 4); if (operateResultExOne.IsSuccess) { for (Boolean boo:operateResultExOne.Content) { System.out.println("------------"+boo); } } else { System.out.println("错误信息:"+operateResultExOne.ErrorCode + "=====" + operateResultExOne.Message); }*/ }
(short)对应的值是获取几位,因为需求,没办法一起获取,所以我是单个获取的
到此这篇关于springboot 通过博途获取plc点位的数据的文章就介绍到这了,更多相关springboot 获取plc点位数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!