Browse Source

plc通讯优化

炒锅机器人_一拖多
pry 4 months ago
parent
commit
a111b9215d
5 changed files with 150 additions and 0 deletions
  1. +6
    -0
      .idea/git_toolbox_blame.xml
  2. +4
    -0
      app/src/main/java/com/bonait/bnframework/HBL/Communication/Modbus/ExceptionServer.java
  3. +114
    -0
      app/src/main/java/com/bonait/bnframework/business/PlcReadHelper.java
  4. +21
    -0
      app/src/main/java/com/bonait/bnframework/business/ReadModel.java
  5. +5
    -0
      app/src/main/java/com/bonait/bnframework/newui/activity/MainHActivity.java

+ 6
- 0
.idea/git_toolbox_blame.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxBlameSettings">
<option name="version" value="2" />
</component>
</project>

+ 4
- 0
app/src/main/java/com/bonait/bnframework/HBL/Communication/Modbus/ExceptionServer.java View File

@@ -32,6 +32,10 @@ public class ExceptionServer {
}
}

public ExceptionServer(){

}

public int GetAddress(String address) {
MessageLog.ShowInfo("GetAddress address="+address);
if (address == null) return -1;


+ 114
- 0
app/src/main/java/com/bonait/bnframework/business/PlcReadHelper.java View File

@@ -0,0 +1,114 @@
package com.bonait.bnframework.business;

import com.bonait.bnframework.HBL.Communication.Modbus.ExceptionServer;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.mode.Res_PLCADDRESS;
import com.bonait.bnframework.modbus.WokModbusTcpServer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PlcReadHelper {
public PlcReadHelper(){

}

public LinkedHashMap<String,Object> PlcStatus=new LinkedHashMap<>();
LinkedHashMap<String,LinkedHashMap<Integer,List<ReadModel>>> addGroup =new LinkedHashMap<>();
public void Init(){
List<Res_PLCADDRESS> PlcAdd = ConfigName.getInstance().PlcAddress_Wok.stream().filter(p->p.isread==1).collect(Collectors.toList());
if(PlcAdd!=null && PlcAdd.size()>0){
//地址转换对象
ExceptionServer es = new ExceptionServer();

//Bool数据类型分组
List<Res_PLCADDRESS> boolData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("M")).collect(Collectors.toList());
if(boolData!=null&&boolData.size()>0){
List<ReadModel> boolReadModel = new ArrayList<>();
boolData.forEach(item->{ boolReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));});
addGroup.put("M",groupBy(boolReadModel));
}

//Short数据类型分组
List<Res_PLCADDRESS> shortData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("VW")).collect(Collectors.toList());
if(shortData!=null&&shortData.size()>0){
List<ReadModel> shortReadModel = new ArrayList<>();
shortData.forEach(item->{ shortReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));});
addGroup.put("VW",groupBy(shortReadModel));
}

//int 数据类型分组
List<Res_PLCADDRESS> intData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("VD")).collect(Collectors.toList());
if(intData!=null&&intData.size()>0){
List<ReadModel> intReadModel = new ArrayList<>();
intData.forEach(item->{ intReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));});
addGroup.put("VD",groupBy(intReadModel));
}

//float数据类型分组
List<Res_PLCADDRESS> floatData = PlcAdd.stream().filter(p->p.address.toUpperCase().contains("VR")).collect(Collectors.toList());
if(floatData!=null&&floatData.size()>0){
List<ReadModel> floatReadModel = new ArrayList<>();
floatData.forEach(item->{ floatReadModel.add(new ReadModel(item.name,item.address,es.GetAddress(item.address)));});
addGroup.put("VR",groupBy(floatReadModel));
}
}
}

public void read(){
for(Map.Entry<String,LinkedHashMap<Integer,List<ReadModel>>> item:addGroup.entrySet()){
for(Map.Entry<Integer,List<ReadModel>> add:addGroup.get(item.getKey()).entrySet()){
if (item.getKey().startsWith("VD"))//int
{
WokModbusTcpServer.get().ReadInt(add.getValue().get(0).plcAddress, add.getValue().size(), ints -> {
for (int i = 0; i < ints.length; i++) {
PlcStatus.put(add.getValue().get(i).name,ints[i]);
}
});
} else if (item.getKey().startsWith("M"))//bool
{
WokModbusTcpServer.get().ReadBool(add.getValue().get(0).plcAddress, add.getValue().size(), val -> {
for (int i = 0; i < val.length; i++) {
PlcStatus.put(add.getValue().get(i).name,val[i]);
}
});
} else if (item.getKey().startsWith("VW"))//short
{
WokModbusTcpServer.get().ReadShort(add.getValue().get(0).plcAddress, add.getValue().size(), val -> {
for (int i = 0; i < val.length; i++) {
PlcStatus.put(add.getValue().get(i).name,val[i]);
}
});
} else if (item.getKey().startsWith("VR"))//float
{
WokModbusTcpServer.get().ReadFloat(add.getValue().get(0).plcAddress, add.getValue().size(), val -> {
for (int i = 0; i < val.length; i++) {
PlcStatus.put(add.getValue().get(i).name,val[i]);
}
});
}
}
}
}

public LinkedHashMap<Integer,List<ReadModel>> groupBy(List<ReadModel> data){
if(data==null) return new LinkedHashMap<>();
Collections.sort(data, Comparator.comparingInt(ReadModel::getModbusAddress));//进行排序
LinkedHashMap<Integer,List<ReadModel>> result = new LinkedHashMap<>();
int groupNum=0;
for(ReadModel value:data){
if(!result.containsKey(groupNum) || result.get(groupNum).get(result.get(groupNum).size()-1).getModbusAddress()+1!=value.getModbusAddress()){
groupNum++;
}
if(!result.containsKey(groupNum))result.put(groupNum,new ArrayList<>());
result.get(groupNum).add(value);
}
return result;
}

}

+ 21
- 0
app/src/main/java/com/bonait/bnframework/business/ReadModel.java View File

@@ -0,0 +1,21 @@
package com.bonait.bnframework.business;

public class ReadModel {
public String name;
public String plcAddress;
private int modbusAddress;

public ReadModel(String name, String plcAddress, int modbusAddress) {
this.name = name;
this.plcAddress = plcAddress;
this.modbusAddress = modbusAddress;
}

public int getModbusAddress() {
return modbusAddress;
}

public void setModbusAddress(int modbusAddress) {
this.modbusAddress = modbusAddress;
}
}

+ 5
- 0
app/src/main/java/com/bonait/bnframework/newui/activity/MainHActivity.java View File

@@ -22,6 +22,7 @@ import com.bonait.bnframework.MainApplication;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.business.ExecuteTheRecipe2;
import com.bonait.bnframework.business.PlcReadHelper;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.file.DBHelper;
@@ -141,6 +142,10 @@ public class MainHActivity extends BaseActivity {
}else {
PreferenceUtils.setInt("loginNum",loginNum+1);
}

// PlcReadHelper plc = new PlcReadHelper();
// plc.Init();
// plc.read();
}

private void init(){


Loading…
Cancel
Save