springboot 通过博途获取plc点位的数据
- maven依赖
这个版本尽量是新版本,不然有的返回的值对应的类型不存在<dependency> <groupId>com.github.dathlin</groupId> <artifactId>HslCommunication</artifactId> <version>3.6.0</version> </dependency>
- 代码实现
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)对应的值是获取几位,因为需求,没办法一起获取,所以我是单个获取的