package com.mes.service; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import com.mes.device.PlcParameterInfo; import com.mes.tools.HexConversion; import com.mes.utils.HexUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.*; import java.net.Socket; import java.util.*; /** * Plc协议:协议参数,协议配置,协议请求头,协议类型 */ @Component @Slf4j public class PlcAgreement { public Socket socket =null;//通讯 /** * 协议参数 */ private List parameterKeys=null; private Map parameters=null; /** * 协议路径 */ private String jsonFilePath=null; /** * 读取起始地址 */ public String plcAddressBegin=null; /** * 读取长度 */ public int plcAddressLength=0; //类似序列号(4)+协议标志(4)+长度(4)+从站地址(2)+功能代码(2)+起始地址(4)+读取数量(4) "000100000006010300000032" public String requestHead=null; PlcAgreement(){ jsonFilePath = System.getProperty("user.dir") + "../../JsonFile/PlcCacheGlass.json"; boolean initSuccess=initword(); log.info("初始化PlcCacheGlass:"+initSuccess); } //初始化word public boolean initword() { try { parameters=new LinkedHashMap(); 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 jsonFile = new JSONObject(content.toString()); JSONArray jsonArray = jsonFile.getJSONArray("parameterInfo"); this.plcAddressBegin=jsonFile.getStr("plcAddressBegin");//设置起始位地址 this.plcAddressLength=Integer.valueOf(jsonFile.getStr("plcAddressLength"));//设置地址长度 this.requestHead=jsonFile.getStr("requestHead");//设置请求头部 for (int i = 0; i < jsonArray.size(); i++) { JSONObject parameterObj = jsonArray.getJSONObject(i); String code = parameterObj.getStr("code"); PlcParameter plcParameter = new PlcParameter( code, Integer.valueOf(parameterObj.getStr("addressIndex")), Integer.valueOf(parameterObj.getStr("addressLength")),""); //参数实例 parameterKeys.add(code); parameters.put(code,plcParameter); } return true; } catch (IOException e) { e.printStackTrace(); } return false; } //读取数据 public void read()throws Exception{ int bufSizes = 0; byte[] msgs = new byte[2048]; //写入读取地址 DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream()); outToServer.write(HexConversion.stringToInt(this.requestHead)); outToServer.flush(); //读取内容 DataInputStream in = new DataInputStream(socket.getInputStream()); bufSizes = in.read(msgs); String message = HexConversion.byteToHexString(bufSizes, msgs);//十进制字节数组转十六进制字符串 //获取参数值 for (String key:parameters.keySet()){ parameters.get(key).setReadValue(message); } } //写入数据 public void write(String key,String writeValue)throws Exception{ parameters.get(key); if (writeValue != null && !"".equals(writeValue)) { //写入发送数据 DataOutputStream out = new DataOutputStream(socket.getOutputStream()); out.write(HexConversion.stringToInt(writeValue)); out.flush(); } } //写 public String message(String senddate, String address) { String Herd = "0110" + address; int length = senddate.length() / 4; String dates = Herd + HexUtil.intTo2ByteHex(length) + HexUtil.intTo1ByteHex(length * 2) + senddate; int lengths = dates.length() / 2; String date = "00000000" + HexUtil.intTo2ByteHex(lengths) + dates; return date; } public String getValueString(String key){ return parameters.get(key).toString(); } public int getValueInt(){ return 0; } public double getValueDouble(){ return 0; } }