| | |
| | | <file url="file://$PROJECT_DIR$/src/builder/MachineManager.java" charset="GBK" /> |
| | | <file url="file://$PROJECT_DIR$/src/builder/Manager.java" charset="GBK" /> |
| | | <file url="file://$PROJECT_DIR$/src/builder/S7control.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/builder/S7control.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/builder/S7control.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/builder/S7objectMachine.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/builder/ThreadHttpServer.java" charset="GBK" /> |
| | | <file url="file://$PROJECT_DIR$/src/ng/db/DBSession.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/ng/devices/InitUtil.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/ng/devices/PlcBitInfo.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/ng/devices/PlcBitObject.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/ng/devices/PlcParameterInfo.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/ng/devices/PlcParameterObject.java" charset="UTF-8" /> |
| | | <file url="file://$PROJECT_DIR$/src/toTcp/TCPClient.java" charset="GBK" /> |
| | | <file url="PROJECT" charset="GBK" /> |
| | | </component> |
| | |
| | | public String ProcName; |
| | | public String Name; |
| | | public byte FunctionType; |
| | | public String remarks; |
| | | |
| | | } |
| | | List<item> items=new ArrayList<item>(); |
| | |
| | | if(itm.serv==null){ |
| | | itm.serv=new MysqlService(itm.helper,itm.ID); |
| | | |
| | | itm.serv.Run(itm.ID,itm.IP,itm.Port, (byte)1,itm.ReadOffset, itm.ReadSize,1000,itm.FunctionType); |
| | | itm.serv.Run(itm.ID,itm.IP,itm.Port, (byte)1,itm.ReadOffset, itm.ReadSize,1000,itm.FunctionType,itm.remarks); |
| | | System.out.println(String.format("设备启动-{%s}(%s)(%s)",itm.Name,itm.IP,itm.FunctionType)); |
| | | } |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | public void createDevice(DBHelper helper,int DeviceID,String Name,String Ip,int Port,int ReadOffset,int ReadSize,String ProcName,byte FunctionType){ |
| | | public void createDevice(DBHelper helper,int DeviceID,String Name,String Ip,int Port,int ReadOffset,int ReadSize,String ProcName,byte FunctionType,String remarks){ |
| | | synchronized(items){ |
| | | |
| | | item itm=new item(); |
| | |
| | | itm.ReadOffset=ReadOffset; //读取偏移 |
| | | itm.ReadSize=ReadSize;//发送偏移 |
| | | itm.FunctionType=FunctionType; |
| | | itm.remarks=remarks; |
| | | /*if(DeviceID==6 || DeviceID==19 || DeviceID==29 || DeviceID==30){ |
| | | itm.ReadOffset=12000; |
| | | }*/ |
| | |
| | | //读取设备列表 |
| | | DBHelper db=DBHelper.getDBHelper("mes"); |
| | | sn=DBHelper.getDBHelper("mes").createSession(false); |
| | | sn.createSql("select id,machine_name,ip_address,port,flag,read_size,read_offset,function_type from machine where id>=80"); |
| | | sn.createSql("select id,machine_name,ip_address,port,flag,read_size,read_offset,function_type,remarks from machine where flag=1"); |
| | | ResultSet set= sn.query().getCurrentResult(); |
| | | |
| | | //创建所有设备通讯 |
| | |
| | | int readSize=set.getInt(6); |
| | | int readOffset=set.getInt(7); |
| | | byte functionType=set.getByte(8); |
| | | String remarks=set.getString(9); |
| | | if(flag==1){ |
| | | mmgr.createDevice(db,id,name,ip,port,readOffset,readSize,null,functionType); |
| | | // if(port==102){ |
| | | // if(id==78){ |
| | | // S7control s7=new S7control(EPlcType.S1500, ip, port, 0, 0); |
| | | // System.out.println("S7-S1500:"+s7.readWord("DB34.122", 1)); |
| | | // s7.writeWord("DB34.122",26); |
| | | // System.out.println("S7-S1500:"+s7.readWord("DB34.122", 1)); |
| | | // }else{ |
| | | // S7control s7=new S7control(EPlcType.S1200, ip, port, 0, 0); |
| | | // System.out.println("S7-S1200:"+s7.readWord("DB20.0", 1)); |
| | | // } |
| | | // }else{ |
| | | // |
| | | // } |
| | | |
| | | mmgr.createDevice(db,id,name,ip,port,readOffset,readSize,null,functionType,remarks); |
| | | } |
| | | |
| | | } |
| | |
| | | // TODO: handle exception |
| | | System.out.println("错误"); |
| | | } |
| | | } |
| | | @Override |
| | | public void start() { |
| | | |
| | | } |
| | | |
| | | public static void main(String[] args){ |
New file |
| | |
| | | package builder; |
| | | |
| | | import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; |
| | | import ng.devices.InitUtil; |
| | | import ng.devices.PlcParameterInfo; |
| | | import ng.devices.PlcParameterObject; |
| | | |
| | | import java.util.LinkedHashMap; |
| | | |
| | | |
| | | /** |
| | | * @Author : yanzhixin |
| | | * @Date: 2024/4/9 15:13 |
| | | * @Description: |
| | | */ |
| | | public class S7objectMachine extends Thread { |
| | | public S7control plccontrol; // PLC通讯类实例 |
| | | private EPlcType plcType = EPlcType.S1200; // 西门子PLC类型 |
| | | private String ip = ""; // plc ip地址 |
| | | private int port = 102; // plc 端口号 |
| | | private String plcFileName=""; // json名称 |
| | | public PlcParameterObject PlcMesObject; |
| | | public S7objectMachine(String ip, int port, String plcFileName, EPlcType plcType) { |
| | | initialize(ip,port,plcFileName,plcType); |
| | | if (plccontrol == null) { |
| | | plccontrol = new S7control(this.plcType, this.ip, this.port, 0, 0); |
| | | //String plcFileUrl = System.getProperty("user.dir") + "D:/HangZhouMes/JsonFile/"+this.plcFileName+".json"; |
| | | String plcFileUrl = "D:/mes/JsonFile/"+this.plcFileName+".json"; |
| | | PlcMesObject = InitUtil.initword(plcFileUrl); |
| | | } |
| | | } |
| | | public void initialize(String ip,int port,String plcFileName,EPlcType plcType){ |
| | | this.ip = ip; |
| | | this.port = port; |
| | | this.plcFileName = plcFileName; |
| | | this.plcType= plcType; |
| | | } |
| | | |
| | | /** |
| | | * 打印参数值 |
| | | */ |
| | | public void consoleLogInfo(){ |
| | | String logInfo=this.plcFileName+" "; |
| | | LinkedHashMap<String, PlcParameterInfo> thisPlcParameterInfo=PlcMesObject.getPlcParameterMap(); |
| | | for (String key:thisPlcParameterInfo.keySet()) { |
| | | logInfo+=key+":"+thisPlcParameterInfo.get(key).getValue()+","; |
| | | } |
| | | } |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | Thread.sleep(100); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | byte[] resultValues=new byte[PlcMesObject.getPlcAddressLength()]; |
| | | int maxRead=1092; |
| | | int size=PlcMesObject.getPlcAddressLength()%maxRead==0? |
| | | (PlcMesObject.getPlcAddressLength()/maxRead): |
| | | (PlcMesObject.getPlcAddressLength()/maxRead+1); |
| | | for (int i = 0; i <size ; i++) { |
| | | int begin=i*maxRead; |
| | | int length=(i==size-1?PlcMesObject.getPlcAddressLength()-begin:maxRead); |
| | | String beginAddress=PlcMesObject.getPlcAddressBegin().substring(0, PlcMesObject.getPlcAddressBegin().indexOf("."))+"."+begin; |
| | | byte[] getplcvlues = plccontrol.readByte(beginAddress, length); |
| | | System.arraycopy(getplcvlues,0,resultValues,begin,length); |
| | | } |
| | | if (resultValues != null) { |
| | | PlcMesObject.setPlcParameterList(resultValues); |
| | | } |
| | | } catch (Exception e) { |
| | | //log.info("异常:ip:{},port:{}",this.ip,this.port); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package ng.devices; |
| | | |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.FileReader; |
| | | import java.io.IOException; |
| | | |
| | | public class InitUtil { |
| | | //初始化word |
| | | public static PlcParameterObject initword(String jsonFilePath) { |
| | | try { |
| | | FileReader fileReader = new FileReader(jsonFilePath); |
| | | BufferedReader bufferedReader = new BufferedReader(fileReader); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | String line; |
| | | |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | content.append(line); |
| | | } |
| | | |
| | | bufferedReader.close(); |
| | | fileReader.close(); |
| | | |
| | | JSONObject jsonfileobj = new JSONObject(content.toString()); |
| | | JSONArray jsonArray = jsonfileobj.getJSONArray("parameteInfor"); |
| | | PlcParameterObject plcParameterObject = new PlcParameterObject(); |
| | | plcParameterObject.setPlcAddressBegin(jsonfileobj.getStr("plcAddressBegin"));//设置起始位地址 |
| | | plcParameterObject.setPlcAddressLength(Integer.valueOf(jsonfileobj.getStr("plcAddressLenght")));//设置地址长度 |
| | | |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject parameterObj = jsonArray.getJSONObject(i); |
| | | PlcParameterInfo plcParameterInfo = new PlcParameterInfo(jsonfileobj.getStr("plcAddressBegin")); //参数实例 |
| | | String codeid = parameterObj.getStr("codeId"); |
| | | plcParameterInfo.setCodeId(codeid); |
| | | plcParameterInfo.setAddressIndex(Integer.valueOf(parameterObj.getStr("addressIndex"))); |
| | | //plcParameterInfo.setRatio(Integer.valueOf(parameterObj.getStr("ratio"))); |
| | | plcParameterInfo.setAddressLength(Integer.valueOf(parameterObj.getStr("addressLenght"))); |
| | | plcParameterInfo.setUnit(parameterObj.getStr("unit")); |
| | | plcParameterObject.addPlcParameter(plcParameterInfo); |
| | | } |
| | | return plcParameterObject; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | //初始化bit |
| | | public static PlcBitObject initbit(String jsonFilePath) { |
| | | PlcBitObject plcBitObject = new PlcBitObject(); |
| | | |
| | | try { |
| | | FileReader fileReader = new FileReader(jsonFilePath); |
| | | BufferedReader bufferedReader = new BufferedReader(fileReader); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | String line; |
| | | |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | content.append(line); |
| | | } |
| | | |
| | | bufferedReader.close(); |
| | | fileReader.close(); |
| | | |
| | | JSONObject jsonfileobj = new JSONObject(content.toString()); |
| | | JSONArray jsonArray = jsonfileobj.getJSONArray("parameteInfor"); |
| | | plcBitObject.setPlcAddressBegin(jsonfileobj.getStr("plcAddressBegin"));//设置起始位地址 |
| | | plcBitObject.setPlcAddressLength(Integer.valueOf(jsonfileobj.getStr("plcAddressLenght")));//设置地址长度 |
| | | |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject parameterObj = jsonArray.getJSONObject(i); |
| | | |
| | | PlcBitInfo plcBitInfo = new PlcBitInfo(jsonfileobj.getStr("plcAddressBegin")); //参数实例 |
| | | String codeid = parameterObj.getStr("codeId"); |
| | | plcBitInfo.setCodeId(codeid); |
| | | plcBitInfo.setAddressIndex(Integer.valueOf(parameterObj.getStr("addressIndex"))); |
| | | |
| | | plcBitObject.addPlcBit(plcBitInfo); |
| | | } |
| | | System.out.println(""); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return plcBitObject; |
| | | } |
| | | } |
| | |
| | | package ng.devices; |
| | | |
| | | import java.sql.CallableStatement; |
| | | import java.sql.Connection; |
| | | import java.sql.SQLException; |
| | | import java.util.List; |
| | | |
| | | import builder.S7control; |
| | | import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; |
| | |
| | | |
| | | |
| | | public abstract class ModbusService implements Runnable { |
| | | |
| | | |
| | | //modbus客户端 |
| | | protected ModbusClient client; |
| | |
| | | protected String ip; |
| | | //设备端口 |
| | | protected int port; |
| | | protected String remarks; |
| | | //线程 |
| | | java.lang.Thread thread; |
| | | //缓冲区(服务允许发送超过协议要求的数据,所以缓冲区更大) |
| | |
| | | } |
| | | |
| | | //运行 |
| | | public void Run(int MachineID,String ip,int port,byte state,int ReadOffset,int ReadSize,int timeout,byte FunctionType){ |
| | | public void Run(int MachineID,String ip,int port,byte state,int ReadOffset,int ReadSize,int timeout,byte FunctionType,String remarks){ |
| | | //设置参数 |
| | | this.machineID=MachineID; |
| | | this.ip=ip; |
| | |
| | | this.readOffset=ReadOffset; |
| | | this.readSize=ReadSize; |
| | | this.functionType= FunctionType; |
| | | this.remarks= remarks; |
| | | |
| | | //启动线程 |
| | | if(this.port==102){ |
| | |
| | | } |
| | | return msg; |
| | | } |
| | | //一次s7读取行为 |
| | | String onceS7(){ |
| | | try { |
| | | DBSession sn=null; |
| | | String result=null; |
| | | String flag=null; |
| | | String messsage="000000000000000000"; |
| | | EPlcType ePlcType=null; |
| | | if(this.machineID==78){ |
| | | ePlcType=EPlcType.S1500; |
| | | }else{ |
| | | ePlcType=EPlcType.S1200; |
| | | } |
| | | S7control s7=new S7control(ePlcType, ip, port, 0, 0); |
| | | //List<Integer> word=s7.readWord("DB34.122", 1); |
| | | List<Integer> listWord=s7.readWord(this.remarks,(this.readSize/2)); |
| | | if(listWord==null){ |
| | | System.out.println("notread"); |
| | | return ""; |
| | | } |
| | | for(int i=0;i<listWord.size();i++){ |
| | | //System.out.println("i "+listWord.size()+" "+messsage); |
| | | messsage+=HexUtil.intTo2ByteHex(listWord.get(i)); |
| | | } |
| | | |
| | | //System.out.println(messsage); |
| | | //System.out.println(ip+" "+s7.readWord("DB34.122", 1)); |
| | | //数据库 |
| | | DBHelper db=DBHelper.getDBHelper("mes"); |
| | | try{ |
| | | //创建连接 |
| | | sn=db.createSession(false); |
| | | Connection con= sn.getConnection(); |
| | | //调用那个存储过程 |
| | | CallableStatement sql=con.prepareCall("{call Total_method(?,?,?,?)}"); |
| | | sql.registerOutParameter(3, java.sql.Types.VARCHAR); |
| | | sql.registerOutParameter(4, java.sql.Types.VARCHAR); |
| | | sql.setString(1, messsage); |
| | | sql.setLong(2, machineID); |
| | | sql.execute(); |
| | | //读取返回参数 |
| | | result= sql.getString(3); |
| | | flag= sql.getString(4); |
| | | if (result!=null&&result.length()>0){ |
| | | String []results=result.split(","); |
| | | //得到存储过程返回的值,数组第一个为地址,第二个为值 |
| | | int resultsLength=results.length; |
| | | int resultsSize=resultsLength/2; |
| | | |
| | | for(int i=1;i<resultsSize+1;i++){ |
| | | int addressIndex=(i-1)*2; |
| | | int valueIndex=i*2-1; |
| | | s7.writeWord(results[addressIndex],Integer.valueOf(results[valueIndex]));//参数组写入 |
| | | } |
| | | // if(results.length==2){ |
| | | // s7.writeWord(results[0],Integer.valueOf(results[1]) ); |
| | | // } |
| | | // if(results.length==4){ |
| | | // s7.writeWord(results[0],Integer.valueOf(results[1]));//配方 |
| | | // s7.writeWord(results[2],Integer.valueOf(results[3]));//速度 |
| | | // } |
| | | List<Integer> listWord2=s7.readWord(this.remarks,(this.readSize/2)); |
| | | System.out.println(listWord2.size()); |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | | catch(Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | finally{ |
| | | sn.close(); |
| | | |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | finally{ |
| | | |
| | | } |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | public void SendPLC(int Address, int count, byte[] buffer, int offset) { |
| | | this.client.write(Address, count, buffer, offset); |
| | |
| | | try { |
| | | java.lang.Thread.sleep(UpadateInterval); |
| | | if(this.port==102){ |
| | | if(this.machineID==78){ |
| | | S7control s7=new S7control(EPlcType.S1500, ip, port, 0, 0); |
| | | System.out.println("S7-S1500:"+s7.readWord("DB34.122", 1)); |
| | | s7.writeWord("DB34.122",26); |
| | | System.out.println("S7-S1500:"+s7.readWord("DB34.122", 1)); |
| | | }else{ |
| | | S7control s7=new S7control(EPlcType.S1200, ip, port, 0, 0); |
| | | System.out.println("S7-S1200:"+s7.readWord("DB20.0", 1)); |
| | | } |
| | | String back=onceS7(); |
| | | }else{ |
| | | String back=once(); |
| | | } |
| | |
| | | DBSession sn=null; |
| | | String result=null; |
| | | String flag=null; |
| | | System.out.println(lastGet); |
| | | |
| | | try{ |
| | | //创建连接 |
| | | sn=this.db.createSession(false); |
| | |
| | | //更新最后一次发送 |
| | | this.lastSend=result; |
| | | //返回要发送的报文 |
| | | System.out.println(result); |
| | | return this.fillPack(arg1,result); |
| | | } |
| | | } |
New file |
| | |
| | | package ng.devices; |
| | | |
| | | public class PlcBitInfo { |
| | | |
| | | public PlcBitInfo(String startAddress) { |
| | | this.startAddress = startAddress; |
| | | } |
| | | |
| | | private String startAddress; |
| | | // 参数标识 |
| | | private String codeId; |
| | | |
| | | // 参数名称 |
| | | private String name; |
| | | |
| | | // 读取 参数值 |
| | | private Boolean value; |
| | | // 参数地址 |
| | | private int addressIndex; |
| | | |
| | | public String getCodeId() { |
| | | return this.codeId; |
| | | } |
| | | |
| | | public void setCodeId(String codeId) { |
| | | this.codeId = codeId; |
| | | } |
| | | |
| | | public String getName() { |
| | | return this.name; |
| | | } |
| | | |
| | | public void setName(String name) { |
| | | this.name = name; |
| | | } |
| | | |
| | | public Boolean getValue() { |
| | | return this.value; |
| | | } |
| | | |
| | | public void setValue(Boolean value) { |
| | | this.value = value; |
| | | } |
| | | |
| | | |
| | | public int getAddressIndex() { |
| | | return this.addressIndex; |
| | | } |
| | | |
| | | /** |
| | | * 获取地址 |
| | | * |
| | | * @param index 索引地址 |
| | | */ |
| | | public String getAddress(int index) { |
| | | String[] stringdatas = this.startAddress.trim().split("\\."); |
| | | if (stringdatas.length < 2) { |
| | | return null; |
| | | } |
| | | int dbwindex = 0; |
| | | int bitindex = 0; |
| | | if (stringdatas.length == 3) { |
| | | dbwindex = Integer.parseInt(stringdatas[1]); |
| | | bitindex = Integer.parseInt(stringdatas[2]); |
| | | } else |
| | | { |
| | | return null; |
| | | } |
| | | dbwindex += index / 8; |
| | | bitindex += index % 8; |
| | | return stringdatas[0] + "." + dbwindex + "." + bitindex; |
| | | } |
| | | /** |
| | | * 获取地址 |
| | | * |
| | | */ |
| | | public String getAddress() { |
| | | return getAddress(this.addressIndex); |
| | | } |
| | | |
| | | |
| | | public void setAddressIndex(int addressindex) { |
| | | this.addressIndex = addressindex; |
| | | } |
| | | } |
New file |
| | |
| | | package ng.devices; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | |
| | | public class PlcBitObject { |
| | | |
| | | // 该模块数据类型,数据起始位置 |
| | | private String plcAddressBegin; |
| | | // 数据地址长度:第一参数到最后一个参数的长度 |
| | | private int plcAddressLength; |
| | | //private ArrayList<PlcBitInfo> plcBitList; |
| | | private LinkedHashMap<String,PlcBitInfo> plcBitMap; |
| | | |
| | | /** |
| | | * @return 数据区开始地址 |
| | | */ |
| | | public String getPlcAddressBegin() { |
| | | return plcAddressBegin; |
| | | } |
| | | |
| | | /** |
| | | * @param plcAddressBegin 设置数据区开始地址 |
| | | */ |
| | | public void setPlcAddressBegin(String plcAddressBegin) { |
| | | this.plcAddressBegin = plcAddressBegin; |
| | | } |
| | | |
| | | /** |
| | | * @return 数据区 读取所有数据所需的长度(以byte类型为基准) |
| | | */ |
| | | public int getPlcAddressLength() { |
| | | return plcAddressLength; |
| | | } |
| | | |
| | | /** |
| | | * @return 设置:数据区 读取所有数据所需的长度(以byte类型为基准) |
| | | */ |
| | | public void setPlcAddressLength(int plcAddressLength) { |
| | | this.plcAddressLength = plcAddressLength; |
| | | } |
| | | |
| | | /** |
| | | * @return 获取参数实例集合 |
| | | */ |
| | | public LinkedHashMap<String,PlcBitInfo> getBitMap() { |
| | | return plcBitMap; |
| | | } |
| | | |
| | | /** |
| | | * 根据参数标识 获取某个参数实例 |
| | | * |
| | | * @param codeid 参数标识 |
| | | * @return 获取某个参数实例 |
| | | */ |
| | | public PlcBitInfo getPlcBit(String codeid) { |
| | | if (plcBitMap != null) { |
| | | /*for (PlcBitInfo plcbitInfo : plcBitList) { |
| | | if (plcbitInfo.getCodeId().equals(codeid)) |
| | | return plcbitInfo; |
| | | }*/ |
| | | return plcBitMap.get(codeid); |
| | | } else |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据参数标识 获取某个参数实例 |
| | | * |
| | | * @param codeids 参数标识 |
| | | * @return 获取某个参数实例 |
| | | */ |
| | | public List<Boolean> getPlcBitValues(List<String> codeids) { |
| | | List<Boolean> arrayList = new ArrayList<>(); |
| | | if (plcBitMap != null) { |
| | | for (String codeId : codeids) { // 按照传入参数的顺序遍历 |
| | | arrayList.add(plcBitMap.get(codeId).getValue()); |
| | | } |
| | | } |
| | | return arrayList; |
| | | } |
| | | |
| | | |
| | | public List<String> getAddressListByCodeId(List<String> codeIdList) { |
| | | List<String> addressList = new ArrayList<>(); |
| | | for (String codeId : codeIdList) { |
| | | addressList.add(plcBitMap.get(codeId).getAddress()); |
| | | } |
| | | return addressList; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 添加参数实例 |
| | | * |
| | | * @param param 参数实例 |
| | | */ |
| | | public void addPlcBit(PlcBitInfo param) { |
| | | if (plcBitMap != null) { |
| | | plcBitMap.put(param.getCodeId(), param); |
| | | } |
| | | else { |
| | | plcBitMap = new LinkedHashMap<String,PlcBitInfo>(); |
| | | plcBitMap.put(param.getCodeId(),param); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据PLC返回的数据 给参数实例赋值 |
| | | * |
| | | * @param plcValueArray PLC读取回来的byte类型数据集合 |
| | | */ |
| | | public void setPlcBitList(List<Boolean> plcValueArray) { |
| | | if (plcBitMap != null) { |
| | | Collection<PlcBitInfo> values=plcBitMap.values(); |
| | | for (PlcBitInfo plcbitInfo : values) { |
| | | plcbitInfo.setValue(plcValueArray.get(plcbitInfo.getAddressIndex())); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package ng.devices; |
| | | |
| | | public class PlcParameterInfo { |
| | | public PlcParameterInfo(String startAddress) { |
| | | this.startAddress = startAddress; |
| | | } |
| | | |
| | | private String startAddress; |
| | | // 参数标识 |
| | | private String codeId; |
| | | |
| | | // 参数名称 |
| | | private String name; |
| | | |
| | | // 读取 参数值 |
| | | private String value; |
| | | |
| | | // // 写入 参数值 |
| | | // private String writeValue; |
| | | |
| | | // 参数单位 |
| | | private String unit; |
| | | |
| | | // 参数值转换系数 |
| | | private int ratio; |
| | | |
| | | // 参数地址 |
| | | private int addressIndex; |
| | | |
| | | // 参数地址位长度 |
| | | private int addressLength; |
| | | |
| | | public String getCodeId() { |
| | | return this.codeId; |
| | | } |
| | | |
| | | public void setCodeId(String codeId) { |
| | | this.codeId = codeId; |
| | | } |
| | | |
| | | public String getName() { |
| | | return this.name; |
| | | } |
| | | |
| | | public void setName(String name) { |
| | | this.name = name; |
| | | } |
| | | |
| | | public String getValue() { |
| | | return this.value; |
| | | } |
| | | |
| | | public void setValue(String value) { |
| | | this.value = value; |
| | | } |
| | | |
| | | // public String getWriteValue() { |
| | | // return this.writeValue; |
| | | // } |
| | | |
| | | // public void setWriteValue(String writeValue) { |
| | | // this.writeValue = writeValue; |
| | | // } |
| | | |
| | | public String getUnit() { |
| | | return this.unit; |
| | | } |
| | | |
| | | public void setUnit(String unit) { |
| | | this.unit = unit; |
| | | } |
| | | |
| | | public int getAddressIndex() { |
| | | return this.addressIndex; |
| | | } |
| | | |
| | | public void setAddressIndex(int addressindex) { |
| | | this.addressIndex = addressindex; |
| | | } |
| | | |
| | | public int getAddressLength() { |
| | | return this.addressLength; |
| | | } |
| | | |
| | | public void setAddressLength(int addresslength) { |
| | | this.addressLength = addresslength; |
| | | } |
| | | |
| | | public int getRatio() { |
| | | return this.ratio; |
| | | } |
| | | |
| | | public void setRatio(int ratio) { |
| | | this.ratio = ratio; |
| | | } |
| | | |
| | | /** |
| | | * 获取地址 |
| | | * |
| | | * @param index 索引地址 |
| | | */ |
| | | public String getAddress(int index) { |
| | | String[] stringdatas = this.startAddress.trim().split("\\."); |
| | | int addressLength = this.addressLength; |
| | | if (addressLength < 2) { |
| | | return null; |
| | | } |
| | | int wordindex = Integer.parseInt(stringdatas[1]) + index; |
| | | return stringdatas[0] + "." + wordindex; |
| | | } |
| | | |
| | | public String getAddress() { |
| | | return getAddress(this.addressIndex); |
| | | } |
| | | } |
New file |
| | |
| | | package ng.devices; |
| | | |
| | | import com.github.s7connector.impl.serializer.converter.StringConverter; |
| | | import com.github.xingshuangs.iot.utils.IntegerUtil; |
| | | import com.github.xingshuangs.iot.utils.ShortUtil; |
| | | |
| | | import java.lang.reflect.Array; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | |
| | | public class PlcParameterObject { |
| | | |
| | | // 该模块数据类型,数据起始位置 |
| | | private String plcAddressBegin; |
| | | // 数据地址长度:第一参数到最后一个参数的长度 |
| | | private int plcAddressLength; |
| | | private LinkedHashMap<String,PlcParameterInfo> plcParameterMap; |
| | | |
| | | |
| | | /** |
| | | * @return 数据区开始地址 |
| | | */ |
| | | public String getPlcAddressBegin() { |
| | | return plcAddressBegin; |
| | | } |
| | | |
| | | /** |
| | | * @param plcAddressBegin 设置数据区开始地址 |
| | | */ |
| | | public void setPlcAddressBegin(String plcAddressBegin) { |
| | | this.plcAddressBegin = plcAddressBegin; |
| | | } |
| | | |
| | | /** |
| | | * @return 数据区 读取所有数据所需的长度(以byte类型为基准) |
| | | */ |
| | | public int getPlcAddressLength() { |
| | | return plcAddressLength; |
| | | } |
| | | |
| | | /** |
| | | * @return 设置:数据区 读取所有数据所需的长度(以byte类型为基准) |
| | | */ |
| | | public void setPlcAddressLength(int plcAddressLength) { |
| | | this.plcAddressLength = plcAddressLength; |
| | | } |
| | | |
| | | /** |
| | | * @return 获取参数实例集合 |
| | | */ |
| | | public LinkedHashMap<String,PlcParameterInfo> getPlcParameterMap() { |
| | | return plcParameterMap; |
| | | } |
| | | |
| | | /** |
| | | * 根据参数标识 获取某个参数实例 |
| | | * |
| | | * @param codeid 参数标识 |
| | | * @return 获取某个参数实例 |
| | | */ |
| | | public PlcParameterInfo getPlcParameter(String codeid) { |
| | | if (plcParameterMap != null) { |
| | | return plcParameterMap.get(codeid); |
| | | } else { |
| | | return null; |
| | | } |
| | | /*if (plcParameterList != null) { |
| | | for (PlcParameterInfo plcParameterInfo : plcParameterList) { |
| | | if (plcParameterInfo.getCodeId().equals(codeid)) |
| | | return plcParameterInfo; |
| | | } |
| | | return null; |
| | | } else |
| | | return null;*/ |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据参数标识 获取某个参数实例 |
| | | * |
| | | * @param codeids 参数标识 |
| | | * @return 获取某个参数实例 |
| | | */ |
| | | public List<String> getPlcParameterValues(List<String> codeids) { |
| | | List<String> arrayList = new ArrayList<>(); |
| | | if (plcParameterMap != null) { |
| | | /*Map<String, PlcParameterInfo> resultMap = new LinkedHashMap<>(); // 使用 LinkedHashMap 保留插入顺序 |
| | | for (PlcParameterInfo plcParameterInfo : plcParameterList) { |
| | | if (codeids.contains(plcParameterInfo.getCodeId())) { |
| | | resultMap.put(plcParameterInfo.getCodeId(), plcParameterInfo); |
| | | } |
| | | }*/ |
| | | for (String codeId : codeids) { // 按照传入参数的顺序遍历 |
| | | PlcParameterInfo plcParameterInfo = plcParameterMap.get(codeId); |
| | | if (plcParameterInfo != null) { |
| | | arrayList.add(plcParameterInfo.getValue()); |
| | | } else { |
| | | arrayList.add(null); // 如果找不到对应的值,添加 null |
| | | } |
| | | } |
| | | } |
| | | return arrayList; |
| | | } |
| | | |
| | | /** |
| | | * 根据参数标识 获取传入参数的plc地址 |
| | | * |
| | | * @param codeIdList 参数标识 |
| | | * @return 传入参数的plc地址 |
| | | */ |
| | | public List<String> getAddressListByCodeId(List<String> codeIdList) { |
| | | List<String> addressList = new ArrayList<>(); |
| | | if (plcParameterMap!=null) |
| | | { |
| | | for (String codeId : codeIdList) { |
| | | String address = plcParameterMap.get(codeId).getAddress(); |
| | | if (address != null) { |
| | | addressList.add(address); |
| | | } |
| | | } |
| | | } |
| | | return addressList; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 添加参数实例 |
| | | * |
| | | * @param param 参数实例 |
| | | */ |
| | | public void addPlcParameter(PlcParameterInfo param) { |
| | | if (plcParameterMap != null) { |
| | | plcParameterMap.put(param.getCodeId(), param); |
| | | } |
| | | else { |
| | | plcParameterMap =new LinkedHashMap<String,PlcParameterInfo>(); |
| | | plcParameterMap.put(param.getCodeId(), param); |
| | | } |
| | | /*if (plcParameterList != null) |
| | | plcParameterList.add(param); |
| | | else { |
| | | plcParameterList = new ArrayList<PlcParameterInfo>(); |
| | | plcParameterList.add(param); |
| | | }*/ |
| | | } |
| | | |
| | | /** |
| | | * 根据PLC返回的数据 给参数实例赋值 |
| | | * |
| | | * @param plcValueArray PLC读取回来的byte类型数据集合 |
| | | */ |
| | | public void setPlcParameterList(byte[] plcValueArray) { |
| | | if (plcParameterMap != null) { |
| | | Collection<PlcParameterInfo> values= plcParameterMap.values(); |
| | | for (PlcParameterInfo plcParameterInfo :values) { |
| | | byte[] valueList = new byte[plcParameterInfo.getAddressLength()]; |
| | | // System.out.println(plcParameterInfo.getAddressLength()); |
| | | for (int i = 0; i < plcParameterInfo.getAddressLength(); i++) { |
| | | Array.setByte(valueList, i, plcValueArray[plcParameterInfo.getAddressIndex() + i]); |
| | | } |
| | | if (plcParameterInfo.getAddressLength() == 2) { |
| | | plcParameterInfo.setValue(String.valueOf(ShortUtil.toUInt16(valueList))); |
| | | } else if (plcParameterInfo.getAddressLength() == 4) { |
| | | plcParameterInfo.setValue(String.valueOf(IntegerUtil.toUInt32(valueList))); |
| | | }else if(plcParameterInfo.getAddressLength()==256){ |
| | | StringConverter converter = new StringConverter(); |
| | | String s=""; |
| | | String extract1 = converter.extract(s.getClass(),valueList, 0, 0); |
| | | plcParameterInfo.setValue(extract1); |
| | | }else { |
| | | plcParameterInfo.setValue((byteToHexString(valueList))); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * 把写入值转化为byte[] |
| | | * @param param 参数实例 |
| | | * @param data 写入值的字符类型 |
| | | */ |
| | | public byte[] setValueToBytes(PlcParameterInfo param, String data) { |
| | | if (param.getAddressLength() == 2) { |
| | | return ShortUtil.toByteArray(Integer.parseInt(data)); |
| | | |
| | | } else if (param.getAddressLength() == 4) { |
| | | |
| | | return IntegerUtil.toByteArray(Long.parseLong(data)); |
| | | } |
| | | else if (param.getAddressLength() >10) { |
| | | return data.getBytes(); |
| | | } else { |
| | | return data.getBytes(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * short类型转byte[] |
| | | * |
| | | * @param s short类型值 |
| | | */ |
| | | public static byte[] short2byte(short s) { |
| | | byte[] b = new byte[2]; |
| | | for (int i = 0; i < 2; i++) { |
| | | int offset = 16 - (i + 1) * 8; //计算偏移量 |
| | | b[i] = (byte) ((s >> offset) & 0xff); //把16位分为2个8位进行分别存储 |
| | | } |
| | | return b; |
| | | } |
| | | |
| | | /** |
| | | * byte[]类型转short |
| | | * |
| | | * @param b byte[]类型值 |
| | | */ |
| | | public static short byte2short(byte[] b) { |
| | | short l = 0; |
| | | for (int i = 0; i < 2; i++) { |
| | | l <<= 8; //<<=和我们的 +=是一样的,意思就是 l = l << 8 |
| | | l |= (b[i] & 0xff); //和上面也是一样的 l = l | (b[i]&0xff) |
| | | } |
| | | return l; |
| | | } |
| | | |
| | | /** |
| | | * byte[]类型转short |
| | | * |
| | | * @param b byte[]类型值 |
| | | */ |
| | | public static int byte2int(byte[] b) { |
| | | int l = 0; |
| | | for (int i = 0; i < 4; i++) { |
| | | l <<= 8; //<<=和我们的 +=是一样的,意思就是 l = l << 8 |
| | | l |= (b[3-i] & 0xff); //和上面也是一样的 l = l | (b[i]&0xff) |
| | | } |
| | | return l; |
| | | } |
| | | public static byte[] int2byte(int s){ |
| | | byte[] b = new byte[2]; |
| | | for(int i = 0; i < 4; i++){ |
| | | int offset = 16 - (i+1)*8; //因为byte占4个字节,所以要计算偏移量 |
| | | b[i] = (byte)((s >> offset)&0xff); //把32位分为4个8位进行分别存储 |
| | | } |
| | | return b; |
| | | } |
| | | public static String byteToHexString(byte[] bytes) { |
| | | String str = new String(bytes, StandardCharsets.UTF_8).trim(); |
| | | return str; |
| | | } |
| | | } |